Here are some programs to practice.
1. Convert the following for loop into equivalent do while loop.
inta,b;
for(a=40,b=20;b>=15;b=b-5)
a=a+3;
2. What is the name of the following loop and write its function?
for(; ;)
3. Predict the output for the following code segment:
int a=0,i=4;
while(i<10)
{
a= ++ i – i*2;
}
System.out.print(“a=” +a +”i=” +i);
4Find the erors if any in the program:
int x=100, y=65;
do
{
s=s+x;
x=x+5;
}while(x<10);
System.out.print(s);
system.out.print(x+y);
Section B
1. Write a program to enter a number and display the factorial of that number.
Example : Factorial of 5 is 1x2x3x4x5
Use for loop.
2. Input a number and print it after reversing it. Use while loop.
Example :
Input : 1479
Output : 9741
3. Write a program to input a number and print the multiplication table of that number from 1 to 10.
Example: if the input is 4 then the output should be
1 x 4 =4
2 x 4 = 8
.
.
10 x 4 = 40
4. Write a program to enter a number and display the sum of the digits of the number. Use do – while.
Example :
Input : 346
Output : 13