Java program to print all the natural numbers from 1 to N can be written in both iterative and recursive methods. In the iterative method, the task can be performed by using for loop, while loop and do-while loop. Here, all the Java programs using different loops have been covered to print the natural numbers from 1 to N (1, 2, 3, ..., N).

#### Page content(s):

1. Program & Output

1. Algorithm

2. Pseudocode

3. Time Complexity

4. Flowchart

## 1. Java Program & output to Print First N Natural Numbers

Code has been copied
/**************************************************
alphabetacoder.com
Java program to print all natual numbers
from 1 to n using for loop
**************************************************/

import java.util.Scanner;
public class PrintNumbers {
public static void main(String args[]) {
//System.in is a standard input stream
// sc is the object
Scanner sc = new Scanner(System.in);
int n, i;

//take input of the number upto
// which numbers will be printed
System.out.print("Enter the upper limit=");
n = sc.nextInt();

// iterate from 1 to n and print the number
System.out.print("First " + n + " natural numbers are : ");
for (i = 1; i <= n; i++) {
System.out.print(i + " ");
}
}
}


Output

#### Case 1:

Enter the upper limit=10

First 10 natural numbers are : 1 2 3 4 5 6 7 8 9 10

#### Case 2:

Enter the upper limit=5

First 5 natural numbers are : 1 2 3 4 5

/**************************************************
alphabetacoder.com
Java program to print all natual numbers
from 1 to n using while loop
**************************************************/

import java.util.Scanner;
public class PrintNumbers {
public static void main(String args[]) {
//System.in is a standard input stream
// sc is the object
Scanner sc = new Scanner(System.in);
int n, i;

//take input of the number upto
// which numbers will be printed
System.out.print("Enter the upper limit=");
n = sc.nextInt();

// iterate from 1 to n and print the number
System.out.print("First " + n + " natural numbers are : ");
i = 1;
while (i <= n) {
System.out.print(i + " ");
i++;
}
}
}


Output

#### Case 1:

Enter the upper limit=10

First 10 natural numbers are : 1 2 3 4 5 6 7 8 9 10

#### Case 2:

Enter the upper limit=5

First 5 natural numbers are : 1 2 3 4 5

/**************************************************
alphabetacoder.com
Java program to print all natual numbers
from 1 to n using do-while loop
**************************************************/

import java.util.Scanner;
public class PrintNumbers {
public static void main(String args[]) {
//System.in is a standard input stream
// sc is the object
Scanner sc = new Scanner(System.in);
int n, i;

//take input of the number upto
// which numbers will be printed
System.out.print("Enter the upper limit=");
n = sc.nextInt();

// iterate from 1 to n and print the number
System.out.print("First " + n + " natural numbers are : ");
i = 1;
do {
System.out.print(i + " ");
i++;
} while (i <= n);
}
}


Output

#### Case 1:

Enter the upper limit=10

First 10 natural numbers are : 1 2 3 4 5 6 7 8 9 10

#### Case 2:

Enter the upper limit=5

First 5 natural numbers are : 1 2 3 4 5