Revision_loops
Revision_loops
Worksheet-3
I. Analyze how many times the following loops gets executed and write
the output:
a)int x= 564;
for (; x>0; x/=10)
System.out.println (x%10);
b)int x=15;
while(x>5)
{
System.out.println(x++);
++x;
}
c) int m=2,n=15;
for(int i=1;i<5;i++)
{
m++;
--n;
}
System.out.println ("m="+m);
System.out.println ("n="+n);
d) int m;
for(m=10; m>0 & m<=30; m+=10);
System.out.println ("m="+m);
II.Rewrite as directed:-
a) Write using while loop
for (int a=10, b=20; a<=50; a+=5)
{
System.out.print(a);
System.out.println (b);
}
Programs:-
1) Accept your age and display your name no of times based on the
age.
2)Write a program to accept two numbers and perform the following
based on the user’s choice.
Choice 1:-To display the HCF of the numbers input by the user.
Choice 2:-To display count of common factors of the numbers input
by the user.
Display an appropriate message if the choice is not valid.
***