Java Program to Print All Numbers in a Range

Print all numbers in a range

Java programs to print all the numbers in a range have been shown here. Both of the iterative and recursive approaches have been covered.






1. Algorithm to Print All numbers in a range


1. Take input of two numbers n1, n2 as range limit.

2. Set another variable i=n1

3. Iterate i from n1 to n2 by incrementing 1

4. Print value of i in each iteration




2. Pseudocode to Print All numbers in a range


Input : Two integer numbers $n_1, n_2$

Output : All the integer numbers from $n_1$ to $n_2$

1. Procedure printAll($n_1$, $n_2$):

2. $i\leftarrow n_1$

3. Repeat until $i > n_2$

4. Print $i$

5. $i \leftarrow i+1$

6. End Procedure





3. Time complexity to Print All numbers in a range


Time Complexity: O(n)

Here, $n$ is the no. of terms in the given range.





4. Java Program & Output to Print All Integer numbers in a range using Iteration

Code has been copied
/*********************************************
            alphabetacoder.com
 Java program to print all integer numbers in 
 a given range using iterative approach
**********************************************/

import java.util.Scanner;

class Main {
    public static void main(String[] args) {
        // declare instance of Scanner class
        Scanner sc = new Scanner(System.in);

        // declare variables
        int n1, n2, i;

        // take input of the inteval
        System.out.print("Enter the lower limit = ");
        n1 = sc.nextInt();
        System.out.print("Enter the upper limit = ");
        n2 = sc.nextInt();

        // iterate from n1 to n2 and print the numbers
        System.out.print("Numbers in the range: ");
        for (i = n1; i <= n2; i++)
            System.out.print(i + " ");
    }
}

Output


Enter the lower limit = 20

Enter the upper limit = 40

Numbers in the range: 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40




5. Java Program & Output to Print All Integer numbers in a range using Recursion

Code has been copied
/**********************************************
              alphabetacoder.com
 Java program to print all integer numbers in
 a given range using a recursive approach
***********************************************/

import java.util.Scanner;

class Main {
    // recursive function
    public void printNumbers(int n1, int n2) {
        //condition for calling
        if (n2 > n1)
            printNumbers(n1, n2 - 1);
        // print current number
        System.out.print(n2 + " ");
    }

    public static void main(String args[]) {
        // declare instance of Scanner class
        Scanner sc = new Scanner(System.in);

        // declare object of class Main
        Main obj = new Main();

        // declare variables
        int n1, n2;

        // take input of the inteval
        System.out.print("Enter the lower limit = ");
        n1 = sc.nextInt();
        System.out.print("Enter the upper limit = ");
        n2 = sc.nextInt();

        System.out.print("Numbers in the range: ");
        // call the recursive function to print
        // all the integer numbers in a range
        obj.printNumbers(n1, n2);
    }
}

Output


Enter the lower limit = -5

Enter the upper limit = 5

Numbers in the range: -5 -4 -3 -2 -1 0 1 2 3 4 5




6. Java Program & Output to Print All Float numbers with specific interval in a range using Iteration

Code has been copied
/********************************************
            alphabetacoder.com
 Java program to print all float numbers with
 specific interval in a given range
*********************************************/

import java.util.Scanner;

class Main {
    public static void main(String args[]) {
        // declare instance of Scanner class
        Scanner sc = new Scanner(System.in);

        // declare variables
        double n1, n2, in, i;

        // take input of the inteval
        System.out.print("Enter the lower limit = ");
        n1 = sc.nextInt();
        System.out.print("Enter the upper limit = ");
        n2 = sc.nextInt();
        System.out.print("Enter the value of interval = ");
        in = sc.nextDouble();

        System.out.print("Numbers in the range: ");
        // dislay all numbers between n1 and n2
        // with given interval
        for (i = n1; i <= n2; i += in)
            System.out.print(i + " ");
    }
}

Output


Enter the lower limit = 50

Enter the upper limit = 55

Enter the value of interval = 0.25

Numbers in the range: 50.0 50.25 50.5 50.75 51.0 51.25 51.5 51.75 52.0 52.25 52.5 52.75 53.0 53.25 53.5 53.75 54.0 54.25 54.5 54.75 55.0




7. Java Program & Output to Print All Float numbers with specific interval in a range using Recursion

Code has been copied
/*****************************************************
            	alphabetacoder.com
 Java program to print all float numbers with specific
 interval in a given range using recursive approach
******************************************************/

import java.util.Scanner;

class Main {
    // recursive function
    public void printNumbers(double n1, double n2, double in ) {
        //condition for calling
        if (n2 >= n1) {
            // print current number
            System.out.print(n1 + " ");
            // call recursive function
            printNumbers(n1 + in, n2, in);
        }
    }

    public static void main(String args[]) {
        // declare instance of Scanner class
        Scanner sc = new Scanner(System.in);

        // declare object of class Main
        Main obj = new Main();

        // declare variables
        double n1, n2, in;

        // take input of the inteval
        System.out.print("Enter the lower limit = ");
        n1 = sc.nextDouble();
        System.out.print("Enter the upper limit = ");
        n2 = sc.nextDouble();
        System.out.print("Enter the value of interval = ");
        in = sc.nextDouble();

        System.out.print("Numbers in the range: ");
        // call the recursive function to print
        // all the float numbers in input range
        obj.printNumbers(n1, n2, in);
    }
}

Output


Enter the lower limit = 1

Enter the upper limit = 10

Enter the value of interval = 0.625

Numbers in the range: 1.0 1.625 2.25 2.875 3.5 4.125 4.75 5.375 6.0 6.625 7.25 7.875 8.5 9.125 9.75