Uploaded by Mohamad Lababidi

Tutorial4

advertisement
Tutorial 4
Write a program that asks the user for a number between 1 and 3 until the answer is correct.
Write a program that asks for a number between 10 and 20, until the answer is correct. In the
event of a response greater than 20, a message will appear: "Smaller! ", And conversely," Bigger! " if
the number is less than 10.
Write a program that asks for a starting number, and then displays the next ten numbers. For
example, if the user enters the number 17, the program will display the numbers from 18 to 27.
Write a program which asks for a starting number, and which then writes the multiplication table of
this number, presented as follows (case where the user enters the number 7):
Table of 7 :
7 x 1 = 7
7 x 2 = 14
7 x 3 = 21
…
7 x 10 = 70
Write a program that asks for a starting number, and which calculates the sum of integers up to
that number. For example, if you enter 5, the program should calculate:
1 + 2 + 3 + 4 + 5 = 15
NB: you have to display only the result, not the decomposition of the calculation.
Write a program which asks for a starting number, and which calculates its factorial.
NB: the factorial of 8, noted 8 !, is worth :
1x2x3x4x5x6x7x8
Write a program that successively asks the user for 20 numbers, and then tells the user which was
the greatest among those 20 numbers:
Enter the value of number 1 : 12
Enter the value of number 2 : 14
etc.
Enter the value of number 20 : 6
The largest of these numbers is : 14
Then modify the algorithm so that the program also displays in which position this number was
entered:
It was the value of number 2
Rewrite the previous program, but this time it is not known how many numbers the user wants to
enter. Entering numbers stops when the user enters a zero.
Write a program that allows you to know your chances of winning the trifecta, quarté, quinté and
other voluntary taxes.
The user is asked for the number of starting horses, and the number of horses played. The two
messages displayed should be:
In order : one in X chance of winning
In disorder : one in Y chance of winning
X and Y are given to us by the following formula, if n is the number of starting horses and p the
number of horses played (remember that the sign ! Means "factorial", as in exercise 4.6 above):
X = n ! / (n - p) !
Y = n ! / (p ! * (n – p) !)
NB: this program can be written in a simple way, but relatively inefficient. Its performance can be
significantly increased by a little trick. You'll start by writing the easiest way, then identify the
problem, and write a second version to fix it.
Download