Question
Check whether the given number is Armstrong number or not.
(A positive number is called Armstrong number if sum of its own digits each raised to the power of the number of digits is equal to number)
Enter a number
8208
8^4+2^4+0^4+8^4=4096+16+0+4096
8208 is an armstrong number
Enter a number
153
1^3+5^3+3^3=1+125+27=153
153 is an armstrong number
Share code with your friends
Share on whatsapp
Share on facebook
Share on twitter
Share on telegram
Code
Java
Python
Java
import java.util.Scanner;
public class Armstrong
{
public static void main(String[] args)
{
int c=0,r,temp=0,num=0,noOfDigits=0;
Scanner sc = new Scanner(System.in);
System.out.println("Enter a number");
num = sc.nextInt();
temp=num;
while(temp>0)
{
noOfDigits++;
temp=temp/10;
}
temp=num;
while(temp>0)
{
r=temp%10;
temp=temp/10;
c=c+(int)(Math.pow(r,noOfDigits));
}
if(num==c)
{
System.out.println(num+" is an armstrong number");
}
else
{
System.out.println(num+" is Not armstrong number");
}
}
}
Python
number=int(input("Enter the number:"))
totalDigits=len(str(number))
"""
str(number) converted integer to string
len() gives length of string
so len(str(number)) combined gives total number of digits
"""
sum=0
temp=number
while(temp>0):
digit=temp%10
sum=sum+digit**totalDigits
temp=temp//10
if(sum==number):
print(number,"is Armstrong number")
else:
print(number,"is not Armstrong number")