Open In App

Armstrong Numbers between two integers

Improve
Improve
Like Article
Like
Save
Share
Report

A positive integer with digits a, b, c, d… is called an Armstrong number of order n if following condition is satisfied. 
 

abcd... = an + bn + cn + dn +...

 

153 = 1*1*1 + 5*5*5 + 3*3*3  
    =  1 + 125 + 27
    =  153        
Therefore, 153 is an Armstrong number.

Examples: 
 

Input : 100 400
Output :153 370 371
Explanation : 100 and 400 are given 
two integers.(interval)
  153 = 1*1*1 + 5*5*5 + 3*3*3 
      = 1 + 125 + 27
      =  153  
  370 = 3*3*3 + 7*7*7 + 0
      = 27 + 343 
      = 370
  371 = 3*3*3 + 7*7*7 + 1*1*1
      = 27 + 343 +1
      = 371

 

The approach implemented below is simple. We traverse through all numbers in given range. For every number, we first count number of digits in it. Let the number of digits in current number be n. Them we find sum of n-th power of all digits. If sum is equal to i, we print the number. 
 

C++




// CPP program to find Armstrong numbers in a range
#include <bits/stdc++.h>
using namespace std;
 
// Prints Armstrong Numbers in given range
void findArmstrong(int low, int high)
{
    for (int i = low+1; i < high; ++i) {
 
        // number of digits calculation
        int x = i;
        int n = 0;
        while (x != 0) {
            x /= 10;
            ++n;
        }
 
        // compute sum of nth power of
        // its digits
        int pow_sum = 0;
        x = i;
        while (x != 0) {
            int digit = x % 10;
            pow_sum += pow(digit, n);
            x /= 10;
        }
 
        // checks if number i is equal to the
        // sum of nth power of its digits
        if (pow_sum == i)
            cout << i << " ";    
    }
}
 
// Driver code
int main()
{
    int num1 = 100;
    int num2 = 400;
    findArmstrong(num1, num2);
    cout << '\n';
    return 0;
}


Java




// JAVA program to find Armstrong
// numbers in a range
import java.io.*;
import java.math.*;
 
class GFG {
     
    // Prints Armstrong Numbers in given range
    static void findArmstrong(int low, int high)
    {
        for (int i = low + 1; i < high; ++i) {
      
            // number of digits calculation
            int x = i;
            int n = 0;
            while (x != 0) {
                x /= 10;
                ++n;
            }
      
            // compute sum of nth power of
            // its digits
            int pow_sum = 0;
            x = i;
            while (x != 0) {
                int digit = x % 10;
                pow_sum += Math.pow(digit, n);
                x /= 10;
            }
      
            // checks if number i is equal
            // to the sum of nth power of
            // its digits
            if (pow_sum == i)
                System.out.print(i + " ");    
        }
    }
      
    // Driver code
    public static void main(String args[])
    {
        int num1 = 100;
        int num2 = 400;
        findArmstrong(num1, num2);
        System.out.println();
    }
}
 
/*This code is contributed by Nikita Tiwari.*/


Python




# PYTHON program to find Armstrong
# numbers in a range
import math
 
# Prints Armstrong Numbers in given range
def findArmstrong(low, high) :
     
    for i in range(low + 1, high) :
         
        # number of digits calculation
        x = i
        n = 0
        while (x != 0) :
            x = x / 10
            n = n + 1
             
        # compute sum of nth power of
        pow_sum = 0
        x = i
        while (x != 0) :
            digit = x % 10
            pow_sum = pow_sum + math.pow(digit, n)
            x = x / 10
             
        # checks if number i is equal to
        # the sum of nth power of its digits
        if (pow_sum == i) :
            print(str(i) + " "),
 
# Driver code
num1 = 100
num2 = 400
findArmstrong(num1, num2)
print("")
 
# This code is contributed by Nikita Tiwari.


C#




// C# program to find Armstrong
// numbers in a range
using System;
 
class GFG {
 
    // Prints Armstrong Numbers in given range
    static void findArmstrong(int low, int high)
    {
        for (int i = low + 1; i < high; ++i) {
 
            // number of digits calculation
            int x = i;
            int n = 0;
            while (x != 0) {
                x /= 10;
                ++n;
            }
 
            // compute sum of nth power of
            // its digits
            int pow_sum = 0;
            x = i;
            while (x != 0) {
                int digit = x % 10;
                pow_sum += (int)Math.Pow(digit, n);
                x /= 10;
            }
 
            // checks if number i is equal
            // to the sum of nth power of
            // its digits
            if (pow_sum == i)
                Console.Write(i + " ");
        }
    }
 
    // Driver code
    public static void Main()
    {
        int num1 = 100;
        int num2 = 400;
        findArmstrong(num1, num2);
        Console.WriteLine();
    }
}
 
/*This code is contributed by vt_m.*/


PHP




<?php
// PHP program to find
// Armstrong numbers
// in a range
 
// Prints Armstrong
// Numbers in given range
function findArmstrong($low, $high)
{
    for ($i = $low + 1;
         $i < $high; ++$i)
    {
 
        // number of digits
        // calculation
        $x = $i;
        $n = 0;
        while ($x != 0)
        {
            $x = (int)($x / 10);
            ++$n;
        }
 
        // compute sum of nth
        // power of its digits
        $pow_sum = 0;
        $x = $i;
        while ($x != 0)
        {
            $digit = $x % 10;
            $pow_sum += (int)(pow($digit, $n));
            $x = (int)($x / 10);
        }
 
        // checks if number i is
        // equal to the sum of
        // nth power of its digits
        if ($pow_sum == $i)
            echo $i . " ";    
    }
}
 
// Driver code
$num1 = 100;
$num2 = 400;
findArmstrong($num1, $num2);
 
// This code is contributed by mits
?>


Javascript




<script>
    // Javascript program to find
// Armstrong numbers
// in a range
   
// Prints Armstrong
// Numbers in given range
function findArmstrong(low, high)
{
    for (let i = low + 1;
         i < high; ++i)
    {
   
        // number of digits
        // calculation
        let x = i;
        let n = 0;
        while (x != 0)
        {
            x = parseInt(x / 10);
            ++n;
        }
   
        // compute sum of nth
        // power of its digits
        let pow_sum = 0;
        x = i;
        while (x != 0)
        {
            let digit = x % 10;
            pow_sum += parseInt(Math.pow(digit, n));
            x = parseInt(x / 10);
        }
   
        // checks if number i is
        // equal to the sum of
        // nth power of its digits
        if (pow_sum == i)
            document.write(i + " ");     
    }
}
   
// Driver code
let num1 = 100;
let num2 = 400;
findArmstrong(num1, num2);
   
// This code is contributed by _saurabh_jaiswal
</script>


Output: 
 

153 370 371

Time Complexity: O(n*logn), n is the range 
Auxiliary Space: O(1), as we are not using any extra space.

 



Last Updated : 13 Jun, 2022
Like Article
Save Article
Previous
Next
Share your thoughts in the comments
Similar Reads