Page 233 - CA_Blue( J )_Class9
P. 233
System.out.print(i+ " ” );
}
Ans. -2 -4 -6 -8 -10
9. Convert the for loop to a while loop.
int a;
for (a=2; a<=10;)
{
a++;
System.out.print (a+ " : ");
}
Ans. int a;
a=2;
while(a<=10)
{
a++;
System.out.print(a+ " : " );
}
10. Print the output of the program of question 9:
Ans. 3 : 4 : 5 : 6 : 7 : 8 : 9 : 10 : 11 :
11. Convert the do-while loop to a for loop.
int i=1;
do
{
System.out.println(i+2);
i+=2;
}while(i<5);
Ans. for(int i=1; i<5; i+=2)
{ System.out.println(i+2); }
12. Convert the while loop to a for loop.
int n=123, s=0;
while(n>0)
{
s=s+n%10;
n=n/10;
}
System.out.println(s);
Ans. int s=0;
for(int n = 123; n>0;)
{
s=s+n%10;
n=n/10;
}
System.out.println(s);
Iterative Constructs in Java 231

