Before going to the Amstrong number in C, Let's understand how to check a number is Armstrong or not.
If a number is equal to the sum of cubes of its digits, then the number is the Amstrong number. 0, 1, 153, 370 .. are Amstrong numbers.
Let's understand how to check 370 is an Amstrong Number:
370 = (3*3*3+7*7*7 +0*0*0)
= 370
Hence 370 is an Amstrong Number
To understand this program, You should have knowledge of
Program to check Armstrong number in c
#include<stdio.h> int main() { int num,rem,sum=0,temp; printf("Enter a number: "); scanf("%d",&num); temp=num; while(num>0){ rem=num%10; sum=sum+(rem*rem*rem); num=num/10; } if(temp==sum){ printf("Armstrong Number"); }else{ printf("Not Armstrong Number"); } return 0; }
Output
Enter a number: 370
not armstrong number
#include <stdio.h> int main() { int temp, rem, sum = 0; int armstrong_number = 370; temp = armstrong_number; while(temp != 0) { rem = temp % 10; sum = sum + (rem * rem * rem); temp = temp / 10; } if(sum == armstrong_number) printf("%d is an Armstrong number.", armstrong_number); else printf("%d is not an Armstrong number.", armstrong_number); return 0; }
Output
370 is an Armstrong number.
0 0C program to check Amstrong Number using User Defined Function
#include <stdio.h> int checkIsAmstrogNo(int number) { int temp=number; int rem,sum; sum=0; while(temp!=0) { rem=temp%10; sum=sum + (rem*rem*rem); temp=temp/10; } if(sum==number) return 1; //Amstrong Number else return 0; //Not an Amstrong Number } int main() { int num; printf("Enter an integer number: "); scanf("%d", &num); if(checkIsAmstrogNo(num)) printf("%d is an Armstrong number.",num); else printf("%d is not an Armstrong number.",num); return 0; }
Output:
Enter an integer number: 370
370 is an Armstrong number.
Please Login to Post the answer