0% found this document useful (0 votes)
41 views16 pages

TD CSC201

Uploaded by

ghida2005
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
41 views16 pages

TD CSC201

Uploaded by

ghida2005
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 16

Université La Sagesse

Faculté d’ingénierie POLYTECH

Département d’Informatique et de Télécommunication

TD

CSC201 : Computer Programming I – C++ (3 Crédits.)


(Programmation Informatique I)

Teacher: Wajdi ABBOUD


Salle- 4F 10

Fall 2023-2024

TD_CSC201 --- Wajdi Abboud 1


Exercise 1:
Write a C++ program that ask the user for an integer and display it.

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

The average of these numbers is: 6.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:

TD_CSC201 --- Wajdi Abboud 2


Introduce the product price in USD: 150
Tax T1: 10.5 $
Tax T2: 9.75 $

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

TD_CSC201 --- Wajdi Abboud 3


Exercise 9:
Using the instruction If…Else; write a C++ program that take a student’s grade at a class
and then display his status from the grade.
The status is given in the following table:

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.

The dialogue should look like:


Enter two integers a and b: -3 2
The value of c is: -1
Do you want to continue (y/n)? y

Enter two integers a and b: 1 5


The value of c is: 12
Do you want to continue (y/n)? n

TD_CSC201 --- Wajdi Abboud 4


Exercise 11:
Write a C++ program for a company to do the following:
a) Read each employee personal information:
− Employee number (integer),
− Actual salary (double).
b) Validate the salary (the salary should be in the interval: ]1500, 12000] )
c) Calculate the salary raise as following:
− 1st section : ]1500, 3000] including the raise of 30%
− 2sd section : ]3000, 7500] including the raise of 15%
− 3rd section : ]7500, 12000] including the raise of 10%

The program will repeat as long as the user wants.

Execution sample:
Enter the employee number: 12

Enter the actual salary: 8000


7500<8000≤12000 then, salary is raised of 10% (3rd section):
8000*0.10 = 800
So, the new salary will be: 8000 + 800 = 8800

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

TD_CSC201 --- Wajdi Abboud 5


Exercise 13:
Write a C++ program that introduce an integer n (between 5 and 25) from the keyboard,
display odd numbers between 1 and n, calculate and show the sum of the odd numbers
from 1 to n.

The dialogue should resemble to:


Enter n (between 5 and 25) : 9
Odd numbers from 1 to 9 are:
1 3 5 7 9

Sum of odd integers from 1 to 9 is: 25

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:

Enter 2 positive integers: 14 15


14 et 15 (friends)

Enter 2 positive integers: 9 13


9 et 13 (not friends)

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)

Divisors of 9 are: 1, 3 & 9 Sum of divisors of 9 (1+3+9= 13)


Divisors of 13 are: 1 & 13 Sum of divisors of 13 (1+13= 14)

TD_CSC201 --- Wajdi Abboud 6


Exercise 15:
Assume you want to generate a table of multiples of any given number. Write a C++
program that allows the user to enter the number, and then generates the table, formatting
it into 10 columns and 4 lines.

Interaction with the program should look like this:


Enter a number : 5

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.

For example, if n = 5, the form will be as follows:

* * * * *
* * * * *
* * * * *
* * * * *
* * * * *
* * * *
* * * *
* * * *
* * * *
* * *
* * *
* * *
* *
* *
*

Note: Use nested for loops to display spaces and stars.

TD_CSC201 --- Wajdi Abboud 7


Exercise 17:
Write a C++ program using a function or a method called Sum(…) that return the sum S
of the following:
S = 12-22+32-42+52-62+72 …+n2

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.

The dialogue should resemble to:


Enter an integer n strictly positive: 4

Sum is: -10


Sum is 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.

Some interaction with the program might look like this:

Enter initial amount: 3000


Enter number of years: 10
Enter interest rate (percent per year) : 5.5 %

At the end of 10 years, you will have 5124.43 dollars

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.

TD_CSC201 --- Wajdi Abboud 8


Exercise 20:
An integer is said to be a perfect number if the sum of its factors, including 1 (but not the
number itself), is equal to the number. For example, 6 is a perfect number, because 6 =
1 + 2 + 3. Write a function that determines whether a number is perfect. Use this function
in a program that determines and prints all the perfect numbers between 1 and 1000.

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:

TD_CSC201 --- Wajdi Abboud 9


