0
0
CProgramBeginner · 2 min read

C Program to Check Armstrong Number with Output

A C program to check an Armstrong number reads a number, calculates the sum of cubes of its digits using while loop and % and / operators, then compares the sum with the original number to decide if it is Armstrong.
📋

Examples

Input153
Output153 is an Armstrong number.
Input370
Output370 is an Armstrong number.
Input123
Output123 is not an Armstrong number.
🧠

How to Think About It

To check if a number is Armstrong, break it into digits by repeatedly using % 10 to get the last digit and / 10 to remove it. Cube each digit and add to a sum. If the sum equals the original number, it is Armstrong.
📐

Algorithm

1
Get input number from user.
2
Store the original number in a variable.
3
Initialize sum to zero.
4
While the number is greater than zero:
5
Extract last digit using modulus 10.
6
Cube the digit and add to sum.
7
Remove last digit by dividing number by 10.
8
Compare sum with original number.
9
Print if it is Armstrong or not.
💻

Code

c
#include <stdio.h>

int main() {
    int num, originalNum, remainder, sum = 0;
    printf("Enter an integer: ");
    scanf("%d", &num);
    originalNum = num;
    while (num > 0) {
        remainder = num % 10;
        sum += remainder * remainder * remainder;
        num /= 10;
    }
    if (sum == originalNum)
        printf("%d is an Armstrong number.\n", originalNum);
    else
        printf("%d is not an Armstrong number.\n", originalNum);
    return 0;
}
Output
Enter an integer: 153 153 is an Armstrong number.
🔍

Dry Run

Let's trace the number 153 through the code to check if it is Armstrong.

1

Input and Initialization

num = 153, originalNum = 153, sum = 0

2

First loop iteration

remainder = 153 % 10 = 3; sum = 0 + 3^3 = 27; num = 153 / 10 = 15

3

Second loop iteration

remainder = 15 % 10 = 5; sum = 27 + 5^3 = 27 + 125 = 152; num = 15 / 10 = 1

4

Third loop iteration

remainder = 1 % 10 = 1; sum = 152 + 1^3 = 152 + 1 = 153; num = 1 / 10 = 0

5

Comparison

sum = 153 equals originalNum = 153, so number is Armstrong

Iterationnumremaindersum
1153327
2155152
311153
💡

Why This Works

Step 1: Extract digits

Using % 10 extracts the last digit of the number to process each digit separately.

Step 2: Calculate cube and sum

Each digit is cubed and added to a running total to check the Armstrong condition.

Step 3: Compare sums

If the sum of cubes equals the original number, it confirms the number is Armstrong.

🔄

Alternative Approaches

Using a function to check Armstrong
c
#include <stdio.h>

int isArmstrong(int num) {
    int originalNum = num, sum = 0, remainder;
    while (num > 0) {
        remainder = num % 10;
        sum += remainder * remainder * remainder;
        num /= 10;
    }
    return sum == originalNum;
}

int main() {
    int num;
    printf("Enter an integer: ");
    scanf("%d", &num);
    if (isArmstrong(num))
        printf("%d is an Armstrong number.\n", num);
    else
        printf("%d is not an Armstrong number.\n", num);
    return 0;
}
Separates logic into a function for better readability and reuse.
Using math library pow() function
c
#include <stdio.h>
#include <math.h>

int main() {
    int num, originalNum, remainder, sum = 0;
    printf("Enter an integer: ");
    scanf("%d", &num);
    originalNum = num;
    while (num > 0) {
        remainder = num % 10;
        sum += (int)pow(remainder, 3);
        num /= 10;
    }
    if (sum == originalNum)
        printf("%d is an Armstrong number.\n", originalNum);
    else
        printf("%d is not an Armstrong number.\n", originalNum);
    return 0;
}
Uses <code>pow()</code> for exponentiation but requires type casting and math library.

Complexity: O(d) time, O(1) space

Time Complexity

The program loops through each digit once, so time depends on the number of digits d, making it O(d).

Space Complexity

Only a few integer variables are used, so space complexity is constant O(1).

Which Approach is Fastest?

Direct arithmetic operations are faster than using pow() from math library due to function call overhead.

ApproachTimeSpaceBest For
Direct arithmeticO(d)O(1)Simple and fast for small powers
Function with pow()O(d)O(1)Flexible for different powers but slower
Function modular designO(d)O(1)Better code organization and reuse
💡
Remember to store the original number before modifying it to compare after calculation.
⚠️
Forgetting to store the original number before the loop causes incorrect digit extraction.