int c=0,a,temp; int n=153;//It is the number to check armstrong temp=n; while(n>0) { a=n%10; n=n/10; c=c+(a*a*a); } if(temp==c) System.out.println("armstrong number"); else System.out.println("Not armstrong number"); } code example
Example 1: armstrong number in java
int c=0,a,temp;
int n=153;//It is the number to check armstrong
temp=n;
while(n>0)
{
a=n%10;
n=n/10;
c=c+(a*a*a);
}
if(temp==c)
System.out.println("armstrong number");
else
System.out.println("Not armstrong number");
Example 2: armstrong number in c
#include
#include
#include
int cube(int a)
{
int c;
c = a*a*a;
return c;
}
int armnum(int *a)
{
int x = *a, n = 0, rem, r = 0;
while (x != 0) {
x /= 10;
n++;
}
x = *a;
while (x != 0) {
rem = x % 10;
r += cube(rem);
x /= 10;
}
if(r == *a){
return 1;
}
}
int main()
{
int a, y;
scanf("%d", &a);
y = armnum(&a);
if(y == 1){
printf("It is an Armstrong number.");
}
else{
printf("It is not an Armstrong number.");
}
}