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

Multiply two numbers in Java using scanner| 5 different ways

Multiply two numbers in Java using scanner| 5 different ways In this article, we will…

3 months ago

5 different ways to Divide two numbers in Java using scanner

5 Different ways to Divide two numbers in Java using scanner In this article, we…

3 months ago

Learn 8 Ways to Subtract Two Numbers Using Methods in Java

Learn 8 Ways to Subtract Two Numbers Using Methods in Java In this article, we…

4 months ago

10 ways to subtract two numbers in Java

10 ways to subtract two numbers in Java In this article, we will discuss the…

4 months ago

Java Code Examples – Multiply Two Numbers in 5 Easy Ways

Java Code Examples – Multiply Two Numbers in 5 Easy Ways In this article, we…

4 months ago

How to Divide two numbers in Java| 5 different ways

How to Divide two numbers in Java| 5 different ways In this article, we will…

4 months ago

This website uses cookies.