Given a number x, determine whether the given number is Armstrong’s number or not.
A positive integer of n digits is called an Armstrong number of order n (order is the number of digits) if
abcd... = pow(a,n) + pow(b,n) + pow(c,n) + pow(d,n) + ....
Example:
Input:153
Output: Yes
153 is an Armstrong number.
1*1*1 + 5*5*5 + 3*3*3 = 153
Input: 120
Output: No
120 is not a Armstrong number.
1*1*1 + 2*2*2 + 0*0*0 = 9
Input: 1253
Output: No
1253 is not a Armstrong Number
1*1*1*1 + 2*2*2*2 + 5*5*5*5 + 3*3*3*3 = 723
Input: 1634
Output: Yes
1*1*1*1 + 6*6*6*6 + 3*3*3*3 + 4*4*4*4 = 1634
Naive Approach
The idea is to first count the number of digits (or find the order).
Algorithm:
- Let the number of digits be n.
- For every digit r in input number x, compute rn.
- If the sum of all such values is equal to x, then return true, else false.
Below is the program to check whether the number is an Armstrong number or not:
C++
#include <bits/stdc++.h>
using namespace std;
int power( int x, unsigned int y)
{
if (y == 0)
return 1;
if (y % 2 == 0)
return power(x, y / 2) * power(x, y / 2);
return x * power(x, y / 2) * power(x, y / 2);
}
int order( int x)
{
int n = 0;
while (x) {
n++;
x = x / 10;
}
return n;
}
bool isArmstrong( int x)
{
int n = order(x);
int temp = x, sum = 0;
while (temp) {
int r = temp % 10;
sum += power(r, n);
temp = temp / 10;
}
return (sum == x);
}
int main()
{
int x = 153;
cout << boolalpha << isArmstrong(x) << endl;
x = 1253;
cout << boolalpha << isArmstrong(x) << endl;
return 0;
}
|
C
#include <stdio.h>
int power( int x, unsigned int y)
{
if (y == 0)
return 1;
if (y % 2 == 0)
return power(x, y / 2) * power(x, y / 2);
return x * power(x, y / 2) * power(x, y / 2);
}
int order( int x)
{
int n = 0;
while (x) {
n++;
x = x / 10;
}
return n;
}
int isArmstrong( int x)
{
int n = order(x);
int temp = x, sum = 0;
while (temp) {
int r = temp % 10;
sum += power(r, n);
temp = temp / 10;
}
if (sum == x)
return 1;
else
return 0;
}
int main()
{
int x = 153;
if (isArmstrong(x) == 1)
printf ( "True\n" );
else
printf ( "False\n" );
x = 1253;
if (isArmstrong(x) == 1)
printf ( "True\n" );
else
printf ( "False\n" );
return 0;
}
|
Java
public class Armstrong {
int power( int x, long y)
{
if (y == 0 )
return 1 ;
if (y % 2 == 0 )
return power(x, y / 2 ) * power(x, y / 2 );
return x * power(x, y / 2 ) * power(x, y / 2 );
}
int order( int x)
{
int n = 0 ;
while (x != 0 ) {
n++;
x = x / 10 ;
}
return n;
}
boolean isArmstrong( int x)
{
int n = order(x);
int temp = x, sum = 0 ;
while (temp != 0 ) {
int r = temp % 10 ;
sum = sum + power(r, n);
temp = temp / 10 ;
}
return (sum == x);
}
public static void main(String[] args)
{
Armstrong ob = new Armstrong();
int x = 153 ;
System.out.println(ob.isArmstrong(x));
x = 1253 ;
System.out.println(ob.isArmstrong(x));
}
}
|
Python3
def isArmstrong(val: int ) - > bool :
parts = [ int (_) for _ in str (val)]
counter = 0
for _ in parts:
counter + = _ * * 3
return (counter = = val)
print (isArmstrong( 153 ))
print (isArmstrong( 1253 ))
|
Python
def power(x, y):
if y = = 0 :
return 1
if y % 2 = = 0 :
return power(x, y / 2 ) * power(x, y / 2 )
return x * power(x, y / 2 ) * power(x, y / 2 )
def order(x):
n = 0
while (x ! = 0 ):
n = n + 1
x = x / 10
return n
def isArmstrong(x):
n = order(x)
temp = x
sum1 = 0
while (temp ! = 0 ):
r = temp % 10
sum1 = sum1 + power(r, n)
temp = temp / 10
return (sum1 = = x)
x = 153
print (isArmstrong(x))
x = 1253
print (isArmstrong(x))
|
C#
using System;
public class GFG {
int power( int x, long y)
{
if (y == 0)
return 1;
if (y % 2 == 0)
return power(x, y / 2) * power(x, y / 2);
return x * power(x, y / 2) * power(x, y / 2);
}
int order( int x)
{
int n = 0;
while (x != 0) {
n++;
x = x / 10;
}
return n;
}
bool isArmstrong( int x)
{
int n = order(x);
int temp = x, sum = 0;
while (temp != 0) {
int r = temp % 10;
sum = sum + power(r, n);
temp = temp / 10;
}
return (sum == x);
}
public static void Main()
{
GFG ob = new GFG();
int x = 153;
Console.WriteLine(ob.isArmstrong(x));
x = 1253;
Console.WriteLine(ob.isArmstrong(x));
}
}
|
Javascript
<script>
function power(x, y)
{
if ( y == 0)
return 1;
if (y % 2 == 0)
return power(x, parseInt(y / 2, 10)) *
power(x, parseInt(y / 2, 10));
return x * power(x, parseInt(y / 2, 10)) *
power(x, parseInt(y / 2, 10));
}
function order(x)
{
let n = 0;
while (x != 0)
{
n++;
x = parseInt(x / 10, 10);
}
return n;
}
function isArmstrong(x)
{
let n = order(x);
let temp = x, sum = 0;
while (temp != 0)
{
let r = temp % 10;
sum = sum + power(r, n);
temp = parseInt(temp / 10, 10);
}
return (sum == x);
}
let x = 153;
if (isArmstrong(x))
{
document.write( "True" + "</br>" );
}
else {
document.write( "False" + "</br>" );
}
x = 1253;
if (isArmstrong(x))
{
document.write( "True" );
}
else {
document.write( "False" );
}
</script>
|
Optimized Approach
Below is the shorter way to implement the above approach:
C++
#include <iostream>
using namespace std;
int main()
{
int n = 153;
int temp = n;
int p = 0;
while (n > 0) {
int rem = n % 10;
p = (p) + (rem * rem * rem);
n = n / 10;
}
if (temp == p) {
cout << ( "Yes. It is Armstrong No." );
}
else {
cout << ( "No. It is not an Armstrong No." );
}
return 0;
}
|
C
#include <stdio.h>
int main()
{
int n = 153;
int temp = n;
int p = 0;
while (n > 0) {
int rem = n % 10;
p = (p) + (rem * rem * rem);
n = n / 10;
}
if (temp == p) {
printf ( "Yes. It is Armstrong No." );
}
else {
printf ( "No. It is not an Armstrong No." );
}
return 0;
}
|
Java
public class ArmstrongNumber {
public static void main(String[] args)
{
int n = 153 ;
int temp = n;
int p = 0 ;
while (n > 0 ) {
int rem = n % 10 ;
p = (p) + (rem * rem * rem);
n = n / 10 ;
}
if (temp == p) {
System.out.println( "Yes. It is Armstrong No." );
}
else {
System.out.println(
"No. It is not an Armstrong No." );
}
}
}
|
Python3
n = 153
temp = n
p = 0
while (n > 0 ):
rem = n % 10
p = (p) + (rem * rem * rem)
n = n / / 10
if temp = = p:
print ( "armstrong" )
else :
print ( "not a armstrong number" )
|
C#
using System;
public class ArmstrongNumber {
public static void Main( string [] args)
{
int n = 153;
int temp = n;
int p = 0;
while (n > 0) {
int rem = n % 10;
p = (p) + (rem * rem * rem);
n = n / 10;
}
if (temp == p) {
Console.WriteLine( "Yes. It is Armstrong No." );
}
else {
Console.WriteLine(
"No. It is not an Armstrong No." );
}
}
}
|
Javascript
let n = 153;
let temp = n;
let p = 0;
while (n > 0) {
let rem = n % 10;
p = (p) + (rem * rem * rem);
n = Math.floor(n / 10);
}
if (temp == p) {
console.log( "Yes. It is Armstrong No." );
}
else {
console.log( "No. It is not an Armstrong No." );
}
|
Output
Yes. It is Armstrong No.
Time complexity: O(log n)
Auxiliary Space:O(1)
Find nth Armstrong Number
Input: 9
Output: 9
Input: 10
Output: 153
Below is the program to find the nth Armstrong number:
C++
#include <bits/stdc++.h>
#include <math.h>
using namespace std;
int NthArmstrong( int n)
{
int count = 0;
for ( int i = 1; i <= INT_MAX; i++) {
int num = i, rem, digit = 0, sum = 0;
num = i;
digit = ( int ) log10 (num) + 1;
while (num > 0) {
rem = num % 10;
sum = sum + pow (rem, digit);
num = num / 10;
}
if (i == sum)
count++;
if (count == n)
return i;
}
}
int main()
{
int n = 12;
cout << NthArmstrong(n);
return 0;
}
|
C
#include <limits.h>
#include <math.h>
#include <stdio.h>
int NthArmstrong( int n)
{
int count = 0;
for ( int i = 1; i <= INT_MAX; i++) {
int num = i, rem, digit = 0, sum = 0;
num = i;
digit = ( int ) log10 (num) + 1;
while (num > 0) {
rem = num % 10;
sum = sum + pow (rem, digit);
num = num / 10;
}
if (i == sum)
count++;
if (count == n)
return i;
}
}
int main()
{
int n = 12;
printf ( "%d" , NthArmstrong(n));
return 0;
}
|
Java
import java.lang.Math;
class GFG {
static int NthArmstrong( int n)
{
int count = 0 ;
for ( int i = 1 ; i <= Integer.MAX_VALUE; i++) {
int num = i, rem, digit = 0 , sum = 0 ;
num = i;
digit = ( int )Math.log10(num) + 1 ;
while (num > 0 ) {
rem = num % 10 ;
sum = sum + ( int )Math.pow(rem, digit);
num = num / 10 ;
}
if (i == sum)
count++;
if (count == n)
return i;
}
return n;
}
public static void main(String[] args)
{
int n = 12 ;
System.out.println(NthArmstrong(n));
}
}
|
Python3
import math
import sys
def NthArmstrong(n):
count = 0
for i in range ( 1 , sys.maxsize):
num = i
rem = 0
digit = 0
sum = 0
num = i
digit = int (math.log10(num) + 1 )
while (num > 0 ):
rem = num % 10
sum = sum + pow (rem, digit)
num = num / / 10
if (i = = sum ):
count + = 1
if (count = = n):
return i
n = 12
print (NthArmstrong(n))
|
C#
using System;
class GFG {
static int NthArmstrong( int n)
{
int count = 0;
for ( int i = 1; i <= int .MaxValue; i++) {
int num = i, rem, digit = 0, sum = 0;
num = i;
digit = ( int )Math.Log10(num) + 1;
while (num > 0) {
rem = num % 10;
sum = sum + ( int )Math.Pow(rem, digit);
num = num / 10;
}
if (i == sum)
count++;
if (count == n)
return i;
}
return n;
}
public static void Main()
{
int n = 12;
Console.WriteLine(NthArmstrong(n));
}
}
|
Javascript
<script>
function NthArmstrong(n)
{
let count = 0;
for (let i = 1; i <= Number.MAX_VALUE; i++)
{
let num = i, rem, digit = 0, sum = 0;
num = i;
digit = parseInt(Math.log10(num), 10) + 1;
while (num > 0)
{
rem = num % 10;
sum = sum + Math.pow(rem, digit);
num = parseInt(num / 10, 10);
}
if (i == sum)
count++;
if (count == n)
return i;
}
return n;
}
let n = 12;
document.write(NthArmstrong(n));
</script>
|
PHP
<?php
function NthArmstrong( $n )
{
$count = 0;
for ( $i = 1;
$i <= PHP_INT_MAX; $i ++)
{
$num = $i ;
$rem ;
$digit = 0;
$sum = 0;
$num = $i ;
$digit = (int) log10( $num ) + 1;
while ( $num > 0)
{
$rem = $num % 10;
$sum = $sum + pow( $rem ,
$digit );
$num = (int) $num / 10;
}
if ( $i == $sum )
$count ++;
if ( $count == $n )
return $i ;
}
}
$n = 12;
echo NthArmstrong( $n );
?>
|
Time complexity: O(log n)
Auxiliary Space: O(1)
Using Numeric Strings
When considering the number as a string we can access any digit we want and perform operations. Below is the program to implement the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
string armstrong( int n)
{
string number = to_string(n);
n = number.length();
long long output = 0;
for ( char i : number)
output = output + ( long ) pow ((i - '0' ), n);
if (output == stoll(number))
return ( "True" );
else
return ( "False" );
}
int main()
{
cout << armstrong(153) << endl;
cout << armstrong(1253) << endl;
}
|
Java
public class armstrongNumber {
public void isArmstrong(String n)
{
char [] s = n.toCharArray();
int size = s.length;
int sum = 0 ;
for ( char num : s) {
int temp = 1 ;
int i
= Integer.parseInt(Character.toString(num));
for ( int j = 0 ; j <= size - 1 ; j++) {
temp *= i;
}
sum += temp;
}
if (sum == Integer.parseInt(n)) {
System.out.println( "True" );
}
else {
System.out.println( "False" );
}
}
public static void main(String[] args)
{
armstrongNumber am = new armstrongNumber();
am.isArmstrong( "153" );
am.isArmstrong( "1253" );
}
}
|
Python3
def armstrong(n):
number = str (n)
n = len (number)
output = 0
for i in number:
output = output + int (i) * * n
if output = = int (number):
return ( True )
else :
return ( False )
print (armstrong( 153 ))
print (armstrong( 1253 ))
|
C#
using System;
public class armstrongNumber {
public void isArmstrong(String n)
{
char [] s = n.ToCharArray();
int size = s.Length;
int sum = 0;
foreach ( char num in s)
{
int temp = 1;
int i = Int32.Parse( char .ToString(num));
for ( int j = 0; j <= size - 1; j++) {
temp *= i;
}
sum += temp;
}
if (sum == Int32.Parse(n)) {
Console.WriteLine( "True" );
}
else {
Console.WriteLine( "False" );
}
}
public static void Main(String[] args)
{
armstrongNumber am = new armstrongNumber();
am.isArmstrong( "153" );
am.isArmstrong( "1253" );
}
}
|
Javascript
<script>
function armstrong(n){
let number = new String(n)
n = number.length
let output = 0
for (let i of number)
output = output + parseInt(i)**n
if (output == parseInt(number))
return ( "True" + "<br>" )
else
return ( "False" + "<br>" )
}
document.write(armstrong(153))
document.write(armstrong(1253))
</script>
|
Time Complexity: O(n).
Auxiliary Space: O(1).
Find all Armstrong Numbers in a Range
Below is the program to find all Armstrong numbers in a given range:
C++
#include <bits/stdc++.h>
using namespace std;
void isArmstrong( int left, int right)
{
for ( int i = left; i <= right; i++) {
int sum = 0;
int temp = i;
while (temp > 0) {
int lastdigit = temp % 10;
sum += pow (lastdigit, 3);
temp /= 10;
}
if (sum == i) {
cout << i << " " ;
}
}
cout << endl;
}
int main()
{
int left = 5, right = 1000;
isArmstrong(left, right);
return 0;
}
|
Java
import java.util.*;
public class Main {
public static void isArmstrong( int left, int right)
{
for ( int i = left; i <= right; i++) {
int sum = 0 ;
int temp = i;
while (temp > 0 ) {
int lastdigit = temp % 10 ;
sum += Math.pow(lastdigit, 3 );
temp /= 10 ;
}
if (sum == i) {
System.out.print(i + " " );
}
}
System.out.println();
}
public static void main(String[] args)
{
int left = 5 , right = 1000 ;
isArmstrong(left, right);
}
}
|
Python3
def armstrong(n):
number = str (n)
n = len (number)
output = 0
for i in number:
output = output + int (i) * * n
if output = = int (number):
return ( True )
else :
return ( False )
arm_list = []
nums = range ( 10 , 1000 )
for i in nums:
if armstrong(i):
arm_list.append(i)
else :
pass
print (arm_list)
|
C#
using System;
class MainClass {
static void isArmstrong( int left, int right)
{
for ( int i = left; i <= right; i++) {
int sum = 0;
int temp = i;
while (temp > 0) {
int lastdigit = temp % 10;
sum += ( int )Math.Pow(lastdigit, 3);
temp /= 10;
}
if (sum == i) {
Console.Write(i + " " );
}
}
Console.WriteLine();
}
public static void Main( string [] args)
{
int left = 5, right = 1000;
isArmstrong(left, right);
}
}
|
Javascript
function isArmstrong(left, right) {
for (let i = left; i <= right; i++) {
let sum = 0;
let temp = i;
while (temp > 0) {
let lastdigit = temp % 10;
sum += Math.pow(lastdigit, 3);
temp = Math.floor(temp / 10);
}
if (sum === i) {
process.stdout.write(i + " " );
}
}
console.log( "\n" );
}
let left = 5,
right = 1000;
isArmstrong(left, right);
|
References:
http://www.cs.mtu.edu/~shene/COURSES/cs201/NOTES/chap04/arms.html
http://www.programiz.com/c-programming/examples/check-armstrong-number
This article is contributed by Rahul Agrawal .If you like GeeksforGeeks and would like to contribute, you can also write an article using write.geeksforgeeks.org or mail your article to review-team@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please write comments if you find anything incorrect, or if you want to share more information about the topic discussed above.
Feeling lost in the world of random DSA topics, wasting time without progress? It's time for a change! Join our DSA course, where we'll guide you on an exciting journey to master DSA efficiently and on schedule.
Ready to dive in? Explore our Free Demo Content and join our DSA course, trusted by over 100,000 geeks!
Last Updated :
03 Aug, 2023
Like Article
Save Article