Do-while loop

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

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

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

 

Karmehavannan

Recent Posts

Subtract two numbers using method overriding

Subtract two numbers using method overriding   Program 1

3 months ago

PHP Star triangle Pattern program

PHP Star triangle Pattern program Here's a simple Java program that demonstrates how to print…

3 months ago

Using function or method to Write temperature conversion : Fahrenheit into Celsius

Using Function or Method to Write to temperature conversion: Fahrenheit into Celsius In this article,…

1 year ago

Function or method:temperature conversion from Fahrenheit into Celsius – Entered by user

Function or method of temperature conversion from Fahrenheit into Celsius In this article, we will…

1 year ago

Write temperature conversion program: Fahrenheit into Celsius

Write temperature conversion program: from Fahrenheit to Celsius In this article, we will discuss the…

1 year ago

How to write a program to convert Fahrenheit into Celsius

How to write a program to convert Fahrenheit into Celsius In this article, we will…

1 year ago

This website uses cookies.