C program to check whether a number is Armstrong or not
Definition of Armstrong Number
Those numbers which sum of the cube of its digits is equal to that number are known as Armstrong numbers. For example 153 since 1^3 + 5^3 + 3^3 = 1+ 125 + 9 =153
Other Armstrong numbers: 370,371,407 etc.
General definition of ArmStrong Number:
Those numbers which sum of its digits to power of number of its digits is equal to that number are known as Armstrong numbers.
Example 1:
153
Total digits in 153 is 3
And 1^3 + 5^3 + 3^3 = 1 + 125 + 27 = 153
Example 2:
1634
Total digits in 1634 is 4
And 1^4 + 6^4 + 3^4 +4^4 = 1 + 1296 + 81 + 64 =1634
# include <stdio.h>
int main(){
int num,r,sum=0,temp;
printf("Enter a number: ");
scanf("%d",&num);
temp=num;
while(num!=0){
r=num%10;
num=num/10;
sum=sum+(r*r*r);
}
if(sum==temp)
printf("%d is an Armstrong number",temp);
else
printf("%d is not an Armstrong number",temp);
return 0;
}
C program for finding armstrong no in a given range
Description Of Hello World Example
The stdio.h is a header file which provide standard input output library function like printf(), scanf() etc. To use the file, we need to include it in our program using #include preprocessor.
The main() function
In C code execution begins from main() function ( put main() function anywhere in the program ).
The code inside the curly braces { } is the body of main() function. The main() function is mandatory in C program.
#include < stdio.h >
int main(){
int num,r,sum,temp;
int min,max;
printf("Enter the minimum range: ");
scanf("%d",&min);
printf("Enter the maximum range: ");
scanf("%d",&max);
printf("Armstrong numbers in given range are: ");
for(num=min;num <=max;num++){
temp=num;
sum = 0;
while(temp!=0){
r=temp%10;
temp=temp/10;
sum=sum+(r*r*r);
}
if(sum==num)
printf("%d ",num);
}
return 0;
}
#include <stdio.h>
int main(){
int num,r,sum=0,temp;
printf("Enter a number: ");
scanf("%d",&num);
for(temp=num;num!=0;num=num/10){
r=num%10;
sum=sum+(r*r*r);
}
if(sum==temp)
printf("%d is an Armstrong number",temp);
else
printf("%d is not an Armstrong number",temp);
return 0;
}
#include <stdio.h>
int main(){
int num,r,sum,temp;
for(num=1;num <=500;num++){
temp=num;
sum = 0;
while(temp!=0){
r=temp%10;
temp=temp/10;
sum=sum+(r*r*r);
}
if(sum==num)
printf("%d ",num);
}
return 0;
}
Ready to get started?
Replenish him third creature and meat blessed void a fruit gathered you’re, they’re two waters own morning gathered greater shall had behold had seed.