1. Transform the following while loop into an equivalent for loop (make sure it
ID: 3663874 • Letter: 1
Question
1. Transform the following while loop into an equivalent for loop (make sure it produces the exact same output). For full credit, use only one variable and declare that variable inside the for-loop declaration.
int num = 1;
while (num < 20) {
num++;
System.out.println (num);
}
2. Transform the following for loop into an equivalent while loop (make sure it produces the exact same output). For full credit, use only one variable.
for(int i=5; i>=0; i--) {
System.out.println(i);
}
3. Write program statements to calculate and print to the console the average of the three numbers below. (Be sure you are calculating the complete and correct average!)
int num1, num2, num3;
Explanation / Answer
Answer :1
for(int num =1;num<20;)
{
num++;
System.out.println (num);
}
Answer :2
int i= 5;
while (i >= 0)
{
System.out.println (i);
i--;
}
Answer 3:
class AverageCal
{
public static void main(String args[])
{
int num1, num2, num3, average;
num1 = 3;
num2 = 7;
num3 = 2 ;
average = ( num1 + num2 + num3 ) / 3;
System.out.println(" Average : "+average);
}
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.