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

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

Program
public class Main
{
    public static void main(String args[])
    {
        int x,n=10;
        for(x=1; x<=n; x++)
        {
            System.out.println(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]);    
        for(x=1; x<=n; x++)
        {
            System.out.println(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();
        for(x=1; x<=n; x++)
        {
            System.out.println(x);
        }
    }
}	

Output:

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





More Questions


43 . Write a Java-program to display 10 to 1 values using for loop
44 . Write a Java-program to display 1 to n values using for loop
45 . Write a Java-program to display display Even Numbers using for loop
46 . Write a Java-program to display Even Number or Odd Number based on the N value using for loop
47 . Write a Java-program to display Even Number or Odd Number based on the N value using single for loop
48 . Write a Java-program count the number of divisible numbers in a given range(1-100) ?
49 . Write a Java-program count the number of divisible numbers in a given range(1-100) without loop
50 . Write a Java-program to print Factorial Number using for loop ?
51 . Write a Java-program to print Factorial of upto N Numbers using for loop ?
52 . Write a Java-program to print sum of natural numbers using for loop ?
53 . Write a Java-program to print sum of Even numbers using for loop ?