Rewrite Questions in JAVA

Rewrite  Questions:

1. Rewrite the following program code using a for loop:
inti,sum=0;
while(i<10)
{ sum +=i;
 i+=2;
}
Ans: int i,sum=0;
for(i=0;i<10;i+=2)
{ sum +=i;
}

2. Rewrite the following code using while loop :
inti,j;
for(i=1;i<=4;i++)
{ for(j=1;j<=i;++j)
{ System.out.print(j);
}
System.out.println();
}
Ans:
int i=1,j;
while(i<=4)
{ j=1;
while(j<=i)
{ System.out.print(j);
++j;
} i++;
System.out.println();
}

3. Write a equivalent while loop for the following code:
intsz=25;
for(int i=0,sum=0;i<sz;i++)
sum+=i;
System.out.println(sum);
Ans: int sz=25;
 int i=0,sum=0;
while(i<sz)
{
sum+=i;
i++;
}
System.out.println(sum);

4. Rewrite the following if-else segment using switch-case statement
char ch='A';
if(ch=='A')
System.out.println("Account");
if((ch=='C') || (ch=='G'))
System.out.println("Admin");
if(ch=='F')
System.out.println("Advisor");
Ans: char ch='A';
switch(ch)
{
 case ‘A':
System.out.println("Account");
break;
case 'C':
case 'G’:
System.out.println("Admin");
break;
case 'F':
System.out.println("Advisor");
 }

5. Rewrite the following code using while loop:
inti,j;
for(i=1,j=2;i<=6;i++,j+=2)
System.out.println(i++);
System.out.println(“Finished!!!”);
Ans: inti=1,j=2;
 while(i<=6)
 {System.out.println(i++);
i++;
j+=2;}
System.out.println(“Finished!!!”);

6. Rewrite the following code using for loop.
int i=0;
while(++i<20)
{ if( i==8)
break;
System.out.println(i++);
}
Ans:
int i;
for(i=1;i<20;++i)
 { if( i==8)
break;
System.out.println(i++);
 }

7. Rewrite the code using switch statement:
If(k==1)
Day=”Monday”;
elseif(k==2)
Day=”Tuesday”;
elseif(k==3)
Day=”Wednesday”;
else
Day=”-”
Ans:
switch(k)
{
case 1: Day=”Monday”;
break;
case 2: Day=”Tuesday”;
break;
case 3: Day=”Wednesday”;
break;
default: Day=””;
}

8. Write the equivalent switch case for the following code :
If (num1 = =1 )
jTextField1.setText(“Number is one”);
else If (num1 = =2 )
jTextField1.setText(“Number is two”);
else If (num1 = =3 )
jTextField1.setText(“Number is three”);
else
jTextField1.setText(“Number is more than three”);
Ans:
Switch(num1)
{
Case 1 :
jTextField1.setText(“Number is one”);
break;
case 2 :
jTextField1.setText(“Number is two”);
break;
case 3 :
jTextField1.setText(“Number is three”);
break;
default:
jTextField1.setText(“Number is more than three”);
}                     

9. Given the following code fragment :
If(a==0)
System.out.println(“zero”);
If(a==1)
System.out.println(“one”);
If(a==2)
System.out.println(“two”);
If(a==3)
System.out.println(“three”);
Write an alternative code (Using if) that saves on number of comparsons
Ans:
if(a==0)
System.out.println(“zero”);
 else if(a==1)
System.out.println(“one”);
else if(a==2)
System.out.println(“two”);
else if(a==3)
System.out.println(“three”);

10. Rewrite the following code fragment using switch :
if(ch = = ‘E’)
east++;
if(ch = = ‘W’)
west++;
if(ch = = ‘N’)
north++;
if(ch = = ‘S’)
south++;
 else
jOptionPane.showMessageDialog(null, “unknown”);
Ans:
Switch(ch)
{
Case ‘E’:
east++;
break;
case ‘W’:
west++;
break;
case ‘N’:
north++;
break;
case ‘S’:
south++;
break;
default :
jOptionPane.showMessageDialog(null, “unknown”);
}

11 Rewrite the following code using for loop:
int i = 0;
while(++i <20)
 {
if(i = = 8)
break;
System.out.println(++i);
 }
Ans:
for(int i = 0;++i <20;)
 {
if(i = = 8)
break;
System.out.println(++i);
 }

12. Rewrite the following fragment using switch:
if ( ch == ‘E’)
eastern ++;
if ( ch == ‘W’)
western ++;
if (ch ==’N’)
northern++;
if (ch == ‘S’)
southern++;
else
unknown++;
Ans:
switch(ch) {
case ‘E’ : eastern ++; break;
case ‘W’ : western ++; break;
case ‘N’ : northern ++; break;
case ‘S’ : southern ++; break;
default : unknown++;}

13. Given the code fragment:
i = 2;
do { System.out.println(i);
i+=2;
}while(i<51);
jOptionPane.showMessageDialog(null, “Thank you”);
Rewrite the above code using a while loop.
Ans: i = 2;
while(i<51) { System.out.println(i);
i+=2;
}
jOptionPane.showMessageDialog(null, “Thank you”);

14 Rewrite following while loop into a for loop
int stripes = 0;
while ( stripes <=13) {
if ( stripes % 2 == 2)
{ System.out.println(“Colours code Red”);
}
else { System.out.println(“Colours code Blue”);
}
System.out.println(“New Stripe”);
stripes = stripes + 1;
}
Ans: for ( int stripes =0; stripes <=13; stripes++)
{ if stripes % 2 == 2)
{ System.out.println(“Colours code Red”);
}
else { System.out.println(“Colours code Blue”);
}
System.out.println(“New Stripe”);

}