Armstrong Numbers between Two Intervals using Java

Armstrong numbers between two intervals IN JAVA

Find the Armstrong Numbers in a given Interval in Java

Given two integers high and low for limits as inputs, the objective is to write a code to Find the Armstrong Numbers in a given Interval in Java.

Example 
Input : 10 1000
Output : 153  370  371  407

Find the Armstrong Numbers in a given Range using Java

Given two integer inputs as high and low, the objective is to find all the Armstrong Numbers that falls under the limits [Low,High] interval. Before Going into the Explanation let’s understand the problem and the definition of Armstrong Numbers.

From the above explanation we gather that to check if a number is it must be represented in the above stated equation. We then check

Java Code

Method 1

This is a simple iterative method to find all Armstrong numbers between two ranges –
Run
//Java program to print armstrong numbers between two intervals
import java.util.Scanner;

public class LearnCoding2
{
    public static void main(String[] args)
    {
        Scanner sc = new Scanner(System.in);
        System.out.print("Enter lower and upper ranges : ");
        int low = sc.nextInt();
        int high = sc.nextInt();

        System.out.println("Armstrong numbers between "+ low + " and " + high + " are : ");

        // loop for finding and printing all armstrong numbers between given range
        for(int num = low ; num <= high ; num++)
        {
            int sum = 0, temp, digit, len;

            len = getOrder(num);
            temp = num;
            // loop to extract digit, find ordered power of digits & add to sum
            while(temp != 0)
            {
                // extract digit
                digit = temp % 10;

                // add power to sum
                sum = sum + (int) Math.pow(digit,len);
                temp /= 10;
            };

            if(sum == num)
                System.out.print(num + " ");
        }

    }

    private static int getOrder(int num) {
            int len = 0;
            while (num!=0)
            {
                len++;
                num = num/10;
            }
            return len;
    }
}

Output

Enter lower and upper ranges : 1 1000
Armstrong numbers between 1 and 1000 are : 
1 2 3 4 5 6 7 8 9 153 370 371 407 

Working

Working of the above code is given below –

  1. Ask the user to enter two ranges, one starting range and the second ending range.
  2. Use a loop to find all Armstrong numbers between starting range and ending range.
  3. Use the logic for checking number is Armstrong or not for every number between the given range and then display the result.

Method 2 (Recursive)

This method uses a recursive approach to find Armstrong sum. You can check recursion in Java here
Run
import java.util.Scanner;

public class LearnCoding2
{
    public static void main(String[] args)
    {
        Scanner sc = new Scanner(System.in);
        System.out.print("Enter lower and upper ranges : ");
        int low = sc.nextInt();
        int high = sc.nextInt();

        System.out.println("Armstrong numbers between "+ low + " and " + high + " are : ");

        // loop for finding and printing all armstrong numbers between given range
        for(int num = low ; num <= high ; num++)
        {
            int len = getOrder(num);

            if(num == getArmstrongSum(num, len))
                System.out.print(num + " ");
        }

    }

    private static int getOrder(int num) {
            int len = 0;
            while (num!=0)
            {
                len++;
                num = num/10;
            }
            return len;
    }
    private static int getArmstrongSum(int num, int order) {
        if(num == 0)
            return 0;

        int digit = num % 10;

        return (int) Math.pow(digit, order) + getArmstrongSum(num/10, order);
    }
}

Output

Enter lower and upper ranges : 1 1000
Armstrong numbers between 1 and 1000 are : 
1 2 3 4 5 6 7 8 9 153 370 371 407

3 comments on “Armstrong Numbers between Two Intervals using Java”


  • Satendra

    Class sd{public static void main (String []args){
    int n1,n2,last,total,n,rem;
    System.out.println(” enter a first no”);
    Scanner s= new Scanner (System.in);
    n1=s.nextInt();
    System.out.println(” enter a second no”);
    n2= s.nextInt();
    for(int i= n1; i0){
    rem=n%10;
    last=last+(rem*rem*rem);
    n=n/10;
    }
    if(total==last){
    System.out.println(last);
    }}}


  • Gautam

    package ppjava;
    import java.util.*;
    public class ArmSeries {
    public static void main(String[] args) {
    Scanner sc = new Scanner(System.in);
    System.out.println(“Enter start no: “);
    int s = sc.nextInt();
    System.out.println(“Enter second no: “);
    int l = sc.nextInt();
    int r=0;
    for(;s0) {
    int d= a%10;
    r= r+(d*d*d);
    a=a/10;
    }
    if(r==s) {
    System.out.println(s);
    }
    }
    sc.close();

    }
    }


  • rachit sharma

    for(int i = start ; i <= end ; i++)
    {
    n = i;
    int length=String.valueOf(i).length();

    sum = 0;
    //logic for checking number is armstrong or not
    while(n != 0)
    {
    int pick_last = n % 10;
    sum = sum + Math.pow(pick_last, length);
    n = n / 10;
    }
    if(sum == i)
    System.out.println(i);
    }