Natural number

Natural numbers are numbers that are common and clearly in nature. As such, it is a whole, nonnegative number.

Logic

To print the first N natural number we only have to run one single loop from 1 to N.

After taking input (num) from user start one loop from 1 to num, and then inside the loop simply print the current variable “i”.

See also: Calculate sum of first N natural numbers

Program

import java.util.Scanner;

public class PrintNaturalNumbers {
    
    public static void main(String[] args) {

        Scanner scanner = new Scanner(System.in);

        // Take number from user
        System.out.print("Enter any number : ");
        int num = scanner.nextInt();

        System.out.println("\nNatural numbers from 1 to " + num);

        int i = 1;

        while (i <= num) {

            System.out.print(i + " ");
            i++;
        }

    }

}

Output

Enter any number : 10
Natural numbers from 1 to 10
1 2 3 4 5 6 7 8 9 10