# Armstrong Number Program in C++

Let's first define Armstrong number before writing the C++ program to check whether the number is Armstrong or not. The sum of the cubes of its digits is equal to the Armstrong number. Armstrong numbers include 0, 153, 1, 371, 407 and 270, for example.

### Let's see if we can figure out why 407 is an Armstrong number:

``````407 = ( 4 * 4 * 4 ) + 0 + ( 7 * 7 * 7 )
Where ;
4 * 4 * 4 = 64
0 = 0 ;
7 * 7 * 7 = 343
64 + 343 = 407
``````

### Let us look at an example of a program in C++ to find Armstrong number:

``````// C++ program to determine whether the number is
// Armstrong number or not
#include < bits/stdc++.h >
#include < iostream >
#include < stdlib >
using namespace std ;
/* Function to calculate x raised to the power y */
int power ( int a, unsigned int b )
{
if (b == 0)
return 1 ;
if ( b % 2 == 0 )
return power ( a , b / 2 ) * power( a , b / 2 ) ;
return a * power ( a , b / 2 ) * power ( a , b / 2 ) ;
}
/* Function to calculate order of the number */
int order ( int a )
{
int i = 0 ;
while ( a ) {
i++ ;
a = a / 10 ;
}
return i ;
}
// Function to check whether the given number is
// Armstrong number or not
bool isArmstrong ( int a )
{
// Calling order function
int i = order ( a ) ;
int temp = a , sum = 0 ;
while ( temp ) {
int y = temp % 10 ;
sum += power ( y , i ) ;
temp = temp / 10 ;
}
// If satisfies Armstrong condition
return ( sum == a ) ;
}
// Driver Program
int main ( )
{
int a = 153 ;
cout << boolalpha << isArmstrong ( a ) << endl ;
a = 1253 ;
cout << boolalpha << isArmstrong ( a ) << endl ;
return 0 ;
}
``````

OUTPUT:

``````True
False
………
Process executed in 0.12 seconds
Press any key to continue.
``````

Another Example:

``````// C++ Program to find
// Nth Armstrong Number
#include < bits/stdc++.h >
#include < iostream >
#include < stdlib >
#include < math.h >
using namespace std ;

// Function to find Nth Armstrong Number
int NthArmstrong ( int a )
{
int count = 0 ;

// upper limit from integer
for ( int i = 1 ; i < = INT_MAX ; i++ ) {
int number = I , rem , digit = 0 , sum = 0 ;
// Copy the value for num in number
number = I ;
// Find total digits in number
digit = ( int ) log10 ( number ) + 1 ;
// Calculate sum of power of digits
while ( num > 0 ) {
rem = number % 10 ;
sum = sum + pow ( rem , digit ) ;
number = number / 10 ;
}
// Check for Armstrong number
if ( i == sum )
count ++ ;
if ( count == a )
return I ;
}
}
// Driver Function
int main ( )
{
int a = 12 ;
cout << NthArmstrong ( a ) ;
return 0 ;
}
``````

OUTPUT:

`371`

### Let us see another example for checking Armstrong number for n digits:

``````#include < cmath >
#include < iostream >
#include < bits/stdc++.h >
using namespace std ;
int main ( ) {
int number , originalNumber , remainder, a = 0 , result = 0 , power ;
cout << " enter an integer : " ;
cin >> number ;
originalNumber = number ;
while ( originalNum != 0 ) {
originalNumber /= 10 ;
++n ;
}
originalNumber = number ;
while ( originalNum != 0 ) {
remainder = originalNum % 10 ;
// pow ( ) returns a double value
// round ( ) returns the equivalent int
power = round ( pow ( remainder , a ) ) ;
result += power ;
originalNumber /= 10 ;
}
if ( result == number )
cout << number << " is an Armstrong number. " ;
else
cout << number << " is not an Armstrong number. " ;
return 0 ;
}
``````

OUTPUT:

``````Enter an integer: 9474
9472 is an Armstrong number.
``````

The number of digits in the input number is computed first and saved in a in this application. In each iteration of the while loop, the pow ( ) function computes the power of specific numbers.