Table of Contents
Java program to display natural numbers from 1 to n
In this tutorial, we will discuss the Java program to display natural numbers from 1 to n through different 5 ways

In this post, we are going to learn how to print natural number from 1 to entered number in different 5 ways
Program 1
Java code to display natural numbers Using for loop
This program allows the user to enter a maximum number and it displays the natural numbers from 1 to given number using for loop in Java language
//Java program to print an integer
import java.util.Scanner;
class PrintNaturalNumbers{
public static void main(String args[]){
Scanner scan=new Scanner(System.in);
//create a scanner instance for receives input
// from the user - input from keyboard
System.out.print("Enter the maximum number for num: ");
int num=scan.nextInt();
//reads the maximum value from the user
//input from keyword
System.out.print("First "+num+" Natural numbers are\n");
for(int i=1; i<=num; i++){
System.out.print(i+" ");
}
}
}
When the above code is executed it produces the following output
Enter the maximum number for num: 25 First 25 natural numbers are 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
Program 2
Java code to display natural numbers Using while loop
This program allows the user to enter a maximum number. and it displays natural numbers from 1 to given number using while loop in Java language
//Java program to print an integer
import java.util.Scanner;
class PrintNaturalNumbers1{
public static void main(String args[]){
Scanner scan=new Scanner(System.in);
//create a scanner instance for receives input
// from the user - input from keyboard
System.out.print("Enter the maximum number for num: ");
int num=scan.nextInt();
//reads the maximum value from the user
//input from keyword
System.out.print("First "+num+" Natural numbers are\n");
int i=1;
while( i<=num){
System.out.print(i+" ");
i++;
}
}
}
When the above code is executed it produces the following output
Enter the maximum number for num: 30 first 30 Natural numbers are 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 2 27 28 29 30
Program 2
Java code to display natural numbers Using do-while loop
This program allows the user to enter a maximum number. and it displays natural numbers from 1 to given number using do-while loop.in Java
//Java program to print an integer
import java.util.Scanner;
class PrintNaturalNumbers2{
public static void main(String args[]){
Scanner scan=new Scanner(System.in);
//create a scanner instance for receives input
// from the user - input from keyboard
System.out.print("Enter the maximum number for num: ");
int num=scan.nextInt();
//reads the maximum value from the user
//input from keyword
System.out.print("First "+num+" Natural numbers are\n");
int i=1;
do{
System.out.print(i+" ");
i++;
}while(i<=num);
}
}
When the above code is executed it produces the following output
Enter the maximum number for num: 15 First 15 Natural numbers are 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
Program 4
Java code to print natural numbers Using the method
This program allows the user to enter a maximum number. and it displays natural numbers from 1 to given number using user defined method in Java
import java.util.Scanner;
class PrintNaturalNumbers3{
public static void main(String args[]){
Scanner scan=new Scanner(System.in);
//create a scanner instance for receives input
// from the user - input from keyboard
System.out.print("Enter the maximum number for num: ");
int num=scan.nextInt();
//reads the maximum value from the user
//input from keyword
NaturamNum(num);//Call the method
}
static void NaturamNum(int num){//user define method or method definition
System.out.print("First "+num+" Natural numbers are: \n");
for(int i=1; i<=num; i++){
System.out.print(i+" ");
}
}
}
When the above code is executed it produces the following output
Enter the maximum number for num: 25 First 25 natural numbers are: 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 1 17 18 19 20 21 22 23 24 25
Java code to print natural numbers Using the recursion
Program 5
This program allows the user to enter a maximum number. and it displays natural numbers from 1 to given number using recursion in Java
import java.util.Scanner;
public class NaturalNumbers5{
public static void main(String args[]){
Scanner scan=new Scanner(System.in);
//create a scanner instance for receives input
// from the user - input from keyboard
System.out.print("Enter the maximum number for num: ");
int num=scan.nextInt();
//reads the maximum value from the user
//input from keyword
naturalNumbers5 obj=new naturalNumbers5();
//object for class
System.out.print("Natural numbers i to "+num+": ")'
obj.naturamNum(num,1);
}
naturamNum(int x, int y){//recursive method definition
if(y<=x){
System.out.print(y+" ");
return(natural(y,++1));
}
}
}
When the above code is executed it produces the following output
Enter the maximum number for num: 20 First 20 natural numbers are: 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 1 17 18 19 20
Similar post
C++ program to display natural numbers from 1 to n
C program to display natural numbers from 1 to n
Python program to display natural numbers from 1 to n
Suggested for you
Do-while loop in Java language
Data type and variable in java language