Computer Science
Rewrite the following using for loop:
a=1;
while(a<=5)
{b = 1;
while(b<=a)
{
System.out.print(b);
b++;
}
System.out.println();
a++;
}
Java Iterative Stmts
5 Likes
Answer
for (a = 1; a <= 5; a++) {
for (b = 1; b <= a; b++) {
System.out.print(b);
}
System.out.println();
}
Answered By
2 Likes
Related Questions
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);
State the final value of q at the end of the following program segment after execution. Show the dry run.
for(m=2;m<=3;++m) { for(n=1;n<=m;++n) { p=m+n-1; if(p%3 == 0) q += p; else q += p+4; } }
Write the following statement using switch case construct.
if (a==1) { b += 10; System.out.println(b); } else if (a == 2) { b -= 10; System.out.println(b); } else { b *= a; System.out.println(b); }