Question Three
Rewrite the following peace of code using a for loop.
Public static void display (int [] ages) {
int index= O;
do {
System.out. println( ages[index ]);
index++;
} While (index< ages.lenght);
}
[8 Marks]
SECTION D: CODING/THEORY
Answer ALL questions
Each correct answer is allocated 8 Marks
Question o
One
[8 Marks]
Write a Java program that computes the areas of 3 circles, giving that:
Area= PR2 where P = 3.14, R for Radius.
For each circle, the program should accept a number as input from the keyboard as Radius.
The program should display this as Sample Output:
Please Enter Radius: 5
Area= 78.5
[Hint: Area= 3.14*5*5=78.5]
Please Enter Radius: 9
Area= 254.34
[Hint: Area= 3.14*9*9=254.34]
Please Enter Radius: 4
Area= 50.24
[Hint: Area= 3. 14*4*4=50.24]
Note that the Hint is not part of the output.
Question Two
[8 Marks]
Write a Java program that will ask for two integer numbers as input and print out the Addition,
Multiplication, Subtraction and Division separated by space.
[Hint: Addition= A+B, Subtraction= A-B, Multiplication= A* B, Division= A/B]
Question Three
[8 Marks]
Write a Java program to sum the square of numbers between two numbers startNo and endNo.
You may either use a for statement or any other looping statement in Java. startNo and endNo
should be accepted as input from the keyboard.
(a) If startNo is less than Othen the program should print "startNo cannot be less than O"and the
program will end.
(b) If startNo is greater than endNo then the program should print "startNo must NOT be greater
than endNo" and the program will end.
(c) But, if the right numbers (startNo and endNo) are entered ie any two numbers within 0, I, 2, 3, 4, 5,
6, 7, 8, 9, then the sum of squares of numbers between startNo and endNo will be calculated and the
program will printout "Sum of squares of Numbers=" sumSquares.
[Hint: For instance, if you enter 2, 4, then all numbers from 2 to 4 are: 2,3,4 therefore Sumsq =
(2*2)+(3 *3)+( 4*4)=29]
---
THE END ---
---
---
4