Computer Science
Rewrite the following for loop by using while and do-while loops:
int p = 20;
for(k=p;k>=0;k-=2)
{
s += k;
}
System.out.println("Sum="+s);
Java Iterative Stmts
11 Likes
Answer
// Using while loop
int p = 20;
k = p;
while (k >= 0) {
s += k;
k-=2;
}
System.out.println("Sum="+s);
// Using do-while loop
int p = 20;
k = p;
do {
s += k;
k-=2;
} while (k >= 0);
System.out.println("Sum="+s);
Answered By
5 Likes
Related Questions
Rewrite the following statement using if-else:
c = (x >= 'A' && x <= 'Z') ? "Upper Case Letter": "Lower Case Letter";
Rewrite the following for loop by using while and do-while loops:
for(i=1,j=1;i<=10;i++,j++) { System.out.println(i*j); }
Rewrite the following for loop by using while and do-while loops:
int a=37, b, c=0; for(b=1;b<=a;b++) { if (a % b == 0) c = c + 1; } if(c == 2) System.out.println(a + " is a prime number"); else System.out.println(a + " is not a prime number");
Rewrite the following using for loop:
int i = 1, f = 1; do { f=f*i; i++; } while(i<=10); System.out.println(f);