TP 3 Solution to Share
TP 3 Solution to Share
1
P1. Sum, Product, and Average
1. Using a while loop, write a program that asks the user for an integer N. Then, the program asks
the user to enter N integers and displays their sum, product, and average.
2. Rewrite the program using a do...while loop.
Solution :
Using ‘while’ loop :
2
P2. Guessing Game
Write a program that implements a game where the goal is to guess an integer given by the first
player. The number must be between 0 and 100. The program asks the second player to guess the
number and provides feedback if the guess is too high or too low, and it continues until the second
player correctly guesses the number. The objective is to find the number in as few guesses as
possible.
Here’s an example of the program’s execution:
Welcome to the guessing game !
I m thinking of a number between 1 and 100.
Enter your guess : 50 Too low ! Try again .
Enter your guess : 75 Toohigh ! Try again .
Enter your guess : 60 Too low ! Try again .
Enter your guess : 62
Congratulations ! You ve guessed the number in 4 attempts !
Solution :
3
P3. Multiplication table
Write a program that displays a multiplication table in the following format:
X*Y| 0 1 2 3 4 5 6 7 8 9 10
- - -| - -- - -- - -- - -- - -- - -- - -- - -- - -- - -- - --
0 | 0 0 0 0 0 0 0 0 0 0 0
1 | 0 1 2 3 4 5 6 7 8 9 10
2 | 0 2 4 6 8 10 12 14 16 18 20
3 | 0 3 6 9 12 15 18 21 24 27 30
4 | 0 4 8 12 16 20 24 28 32 36 40
5 | 0 5 10 15 20 25 30 35 40 45 50
6 | 0 6 12 18 24 30 36 42 48 54 60
7 | 0 7 14 21 28 35 42 49 56 63 70
8 | 0 8 16 24 32 40 48 56 64 72 80
9 | 0 9 18 27 36 45 54 63 72 81 90
10 | 0 10 20 30 40 50 60 70 80 90 100