Skip to content
Menu
Code for Java c
  • Home
  • Java
    • Java Examples
    • Java tutorials
  • C
    • C tutorials
    • C Examples
  • C++
    • C++ Tutorials
    • C++ Examples
  • Python
    • Python Tutorials
    • Python Examples
  • About
    • About me
    • contact us
    • disclaimer
    • Privacy Policy
Code for Java c

Java program to display natural numbers from 1 to n – in 5 ways

Posted on October 5, 2019October 6, 2019

Table of Contents

  • Java program to display natural numbers from 1 to n
    • Java code  to display natural numbers Using for loop
    • Java code to display natural numbers Using while loop
    • Java code to display natural numbers Using do-while loop
    • Java code to print natural numbers Using the method
    • Java code to print  natural numbers Using the recursion

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

Java program to display natural numbers from 1 to n
Natural numbers

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

For loop in Java language

While loop in Java language

Do-while loop in Java language

Data type and variable in java language

Operator in Java language

 

Related

Recent Posts

  • Subtract two numbers using method overriding
  • PHP Star triangle Pattern program
  • Using function or method to Write temperature conversion : Fahrenheit into Celsius
  • Function or method:temperature conversion from Fahrenheit into Celsius – Entered by user
  • Write temperature conversion program: Fahrenheit into Celsius
  • How to write a program to convert Fahrenheit into Celsius

tag

Addition (6) Array (38) C++ language (91) C language (98) c sharp (23) Division (6) Function (29) if else (32) Java language (102) JavaScript (5) loops (137) Multiply (7) Oop (2) patterns (65) PHP (13) Python Language (38) Subtraction (7) temperature (20)

Archives

Categories

Address

Global information technology

Puloly south, PointPedro

Jaffna

Srilanka

©2025 Code for Java c | Powered by SuperbThemes