Friday, August 26, 2011

While loop example in java


import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;

public class WhileExample
{
public static void main(String args[]) throws IOException
{

int n,i=1;
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
//initialize
System.out.println("Enter number of lines you want to display?");
n=Integer.valueOf(br.readLine()).intValue();
//condition
while(i<=n)
{
for(int j=0;j {
System.out.print(i);
System.out.print(" ");
}
System.out.println("\n");
//increment
i++;
}
}
}


Output =>

Enter number of lines you want to display?
10
1

2 2

3 3 3

4 4 4 4

5 5 5 5 5

6 6 6 6 6 6

7 7 7 7 7 7 7

8 8 8 8 8 8 8 8

9 9 9 9 9 9 9 9 9

10 10 10 10 10 10 10 10 10 10

No comments:

Post a Comment