24 Computer -- Programming Concepts and Logics

ask mattrab Visit www.askmattrab.com for more academic resources.

C program to check if a number is Armstrong or not

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


#include<stdio.h>

int main(){
    int n, i, sum = 0, temp;
    printf("Enter a number: ");
    scanf("%d", &n);
    temp = n;
    while (n != 0)
    {
        i = n % 10;
        sum = sum + i * i * i;
        n = n / 10;
    }
    if (sum == temp){
        printf("%d is an Armstrong number", temp);
    }
    else{
        printf("%d is not an Armstrong number", temp);
    }
    return 0;
}


// Input: 153

// Output: 153 is an Armstrong number

Discussions

More notes on Programming Concepts and Logics

Close Open App