Java Program to Print Numbers From 1 to N Using While Loop

You are currently viewing Java Program to Print Numbers From 1 to N Using While Loop

Java program to print numbers from 1 to N using while loop

import java.util.Scanner;

public class Print_1_To_N_UsingWhile 
{
	public static void main(String[] args)
	{
		//loop counter initialisation
		int i =1;

		//create object of scanner class
		Scanner Sc = new Scanner(System.in);

		// enter the value of " n "
		System.out.print("Enter the value n : ");

		// read the value.
		int n = Sc.nextInt();

		System.out.println("Numbers are : ");

		while(i<=n)
		{
			System.out.println(i);
			i++;
		}
	}
}

Output

Enter the value n : 15
Numbers are : 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15

Ranjith

Hi, I'm Manoj a full-time Blogger, YouTuber, Affiliate Marketer, & founder of Coding Diksha. Here, I post about programming to help developers.

Leave a Reply