ClASS 9 - COMPUTER - WOOKBOOK - CHAPTER - 9 (ITERATIVE CONSTRUCT IN JAVA, PART - 1) 1

You might also like

Download as pdf or txt
Download as pdf or txt
You are on page 1of 4

ITERATION PROGRAMMMING

EXERCISE
Page:236
Q2. Write a for () loop statement /code to print all natural numbers from 1 to 20.
Answer : System.out.println( “ Displaying all natural numbers from 1 to 20 in
different lines”);
for( int x=1;x<=20;x++)
{
System.out.println(x);
}

Q3. Write a while() loop to print all natural numbers from 1 to 10.
Answer : System.out.println( “ Displaying all natural numbers from 1 to 10 in same
line);
int x=1;
while(x<=10)
{
System.out.print(x+ “ , ”);
x++; // x=x+1;
}

Q4. Write a do.. while() loop to print all natural numbers from 1 to 15.
Answer : System.out.println( “ Displaying all natural numbers from 1 to 15 in same
line);
int x=1;
do
{
System.out.print(x+ “ , ”);
x++; // x=x+1;
}
while(x<=15);

Q5. What will be the output of the following :


int m=75;
while ( true)
{
if (m<10)
break;
m-=10;
}
System.out.println(" The value of m is :"+m);
Answer : Output :
m= 75 m<10 break m - = 10
False m= 75 -10 =65
m=65 False m= 65-10=55
m=55 False m= 55-10=45
m=45 False m= 45-10=35
m=35 False m= 35-10=25
m=25 False m= 25-10=15
m=15 False m= 15-10=5
m=5 True
The value of m is : 5
Q6. Write do.. while() loop code to print all natural numbers from 15 to 1 ( in
reverse order ).
Answer : System.out.println( “ Displaying all natural numbers from 15 to 1 in reverse
order );
int x= 15 ;
do
{
System.out.print(x+ “ , ”);
x--; // x=x-1;
}
while(x>=1);

Q7. Rewrite the following code using the while () loop :


for( int j=1; j<=12;j++)
{
System.out.println(j+ “ * ” + j + “ = ” + j*j);
j++ ;
}

Answer : The while () loop format of the mentioned loop is :


int j=1;
while (j<=12)
{
System.out.println(j+ “ * ” + j + “ = ” + j*j);
j+=2 ; // j=j +2;
}

Q8. . Rewrite the following code using the do while () loop :


for( int j=1; j<=12;j++)
System.out.println(j+ “ * ” + j + “ = ” + j*j);
Answer : The do..while () loop format of the mentioned loop is :
int j=1;
do
{
System.out.println(j+ “ * ” + j + “ = ” + j*j);
j=j+1; //j++;
}
while (j<=12);

Page : 238
Q9. Rewrite the following code using the while () loop :
for( int x=10; x>=0 ; x--)
System.out.println(6+ “ * ” + x + “ = ” + 6*x);

Answer : The while () loop format of the mentioned loop is :


int x=10;
while (x>=0)
{
System.out.println(6+ “ * ” + x + “ = ” + 6*x);
x--; //x=x-1;
}

Q10. Rewrite the following code using the do while () loop :


for( int j=12; j>=1 ; --j)
System.out.println(j+ “ * ” + j + “ = ” + (j+j));

Answer : The do while () loop format of the mentioned loop is :


int j=12;
do
{
System.out.println(j+ “ * ” + j + “ = ” + (j+j));
j--; //j=j-1;
}
while (j>=1);

Q11. Rewrite the following code using the for() loop :


int y=10;
do
{
System.out.println( y*3);
y=y-1;
}
while(y>=1);
Answer : The for () loop format of the mentioned loop is :
int y;
for ( y=10 ; y>=1; y=y-1)
{
System.out.println( y*3);
}

Q12. Rewrite the following code using the for() loop :


int n=10;
while( n<=15)
{
n++;
System.out.println( n*10);
}
Answer : The for () loop format of the mentioned loop is :
for ( int n=10 ; n<=15; n++)
{

System.out.println( n*10);
}

Q13. Rewrite the following code using the for() loop :


int a=10 , s=0;
while( a>0)
{
a--; s+=a;
}
System.out.println( “ The final value of s= ” + s);

Answer : The for () loop format of the mentioned loop is :


int a , s=0;
for( a=10 ; a>0 ; a--)
{
s+=a;// s=s+a
}
System.out.println( “ The final value of s=”+s);

You might also like