Prog (C++) 621 (P)
Prog (C++) 621 (P)
Prog (C++) 621 (P)
Page 1 of 2
SECTION B (COMPULSORY) (20 MARKS)
QUESTION TWO
2.1 Write a program that reads a Celsius degree in a double value from the console, then converts
it to Fahrenheit and displays the Result. The formula for the conversion is as follows:
Fahrenheit = (9 / 5) * Celsius + 32
Hint: In C++, 9 / 5 is 1, but 9.0 / 5 is 1.8. (10)
2.2 Write a program that reads in the radius and length of a cylinder and computes the area and
volume using the following Formulas:
Area = radius *
radius * p Volume
= area * length
Use the Math class from C++ to handle P for a pie constant. (10)
SECTION C (ANSWER ANY ONE QUESTION FROM THIS SECTION) (60 MARKS)
3.1 Write a program that reads an integer between 0 and 1000 and adds all the digits in the integer.
For example, if an integer is 932, the sum of all its digits is 14.
Hint: Use the % operator to extract digits, and use the / operator to remove the extracted digit.
For instance, 932 % 10 = 2 and 932 / 10 = 93. (15)
3.2 Write a program that prompts the user to enter a three-digit integer and determines whether it
is a palindrome number. A number is palindrome if it reads the same from right to left and from
left to right. (15)
4.1 Write a method that computes the sum of the digits in an integer. Use the following method
header: public static int sumDigits(long n)
For example, sumDigits(234) returns 9 (2 + 3 + 4). (Hint: Use the % operator to extract digits,
and the / operator to remove the extracted digit. For instance, to extract 4 from 234, use 234
% 10 (= 4). To remove 4 from 234, use 234 / 10 (= 23). Use a loop to repeatedly extract and
remove the digit until all the digits are extracted. Write a test program that prompts the user
to enter an integer and displays the sum of all its digits. (30)
TOTAL MARKS: 50
Page 2 of 2