Exercises On Loops
Exercises On Loops
1. Write a program which inputs a person’s height (in centimeters) and weight (in
kilograms) and outputs one of the messages: underweight, normal, or
overweight, using the criteria:
2. Assuming that n is 20, what will the following code fragment output when
executed?
if (n >= 0)
if (n < 10)
cout << "n is small\n";
else
cout << "n is negative\n";
3. Write a program which inputs an integer value, checks that it is positive, and
outputs its factorial, using the formulas:
factorial(0) = 1
factorial(n) = n × factorial(n-1)
4. Write a program which inputs an octal number and outputs its decimal
equivalent.
The following example illustrates the expected behavior of the program: