Looping
Looping
Page | 22
1. What is Looping Statement?
In real life we come across situations when we need to perform a set of task repeatedly till
some condition is met.
Such as – sending email to all employees, deleting all files and printing 1000 pages of a
document. All of these tasks is handled by looping statements.
Looping is the Programming concept to perform the given jobs/task various times then this
concept is called looping Statement.
While Loop:
class Loop
{
public static void main(String args[])
{
int i=1;
while(i<=10)
{
System.out.println("Dileep");
i++;
}
}
}
do - while Loop:
class Loop
{
public static void main(String args[])
{
int i=1;
do
{
System.out.println("Dileep");
i++;
}
while(i<=10);
}
}
2. Write a program in Java to print natural numbers from 1 to 10.
class Loop
{
public static void main(String args[])
{ Page | 25
int i;
for(i=1;i<=10;i++)
{
System.out.println(i);
}
}
}
int n= Integer.parseInt(in.readLine());
for(int i=1;i<=10;i++)
{
int t=n*i;
System.out.println(n +"*"+ i +"=" +t);
}
}
}
5. WAP in java to find factorial of a number.
import java.io.*;
class Factorial
{
public static void main(String args[]) throws IOException Page | 26
{
int n,i, f=1;
BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
n= Integer.parseInt(in.readLine());
for(i=1;i<=n;i++)
{
f=f*i;
}
System.out.println("factorial is="+f);
}
}