To print first N natural numbers in Java

In this article, we will discuss how to print n natural numbers in Java

 

Printing first N natural numbers:

  1. Using for loop
  2. Using while loop
  3. Using do-while loop
  4. Using recursive function
  5. Using Java 8 Streams
  6. Printing in reverse order

 

Lets get started with coding part, to print first N natural numbers

Example 1: using for loop

NaturalNumbersUsingForLoop.java

package in.bench.resources.print.natural.numbers;

import java.util.Scanner;

public class NaturalNumbersUsingForLoop {

	public static void main(String[] args) {

		// instantiate Scanner class
		Scanner scanner = new Scanner(System.in);

		// get the limit value of n from user
		System.out.print("Enter limit of value n : ");
		int n = scanner.nextInt();

		System.out.println("\nFirst " + n + " Natural numbers using for-loop : ");
		for(int i=1; i<=n; i++) {
			System.out.println(i);
		}
		
		// finally close - its a good programming practice to wrap inside try-catch block
		scanner.close();
	}
}

Output:

Enter limit of value n : 7

First 7 Natural numbers using for-loop : 
1
2
3
4
5
6
7

 

Example 2: using while loop

NaturalNumbersUsingWhileLoop.java

package in.bench.resources.print.natural.numbers;

import java.util.Scanner;

public class NaturalNumbersUsingWhileLoop {

	public static void main(String[] args) {

		// local variable
		int i =1;

		// instantiate Scanner class
		Scanner scanner = new Scanner(System.in);

		// get the limit value of n from user
		System.out.print("Enter limit of value n : ");
		int n = scanner.nextInt();

		System.out.println("\nFirst " + n + " Natural numbers using while-loop : ");
		while(i<=n){
			System.out.println(i);
			i++;
		}

		// finally close - its a good programming practice to wrap inside try-catch block
		scanner.close();
	}
}

Output:

Enter limit of value n : 5

First 5 Natural numbers using while-loop : 
1
2
3
4
5

 

Example 3: using do-while loop

NaturalNumbersUsingDoWhileLoop.java

package in.bench.resources.print.natural.numbers;

import java.util.Scanner;

public class NaturalNumbersUsingDoWhileLoop {

	public static void main(String[] args) {

		// local variable
		int i =1;

		// instantiate Scanner class
		Scanner scanner = new Scanner(System.in);

		// get the limit value of n from user
		System.out.print("Enter limit of value n : ");
		int n = scanner.nextInt();

		System.out.println("\nFirst " + n + " Natural numbers using do-while loop : ");
		do {
			System.out.println(i);
			i++;
		} while(i<=n);

		// finally close - its a good programming practice to wrap inside try-catch block
		scanner.close();
	}
}

Output:

Enter limit of value n : 8

First 8 Natural numbers using do-while loop : 
1
2
3
4
5
6
7
8

 

Example 4: using recursive function

NaturalNumbersUsingRecursiveFunction.java

package in.bench.resources.print.natural.numbers;

import java.util.Scanner;

public class NaturalNumbersUsingRecursiveFunction {

	public static void main(String[] args) {

		// instantiate Scanner class
		Scanner scanner = new Scanner(System.in);

		// get the limit value of n from user
		System.out.print("Enter limit of value n : ");
		int n = scanner.nextInt();

		System.out.println("\nFirst " + n + " Natural numbers using Recursive function : ");
		NaturalNumbersUsingRecursiveFunction.naturalNumbering(n, 1);

		// finally close - its a good programming practice to wrap inside try-catch block
		scanner.close();
	}

	/**
	 * recursive function
	 * @param n
	 * @param i
	 * @return
	 */
	public static int naturalNumbering(int n, int i) {
		
		if(n >= i) {
			System.out.println(i);
			return(naturalNumbering(n, ++i));
		}
		return 1;
	}
}

Output:

Enter limit of value n : 10

First 10 Natural numbers using Recursive function : 
1
2
3
4
5
6
7
8
9
10

 

Example 5: using Java 8 Streams

NaturalNumbersUsingStreamInJava8.java

package in.bench.resources.print.natural.numbers;

import java.util.Scanner;
import java.util.stream.IntStream;

public class NaturalNumbersUsingStreamInJava8 {

	public static void main(String[] args) {

		// instantiate Scanner class
		Scanner scanner = new Scanner(System.in);

		// get the limit value of n from user
		System.out.print("Enter limit of value n : ");
		int n = scanner.nextInt();

		System.out.println("\nFirst " + n + " Natural numbers using Java 8 Stream : ");
		IntStream.rangeClosed(1, n).forEach(System.out::println);

		// finally close - its a good programming practice to wrap inside try-catch block
		scanner.close();
	}
}

Output:

Enter limit of value n : 7

First 7 Natural numbers using Java 8 Stream : 
1
2
3
4
5
6
7

 

Example 6: printing N natural number in reverse order

NaturalNumbersInReverseOrder.java

package in.bench.resources.print.natural.numbers;

import java.util.Scanner;

public class NaturalNumbersInReverseOrder {

	public static void main(String[] args) {

		// instantiate Scanner class
		Scanner scanner = new Scanner(System.in);

		// get the limit value of n from user
		System.out.print("Enter limit of value n : ");
		int n = scanner.nextInt();

		System.out.println("\nFirst " + n + " Natural numbers in reverse order : ");
		for(int i=n; i>=1; i--) {
			System.out.println(i);
		}

		// finally close - its a good programming practice to wrap inside try-catch block
		scanner.close();
	}
}

Output:

Enter limit of value n : 10

First 10 Natural numbers in reverse order : 
10
9
8
7
6
5
4
3
2
1

 

Hope, you found this article very helpful. If you have any suggestion or want to contribute any other way or tricky situation you faced during Interview hours, then share with us. We will include that code here.

 

Happy Coding !!
Happy Learning !!

3 types of for loop in Java
Java features version-wise