Exercise 22:
Write a C++ program that introduce the size n of an integer table T (maximum size 20
elements), fill the table values and display the odd elements of the table. Then calculate
and display the sum of the elements.

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

Odd elements of the table are:


11 33 55

Total sum is: 143

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

The maximum value of T is: 55


The minimum value of T is: 10

TD_CSC201 --- Wajdi Abboud 10


Exercise 24:
Write a C++ program that calculate and display the average avg of table V elements of
type real (table size ≤ 10) and the sum S given in the following formula:

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

The average avg is: 4


The sum S is: 11

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 elements of table TPOS are:


7 3 13 11 77

The elements of table TNEG are:


-5 -12 -32 -14

TD_CSC201 --- Wajdi Abboud 11


Exercise 26:
Write a C++ program that allow the user to enter 5 grades between 0 and 20 in table V.
Once the program read the grades, it calculates the arithmetic average of class grades
excluding the highest and the lowest grade. It should display the number of grades above
this average.

The dialogue should resemble to:


Enter 5 grades between 0 and 20
Enter grade 1 between 0 and 20: 20
Enter grade 2 between 0 and 20: 14
Enter grade 3 between 0 and 20: 25
Enter grade 3 between 0 and 20: 7
Enter grade 4 between 0 and 20: 10
Enter grade 5 between 0 and 20: 3

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.

For example: if a skater scored the following points:


5.8 5.6 5.6 5.9 5.7 5.9 5.4 5.8

The lowest score is: 5.4 (only once) we eliminate it


The highest score is: 5.9 (twice) we eliminate it only once
The skater score is:
(5.8 + 5.6 + 5.6 + 5.7 + 5.9 + 5.8) / 6

Write a C++ program allowing to:


- introduce tables of the 8 scores of 3 skaters;
- calculate and display the final score (average of the 6 saved scores) of each skater.

TD_CSC201 --- Wajdi Abboud 12


The screen should display the following dialogue:
Introduce the 8 scores of skater 1:
Score 1: 5.8
Score 2: 5.6
Score 3: 5.6
Score 4: 5.9
Score 5: 5.7
Score 6: 5.9
Score 7: 5.4
Score 8: 5.8
The final score of skater 1 is: 5.73333

Introduce the 8 scores of skater 2:


Score 1: 5.4
Score 2: 5.3
Score 3: 5.2
Score 4: 5.2
Score 5: 5.1
Score 6: 5.3
Score 7: 5.4
Score 8: 5.1
The final score of skater 2 is: 5.25

Introduce the 8 scores of skater 3:


Score 1: 5.8
Score 2: 5.9
Score 3: 6.0
Score 4: 5.9
Score 5: 5.8
Score 6: 5.7
Score 7: 5.9
Score 8: 5.8
The final score of skater 3 is: 5.85

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

b[8] = 4.0 -3.0 2.0 -1.0 0.0 0.0 0.0 0.0

Write a C++ program that:


- Call the function or method "ScalarProduct" which calculates the scalar product
of the first n elements of “a” with the first n elements of “b”. This is defined as
the sum of the products corresponding terms. So the call of ScalarProduct(a,5,b)
returns:

(2.2)(4.0) + (3.3)(-3.0) + (4.4)(2.0) + (5.5)(-1.0) + (6.6)(0.0) = 2.2

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

TD_CSC201 --- Wajdi Abboud 14


Exercise 31:
Write a C++ program that does the following:
1- Declare an array of integer A[n][n] where n<=10, then ask the user to fill this
array.
2- Replace all even elements of the two diagonals of A by zero. Print A.
3- Print the sum of each line in A.

Execution sample:
Enter the length n of array A: 3

Enter the elements of A:


1 2 4
4 7 6
2 3 4

After replacement, A becomes:


1 2 0
4 7 6
0 3 0

Sum of elements in line 1: 3


Sum of elements in line 2: 17
Sum of elements in line 3: 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.

The dialogue should look like this:

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

TD_CSC201 --- Wajdi Abboud 15


Tab 1 contains : 11 12 13 14 15 16 17 18 19 20
Tab 2 contains : 11 12 13 14 15 16 17 18 19 20

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.

Hint: use successive addition to multiply the two numbers.


Example: If x=7 and y=5 then 7*5 = 7+7+7+7+7

- Call this function through the main() menu.

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.

TD_CSC201 --- Wajdi Abboud 16

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy