C Program to Check Armstrong Number

Armstrong Number

Armstrong number is a number that is equal to the sum of cubes of its digits.

For example : 153, 370, 371 and 407 are the Armstrong numbers.

Let us take an example to understand Armstrong Number

153 = 13 + 53 + 33

153 = 1 + 125 + 27

153 = 153

Hence, 153 is Armstrong number.


Let's write a C Program to check a number is Armstrong or not.

Armstrong number checker program


#include <stdio.h>

    int main(){
        int num, rev = 0, rem, temp;
    
        printf("Enter a number to check Armstrong or not : ");
        scanf("%d", &num);
    
        temp = num;
    
        while(num != 0){
            // rem contains the last digit
            rem = num % 10;
    
            rev = rev + rem * rem * rem;
    
            // removing last digit from the num
            num = num / 10;
        }
    
        if(temp == rev){
            printf("Armstrong number");
        }
    
        else{
            printf("Not an Armstrong number");
        }
        return 0;
    }
    

Output 1


Enter a number to check Armstrong or not : 153
Armstrong number

Output 2


Enter a number to check Armstrong or not : 253
Not an Armstrong number

Post a Comment

0 Comments