Given a number x, determine whether the given number is Armstrong number or not. A positive integer of n digits is called an Armstrong number of order n (order is number of digits) if.
Attention reader! Don’t stop learning now. Get hold of all the important mathematical concepts for competitive programming with the Essential Maths for CP Course at a student-friendly price. To complete your preparation from learning a language to DS Algo and many more, please refer Complete Interview Preparation Course.
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
The idea is to first count number digits (or find order). Let the number of digits be n. For every digit r in input number x, compute rn. If sum of all such values is equal to n, then return true, else false.
C++
// C++ program to determine whether the number is
// Armstrong number or not
#include<bits/stdc++.h>
usingnamespacestd;
/* Function to calculate x raised to the power y */
intpower(intx, unsigned inty)
{
if( y == 0)
return1;
if(y%2 == 0)
returnpower(x, y/2)*power(x, y/2);
returnx*power(x, y/2)*power(x, y/2);
}
/* Function to calculate order of the number */
intorder(intx)
{
intn = 0;
while(x)
{
n++;
x = x/10;
}
returnn;
}
// Function to check whether the given number is
// Armstrong number or not
boolisArmstrong(intx)
{
// Calling order function
intn = order(x);
inttemp = x, sum = 0;
while(temp)
{
intr = temp%10;
sum += power(r, n);
temp = temp/10;
}
// If satisfies Armstrong condition
return(sum == x);
}
// Driver Program
intmain()
{
intx = 153;
cout << isArmstrong(x) << endl;
x = 1253;
cout << isArmstrong(x) << endl;
return0;
}
C
// C program to find Armstrong number
#include <stdio.h>
/* Function to calculate x raised to the power y */
intpower(intx, unsigned inty)
{
if(y == 0)
return1;
if(y % 2 == 0)
returnpower(x, y / 2) * power(x, y / 2);
returnx * power(x, y / 2) * power(x, y / 2);
}
/* Function to calculate order of the number */
intorder(intx)
{
intn = 0;
while(x) {
n++;
x = x / 10;
}
returnn;
}
// Function to check whether the given number is
// Armstrong number or not
intisArmstrong(intx)
{
// Calling order function
intn = order(x);
inttemp = x, sum = 0;
while(temp) {
intr = temp % 10;
sum += power(r, n);
temp = temp / 10;
}
// If satisfies Armstrong condition
if(sum == x)
return1;
else
return0;
}
// Driver Program
intmain()
{
intx = 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");
return0;
}
Java
// Java program to determine whether the number is
// Armstrong number or not
publicclassArmstrong
{
/* Function to calculate x raised to the
power y */
intpower(intx, longy)
{
if( y == 0)
return1;
if(y%2== 0)
returnpower(x, y/2)*power(x, y/2);
returnx*power(x, y/2)*power(x, y/2);
}
/* Function to calculate order of the number */
intorder(intx)
{
intn = 0;
while(x != 0)
{
n++;
x = x/10;
}
returnn;
}
// Function to check whether the given number is
// Armstrong number or not
booleanisArmstrong (intx)
{
// Calling order function
intn = order(x);
inttemp=x, sum=0;
while(temp!=0)
{
intr = temp%10;
sum = sum + power(r,n);
temp = temp/10;
}
// If satisfies Armstrong condition
return(sum == x);
}
// Driver Program
publicstaticvoidmain(String[] args)
{
Armstrong ob = newArmstrong();
intx = 153;
System.out.println(ob.isArmstrong(x));
x = 1253;
System.out.println(ob.isArmstrong(x));
}
}
Python
# Python program to determine whether the number is
# Armstrong number or not
# Function to calculate x raised to the power y
defpower(x, y):
ify==0:
return1
ify%2==0:
returnpower(x, y/2)*power(x, y/2)
returnx*power(x, y/2)*power(x, y/2)
# Function to calculate order of the number
deforder(x):
# variable to store of the number
n =0
while(x!=0):
n =n+1
x =x/10
returnn
# Function to check whether the given number is
# Armstrong number or not
defisArmstrong (x):
n =order(x)
temp =x
sum1 =0
while(temp!=0):
r =temp%10
sum1 =sum1 +power(r, n)
temp =temp/10
# If condition satisfies
return(sum1 ==x)
# Driver Program
x =153
print(isArmstrong(x))
x =1253
print(isArmstrong(x))
Python3
# python3 >= 3.6 for typehint support
# This example avoids the complexity of ordering
# through type conversions & string manipulation
defisArmstrong(val:int) -> bool:
"""val will be tested to see if its an Armstrong number.
Arguments:
val {int} -- positive integer only.
Returns:
bool -- true is /false isn't
"""
# break the int into its respective digits
parts =[int(_) for_ instr(val)]
# begin test.
counter =0
for_ inparts:
counter +=_**3
return( counter ==val )
# Check Armstrong number
print(isArmstrong(100))
print(isArmstrong(153))
# Get all the Armstrong number in range(1000)
print([ _ for_ inrange(1000) ifisArmstrong(_)])
C#
// C# program to determine whether the
// number is Armstrong number or not
usingSystem;
publicclassGFG
{
// Function to calculate x raised
// to the power y
intpower(intx, longy)
{
if( y == 0)
return1;
if(y % 2 == 0)
returnpower(x, y / 2) *
power(x, y / 2);
returnx * power(x, y / 2) *
power(x, y / 2);
}
// Function to calculate
// order of the number
intorder(intx)
{
intn = 0;
while(x != 0)
{
n++;
x = x / 10;
}
returnn;
}
// Function to check whether the
// given number is Armstrong number
// or not
boolisArmstrong (intx)
{
// Calling order function
intn = order(x);
inttemp = x, sum = 0;
while(temp != 0)
{
intr = temp % 10;
sum = sum + power(r, n);
temp = temp / 10;
}
// If satisfies Armstrong condition
return(sum == x);
}
// Driver Code
publicstaticvoidMain()
{
GFG ob = newGFG();
intx = 153;
Console.WriteLine(ob.isArmstrong(x));
x = 1253;
Console.WriteLine(ob.isArmstrong(x));
}
}
// This code is contributed by Nitin Mittal.
Javascript
<script>
// JavaScript program to determine whether the
// number is Armstrong number or not
// Function to calculate x raised
// to the power y
functionpower(x, y)
{
if( y == 0)
return1;
if(y % 2 == 0)
returnpower(x, parseInt(y / 2, 10)) *
power(x, parseInt(y / 2, 10));
returnx * power(x, parseInt(y / 2, 10)) *
power(x, parseInt(y / 2, 10));
}
// Function to calculate
// order of the number
functionorder(x)
{
let n = 0;
while(x != 0)
{
n++;
x = parseInt(x / 10, 10);
}
returnn;
}
// Function to check whether the
// given number is Armstrong number
// or not
functionisArmstrong(x)
{
// Calling order function
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);
}
// If satisfies Armstrong condition
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>
Output:
True
False
Find nth Armstrong number
Input : 9
Output : 9
Input : 10
Output : 153
C++
// C++ Program to find
// Nth Armstrong Number
#include<bits/stdc++.h>
#include<math.h>
usingnamespacestd;
// Function to find Nth Armstrong Number
intNthArmstrong(intn)
{
intcount=0;
// upper limit from integer
for(inti = 1; i <= INT_MAX; i++)
{
intnum=i, rem, digit=0, sum=0;
//Copy the value for num in num
num = i;
// Find total digits in num
digit = (int) log10(num) + 1;
// Calculate sum of power of digits
while(num > 0)
{
rem = num % 10;
sum = sum + pow(rem,digit);
num = num / 10;
}
// Check for Armstrong number
if(i == sum)
count++;
if(count==n)
returni;
}
}
// Driver Function
intmain()
{
intn = 12;
cout<<NthArmstrong(n);
return0;
}
// This Code is Contributed by 'jaingyayak'
Java
// Java Program to find
// Nth Armstrong Number
importjava.lang.Math;
classGFG
{
// Function to find Nth Armstrong Number
staticintNthArmstrong(intn)
{
intcount = 0;
// upper limit from integer
for(inti = 1; i <= Integer.MAX_VALUE; i++)
{
intnum = i, rem, digit = 0, sum = 0;
//Copy the value for num in num
num = i;
// Find total digits in num
digit = (int) Math.log10(num) + 1;
// Calculate sum of power of digits
while(num > 0)
{
rem = num % 10;
sum = sum + (int)Math.pow(rem, digit);
num = num / 10;
}
// Check for Armstrong number
if(i == sum)
count++;
if(count == n)
returni;
}
returnn;
}
// Driver Code
publicstaticvoidmain(String[] args)
{
intn = 12;
System.out.println(NthArmstrong(n));
}
}
// This code is contributed by Code_Mech.
Python3
# Python3 Program to find Nth Armstrong Number
importmath;
importsys;
# Function to find Nth Armstrong Number
defNthArmstrong(n):
count =0;
# upper limit from integer
fori inrange(1, sys.maxsize):
num =i;
rem =0;
digit =0;
sum=0;
# Copy the value for num in num
num =i;
# Find total digits in num
digit =int(math.log10(num) +1);
# Calculate sum of power of digits
while(num > 0):
rem =num %10;
sum=sum+pow(rem, digit);
num =num //10;
# Check for Armstrong number
if(i ==sum):
count +=1;
if(count ==n):
returni;
# Driver Code
n =12;
print(NthArmstrong(n));
# This code is contributed by chandan_jnu
C#
// C# Program to find
// Nth Armstrong Number
usingSystem;
classGFG
{
// Function to find Nth Armstrong Number
staticintNthArmstrong(intn)
{
intcount = 0;
// upper limit from integer
for(inti = 1; i <= int.MaxValue; i++)
{
intnum = i, rem, digit = 0, sum = 0;
// Copy the value for num in num
num = i;
// Find total digits in num
digit = (int) Math.Log10(num) + 1;
// Calculate sum of power of digits
while(num > 0)
{
rem = num % 10;
sum = sum + (int)Math.Pow(rem, digit);
num = num / 10;
}
// Check for Armstrong number
if(i == sum)
count++;
if(count == n)
returni;
}
returnn;
}
// Driver Code
publicstaticvoidMain()
{
intn = 12;
Console.WriteLine(NthArmstrong(n));
}
}
// This code is contributed by Code_Mech.
PHP
<?php
// PHP Program to find
// Nth Armstrong Number
// Function to find
// Nth Armstrong Number
functionNthArmstrong($n)
{
$count= 0;
// upper limit
// from integer
for($i= 1;
$i<= PHP_INT_MAX; $i++)
{
$num= $i;
$rem;
$digit= 0;
$sum= 0;
// Copy the value
// for num in num
$num= $i;
// Find total
// digits in num
$digit= (int) log10($num) + 1;
// Calculate sum of
// power of digits
while($num> 0)
{
$rem= $num% 10;
$sum= $sum+ pow($rem,
$digit);
$num= (int)$num/ 10;
}
// Check for
// Armstrong number
if($i== $sum)
$count++;
if($count== $n)
return$i;
}
}
// Driver Code
$n= 12;
echoNthArmstrong($n);
// This Code is Contributed
// by akt_mit
?>
Javascript
<script>
// Javascript program to find Nth Armstrong Number
// Function to find Nth Armstrong Number
functionNthArmstrong(n)
{
let count = 0;
// Upper limit from integer
for(let i = 1; i <= Number.MAX_VALUE; i++)
{
let num = i, rem, digit = 0, sum = 0;
// Copy the value for num in num
num = i;
// Find total digits in num
digit = parseInt(Math.log10(num), 10) + 1;
// Calculate sum of power of digits
while(num > 0)
{
rem = num % 10;
sum = sum + Math.pow(rem, digit);
num = parseInt(num / 10, 10);
}
// Check for Armstrong number
if(i == sum)
count++;
if(count == n)
returni;
}
returnn;
}
// Driver code
let n = 12;
document.write(NthArmstrong(n));
// This code is contributed by rameshtravel07
</script>
Output:
371
Using Strings:
When considering the number as a string we can access any digit we want and perform operations
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
We use cookies to ensure you have the best browsing experience on our website. By using our site, you
acknowledge that you have read and understood our
Cookie Policy &
Privacy Policy