How to write a C Program to Armstrong Number Checker in C Programming Language ?
An Armstrong number of three digits is an integer such that the sum of the cubes of its digits is equal to the number itself. For example, 371 is an Armstrong number since 3**3 + 7**3 + 1**3 = 371.
Solution:
- #include <stdio.h>
- int numbers[50];
- int digit, i=0, j, sum=0, lengthOfNumber, armstrong;
- int separateDigits(num){
- // This function first takes modulus by dividing the number by 10 and then actually divides it by 10.
- while(num>0){
- numbers[i]=num%10;
- num/= 10;
- i++;
- }
- lengthOfNumber=i;
- }
- int checkArmstrong(originalNumber){
- for(j=0; j<=lengthOfNumber; j++){
- sum+=numbers[j]*numbers[j]*numbers[j];
- }
- if(sum==originalNumber){
- return 1;
- }
- else return 0;
- }
- int main()
- {
- printf("Enter Number : ");
- scanf("%d", &digit);
- separateDigits(digit); //We separate digits by calling a function separateDigits and giving the input as argument.
- armstrong = checkArmstrong(digit); // Variable 'armstrong' holds the value that the function returns.
- if(armstrong==1){
- printf("\nNumber is Armstrong !");
- }
- else printf("\nNumber is not Armstrong!");
- return 0;
- }