Write a Java Program to Print Natural Numbers from 1 to N using For Loop, and While Loop with an example.
Java Program to Print Natural Numbers from 1 to N Example 1
This program allows the user to enter any integer value(the maximum limit value). Next, this Java program displays all the natural numbers from 1 to maximum limit value (N) using For Loop.
// Java Program to Print Natural Numbers from 1 to N import java.util.Scanner; public class NaturalNumbers1 { private static Scanner sc; public static void main(String[] args) { int number, i; sc = new Scanner(System.in); System.out.print(" Please Enter any Number : "); number = sc.nextInt(); for(i = 1; i <= number; i++) { System.out.print(i +"\t"); } } }

First, we used the Java For loop to iterate from 1 to maximum value (Here, number = 6).
User entered value: number = 6
For Loop First Iteration: for(i = 1; i <= 6; i++)
Condition is True. So, i Value printed (i.e., 1)
Second Iteration: for(i = 2; 2 <= 6; 2++)
Condition is True. It prints 2
Third Iteration: for(i = 3; 3 <= 6; 3++)
Condition is True. It prints 3
Fourth Iteration: for(i = 4; 4 <= 6; 4++)
Condition is True. 4 printed.
Fifth Iteration: for(i = 5; 5 <= 6; 5++)
Condition is True. It prints 5
Sixth Iteration: for(i = 6; 6 <= 6; 6++)
Condition is True. 6 printed
7th Iteration: for(i = 7; 7 <= 6; 7++)
Condition (7 <= 6) is False. So, the Java compiler exits from the For Loop
Java Program to Print Natural Numbers from 1 to N using While loop
This Java program to return natural numbers from 1 to N is the same as the above example, but we are using the While Loop.
// Java Program to Print Natural Numbers from 1 to N import java.util.Scanner; public class NaturalNumbers2 { private static Scanner sc; public static void main(String[] args) { int number, i = 1; sc = new Scanner(System.in); System.out.print(" Please Enter any Number : "); number = sc.nextInt(); while(i <= number) { System.out.print(i +"\t"); i++; } } }
Please Enter any Number : 10
1 2 3 4 5 6 7 8 9 10
Java Program to Print Natural Numbers from 1 to N using Method
This Java program for natural numbers is the same as the first example. But we separated the logic and placed it in a separate method.
// Java Program to Print Natural Numbers from 1 to N import java.util.Scanner; public class NaturalNumbers3 { private static Scanner sc; public static void main(String[] args) { int number; sc = new Scanner(System.in); System.out.print(" Please Enter any Number : "); number = sc.nextInt(); NaturalNumbers(number); } public static void NaturalNumbers(int num) { int i; for(i = 1; i <= num; i++) { System.out.print(i +" "); } } }
Java natural numbers from 1 to 25 output
Please Enter any Number : 25
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25
Java Program for Natural Numbers between a Range
This program allows entering Minimum and maximum value. Next, the Java program displays natural numbers between Minimum value and maximum value.
// Java Program to Print Natural Numbers from 1 to N import java.util.Scanner; public class NaturalNumbers4 { private static Scanner sc; public static void main(String[] args) { int minimum, maximum; sc = new Scanner(System.in); System.out.print(" Please Enter the Minimum value : "); minimum = sc.nextInt(); System.out.print(" Please Enter the Maximum value : "); maximum = sc.nextInt(); NaturalNumbers(minimum, maximum); } public static void NaturalNumbers(int min, int max) { int i; for(i = min; i <= max; i++) { System.out.print(i +" "); } } }
Please Enter the Minimum value : 5
Please Enter the Maximum value : 50
5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50
Please refer to
Comments are closed.