# Armstrong Numbers between Two Intervals using 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

## 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;

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```

### Related Banners

Get PrepInsta Prime & get Access to all 200+ courses offered by PrepInsta in One Subscription

### 9 comments on “Armstrong Numbers between Two Intervals using Java”

• Pulivendula

public class Main
{
public static void main(String[] args)
{
int c=0;
int s=0;

for(int i=1; i<=1000; i++)
{
if(amstrong(i))
{
System.out.print(i+" ");
c++;
s=s+i;
}
}
System.out.println();
System.out.println("Sum :"+s);
System.out.println("Count :"+c);
System.out.println("Avg :"+s/c);
}
public static boolean amstrong(int n)
{
int s=0;
int n1=n;
while(n!=0)
{
int d=n%10;
s=s+(d*d*d);
n=n/10;
}
return n1==s;
}

}

• Sornammal

public class Main
{
public static void main(String[] args) {
//Armstrong number in a given range :
int start=1;
int end =500;

for(int i=start;i<=end;i++)
{
if(amstrong(i))
{
System.out.println(i);
}
}
}
static boolean amstrong(int n)
{
int original=n;
int sum=0;
int digit=digits(n);
while(n!=0)
{
int rem=n%10;
sum += (int) Math.pow(rem,digit);
n=n/10;
}
return sum==original;
}
static int digits(int n)
{
int count =0;
while (n!=0)
{
count++;
n=n/10;
}
return count;
}
}

• Sumit

import java.util.Scanner;
public class ArmstrongBetweenTwoNumbers {
public static void main(String args[]){
int num1, num2;
Scanner sc = new Scanner(System.in);
System.out.println(“Enter the first number ::”);
num1 = sc.nextInt();
System.out.println(“Enter the second number ::”);
num2 = sc.nextInt();

for (int i = num1; i<num2; i++){
int check, rem, sum = 0;
check = i;
while(check != 0) {
rem = check % 10;
sum = sum + (rem * rem * rem);
check = check / 10;
}
if(sum == i){
System.out.println(""+i+" is an Armstrong number.");
}
}
}

• 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);
}