Write a Java-program to display 1 to n values using while loop

Write a Java-program to display 1 to n values using while loop

Write a Java-program to display 1 to n values using while loop

Program
public class Main
{
    public static void main(String args[])
    {
        int x=1,n=10;
        while(x<=n)
        {
            System.out.println(x);
            x++;
        }
    }
}	

Output:

1
2
3
4
5
6
7
8
9
10

Using CommandLine Arguments
public class Main
{
    public static void main(String args[])
    {
        int x,n;
        n=Integer.parseInt(args[0]);
        while(x<=n)
        {
            System.out.println(x);
            x++;
        }
    }
}	

Output:

1
2
3
4
5
6
7
8
9
10

Using Scanner Class
import java.util.*;
public class Main
{
    public static void main(String args[])
    {
        int x,n;
        Scanner s=new Scanner(System.in);
        System.out.println("Enter n value: ");
        n=s.nextInt();
        while(x<=n)
        {
            System.out.println(x);
            x++;
        }
    }
}	

Output:

Enter n value : 10
1
2
3
4
5
6
7
8
9
10





More Questions


59 . Write a Java-program to display 10 to 1 values using while loop
60 . Write a Java-program to display 1 to n values using while loop
61 . Write a Java-program to display display Even Numbers using while loop
62 . Write a Java-program to display Even Number or Odd Number based on the N value using while loop
63 . Write a Java-program to display Even Number or Odd Number based on the N value using single while loop
64 . Write a Java-program count the number of divisible numbers in a given range(1-100) using while loop ?
65 . Write a Java-program to print Factorial Number using while loop ?
66 . Write a Java-program to print Factorial of upto N Numbers using while loop ?
67 . Write a Java-program to print sum of natural numbers using while loop ?
68 . Write a Java-program to print sum of Even numbers using while loop ?
69 . Write a Java-program to print sum of Odd numbers using while loop ?


Java Quiz

Java Quiz - 13

Java Quiz - 22

Java Quiz - 17

Java Quiz - 3

Java Quiz - 2