Java program to Print all Natural numbers in given range

In Java, to print all natural numbers in a given range we can use loop concepts such as while loop or for loop. For a given input range, we have to print every number in that range. For example, starting number is 1 and ending number is 5 as range, then output will be 1,2,3,4 and 5. Here input value is inclusive.

In this article, we are learning through the example, detailed logic and program explanation for better understanding.

Required Knowledge

Problem Statement

We have to write a java program to print all the natural numbers in a given range. The program should take two input values as numbers, one input value is the first/starting number and the another input value is the last number, and should print all the numbers in that range. This action will be done in our program.

Let’s see an example for better understanding,

Example:

For input numbers 1 and 6, output should be 1 2 3 4 5 6.

Input integer numbers: 1 and 6

Output: 1, 2, 3, 4, 5, 6

For the given input range(1,6), Output is all the positive numbers (1 2 3 4 5 6) in that range.

Logic to print Natural numbers in a given range

  • Natural numbers are all the positive numbers from 1 to n.
  • We should take two numbers as the range. Using loops (while or for loop) we have to check our first number is less than or equal to last number (nth number). if condition satisfy, print the natural numbers.
  • Following this approach will print the all natural numbers in the given range.

Java Program to print Natural numbers in a given range

Program 1: Using While loop

In the below program we are using while loop to print Natural numbers in a given range. Here is given range is inclusive.

import java.util.Scanner;
public class NaturalNumbers {
	public static void main(String[] args) {
		Scanner sc = new Scanner(System.in);
		System.out.println("Please enter the first number");
		int num = sc.nextInt();
		System.out.println("Please enter nth number");
		int n = sc.nextInt();
		System.out.println("Natural numbers in given range: ");
		while(num<=n) {
			System.out.println(num);
		  num++;
		}
    sc.close();
	}
}

Output:

Please enter the first number
1
Please enter nth number
4
Natural numbers in given range:
1
2
3
4

Java program explanation to print all natural numbers in a given range

  • This Java program ask the user to input two integers: one representing the starting natural number (num) and the other representing the ending natural number (n).
  • It then prints all the natural numbers from num to n, inclusive.
  • It uses a Scanner object to read user input from the console.
  • Inside a while loop, it prints each number from num to n.
  • After printing all the numbers, it closes the Scanner object to release system resources.

Program 2: Using for loop

In the below program we are using for loop to print Natural numbers in a given range

import java.util.Scanner;
public class NaturalNumbers {
	public static void main(String[] args) {
		Scanner sc = new Scanner(System.in);
		System.out.println("Please enter the first number");
		int num = sc.nextInt();
		System.out.println("Please enter nth number");
		int n = sc.nextInt();
		System.out.println("Natural numbers in given range: ");
		for (int number = num; number <= n; number++) {
			System.out.println(number);
		}
    sc.close();
	}
}

Output:

Please enter the first number
2
Please enter nth number
4
Natural numbers in given range:
2
3
4

Java program explanation to print all natural numbers in a given range

  • This Java program ask the user to input two integers: one representing the starting natural number (num) and the other representing the ending natural number (n).
  • It then iterates through a range of numbers from num to n using a for loop.
  • Within the loop, it prints each number from num to n.
  • After printing all the numbers, it closes the Scanner object to release system resources.

Conclusion

In this article, we have learnt how to print natural numbers in a given range in Java. We have understood the logic and implemented the program. Hope this article helped you in understanding the logic and program.

You can also check our another awesome tutorials

  1. Java program to print sum of prime numbers in given range
  2. Palindrome number program in Java
  3. Java program to print sum of digits of a number in Java
  4. Java program to swap two values without using third variable

Happy coding !!

How useful was this post?

Click on a star to rate it!

Average rating 0 / 5. Vote count: 0

No votes so far! Be the first to rate this post.