TD CSC201
TD CSC201
TD
Fall 2023-2024
Execution sample:
Enter an integer: 5
The integer is: 5
Exercise 2:
Write a C++ program that introduce a person age, then display his age after 10 years.
Execution sample:
What is your age? 19
In 10 years, you will be: 29
Exercise 3:
Write a C++ program that add and subtract two numbers, then display the two results.
Execution sample:
Enter first number A: 5
Enter second number B: 4
A+B=9
A-B=1
Exercise 4:
Write a C++ program that introduce 3 reals numbers given by the user, calculate their
average and then display the result.
Execution sample:
Enter first number: 3.0
Enter second number: 5.0
Enter third number: 10.0
Exercise 5:
Write a C++ program that allow the user to enter a product price (real number). Then,
calculate the taxes T1 (7% of price) and T2 (6.5% of price) and display information as
follows:
TOTAL: 170.25 $
Exercise 6:
Write a C++ program that indicate if a given integer is odd or even.
Execution sample:
Enter a number x: 5
The given number (5) is odd.
Exercise 7:
Write a C++ program that indicate if a given integer is negative, positive or null.
Execution sample:
Enter a number: -5
The given number (-5) is negative.
Exercise 8:
Write a C++ program that indicate if the given number is multiple of 3 or 5, or multiple
of (3 and 5) in the same time.
Execution sample:
Enter a number: 15
15 is multiple of 3
15 is multiple of 5
15 is multiple of 3 and 5
Grade Status
grade<10 Failed
10≤ grade <13 Passed
13≤ grade <16 Good
16≤ grade < 18 Very Good
18≤ grade ≤20 Excellent
Exercise 10:
Write a C++ program (method 1 using If …Else, method 2 using Switch) that:
- Read two variables a and b of type integer given from the keyboard.
- Calculate and display through a+b the value of c according to the following
table:
If a+b = -1 c = a+2
If a+b = 1 or 2 or 5 c = ab2
If a+b = 3 or 4 or 6 c = 3(b-a)
Any other value c = a+b+3
- Repeat same operation as long as the user wants to continue.
Execution sample:
Enter the employee number: 12
Continue (y/n)? n
Exercise 12:
Write a C++ program that allow the user to enter a strictly positive integer in order to
calculate and display the value of S in the following case:
S = 12+22+32+42+52+…+n2
Execution sample:
Enter an integer strictly positive: 3
Sum: 14
Exercise 14:
Two positive integers are called friends if the sum of the divisor of the first integer is
equal to the second and vice versa.
Write a C++ program that reads through the keyboard two positive integers given by the
user then determine if the two integers are friends or not.
Execution sample:
In fact:
Divisors of 14 are: 1, 2, 7 & 14 Sum of divisors of 14 (1+2+7+14= 24)
Divisors of 15 are: 1, 3, 5 & 15 Sum of divisors of 15 (1+3+5+15= 24)
5 10 15 20 25 30 35 40 45 50
55 60 65 70 75 80 85 90 95 100
105 110 115 120 125 130 135 140 145 150
155 160 165 170 175 180 185 190 195 200
Exercise 16:
Write a C ++ program that displays the form given below in stars where n is the number
of stars on the side of the first square.
* * * * *
* * * * *
* * * * *
* * * * *
* * * * *
* * * *
* * * *
* * * *
* * * *
* * *
* * *
* * *
* *
* *
*
Instructions to follow: through a main menu, read/take a strictly positive integer, call
function or method Sum(…), then display if the returned sum is positive or negative.
Exercise 18:
Write a function called Interest() that calculate how much money you’ll end up with if
you invest an amount of money at a fixed interest rate, compounded yearly. Have the user
furnish the initial amount, the number of years, and the yearly interest rate in percent.
Create a main program in C++ to exercise the function.
NB: At the end of the first year you have 3000 + (3000*0.055), which is 3165. At the end
of the second year you have 3165+(3165*0.055), which is 3339.08
Do this as many times as there are years. A for loop makes the calculation easy.
Exercise 19:
Using a C++ main menu, write a program to:
− Introduce two strictly positive integers x and y,
− Call a function named Multiply(x,y) of 2 parameters that allows to return the
multiplication of x by y using the method of successive addition to perform this
calculus operation,
Example: If x=7 and y=5 Then 7*5 = 7+7+7+7+7
− Display the result from the main menu.
Sample Output:
For the integers from 1 to 1000:
6 is perfect number
28 is perfect number
496 is perfect number
Exercise 21:
An integer is said to be prime if it is divisible by only1 and itself.
For example, 2, 3, 5 and 7 are prime, but 4, 6, 8 and 9 are not.
- Write a function to determine if a number is prime.
- Use this function in a program that determines and prints all the primenumbers between 1
and 1000.
Sample Output:
Execution sample:
Enter the number of table elements (max = 20): 5
Enter 5 integers:
T [0] = 11
T [1] = 22
T [2] = 33
T [3] = 55
T [4] = 22
Exercise 23:
Write a C++ program that introduce the size n of an integer table T(maximum size 20
elements), fill the table values then calculate and display the maximum and minimum
values of the table elements.
Execution sample:
Enter the number of elements of table T(max = 20): 5
Enter 5 integers:
T [0] = 11
T [1] = 22
T [2] = 55
T [3] = 33
T [4] = 10
S = (V(0) − avg )0 + (V(1) − avg )1 + (V(2) − avg )2 + ..... + (V(n −1) − avg )N-1
The program should:
1- Read the value of n and introduce the elements of table V.
2- Calculate and display the average avg of table V elements.
3- Calculate and display the sum S.
Execution sample:
Enter the size of table V (max = 10): 5
Introduce 5 elements:
Enter V[0]: 2
Enter V[1]: 4
Enter V[2]: 5
Enter V[3]: 6
Enter V[4]: 3
Exercise 25:
Write a C++ program that introduce the size n of an integer table (maximum size 30
elements), fill the table values, then copy the strictly positive values into table TPOS and
the strictly negative values into table TNEG. Display all values of table TPOS and table
TNEG.
Execution sample:
Enter table T size (maximum 30): 9
Enter 9 integers:
7 -5 3 -12 13 11 -32 77 -14
The average excluding the highest and the lowest grade is: 10.33
The number of grades above the average is: 2
Exercise 27:
In artistic skating eight judges give an actual score between 0 and 6.
The highest score and the lowest score are eliminated (just once) and the average of the 6
saved scores is displayed.
Exercise 28:
Write in C++ a function with the following header:
Bool Double_I (int tab[], int n)
- tab: is a table of integers.
- n: the number of elements in tab.
This function should return value true if each element of the table is equal to twice its
previous and false otherwise.
If the table has [3, 6, 12, 24] as elements, then function Double_I should return value
true.
Through a main menu (main); enter an integer table of size n strictly positive and call
function Double_I.
TD_CSC201 --- Wajdi Abboud 13
Exercise 29:
The following two tables (one dimension) of 8 real numbers are given:
0 1 2 3 4 5 6 7
a[8] = 2.2 3.3 4.4 5.5 6.6 7.7 8.8 9.9
Exercise 30:
Write and test a C++ function/procedure which implements the Shuffle (Brassage
parfait) of a one dimensional table having an even number of elements. For example, it
will replace:
{11, 22, 33, 44, 55, 66, 77, 88} By {11, 55, 22, 66, 33, 77, 44, 88}
0 1 2 3 4 5 6 7
11 22 33 44 55 66 77 88
11 55 22 66 33 77 44 88
0 1 2 3 4 5 6 7
Execution sample:
Enter the length n of array A: 3
Exercise 32:
Using a main menu; write a C ++ program allowing to:
- enter a square matrix A [n][n] of integer type (it is up to the user to enter the value of n
between 3 and 10).
- put in a one-dimensional array Tab1 the lower triangular part of matrix A and in
another table Tab2 the elements of the upper triangular part of matrix A.
- call a boolean function called Comparison (...) having as arguments the 2 arrays Tab1
and Tab2 as well as their size. This function is used to compare if the first array Tab1
contains the same elements of the second array Tab2.
Enter n: 5
Enter the elements of matrix A:
5 11 12 13 14
11 5 15 16 17
12 13 5 18 19
14 15 16 5 20
17 18 19 20 5
The Tab1 array contains the same elements of the Tab2 array.
Exercise 33:
Write a recursive function named Multiply(x, y) which returns the multiplication of two
integer numbers given by the user.
Exercise 34:
Write a recursive function named PalindromeTab(Tab, n.) that determines if an array of
n integers is palindrome (n is a strictly positive integer), meaning if it is equal to its
inverse. If the table contains four elements [2, 5, 5, 2], the PalindromeTab(Tab, n.)
function should return true.
- Call this function through the main() menu.
Exercise 35:
Write a recursive function SquareSum(Tab, n) which calculates the sum of squares of n
elements in an integer table (n is strictly positive).
- Call this function through the main() menu.