0
0
CppProgramBeginner · 2 min read

C++ Program to Check Armstrong Number

A C++ program to check an Armstrong number reads a number, calculates the sum of the cubes of its digits using while loop and pow function, 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, cube each digit, and add these cubes. If the total equals the original number, it is Armstrong. This works because Armstrong numbers are equal to the sum of their digits raised to the power of the number of digits.
📐

Algorithm

1
Get input number from user.
2
Store the original number for later comparison.
3
Initialize sum to zero.
4
Extract each digit by taking remainder with 10.
5
Cube the digit and add to sum.
6
Remove the last digit by dividing number by 10.
7
Repeat until number becomes zero.
8
Compare sum with original number and print result.
💻

Code

cpp
#include <iostream>
#include <cmath>
using namespace std;

int main() {
    int num, originalNum, remainder, sum = 0;
    cout << "Enter an integer: ";
    cin >> num;
    originalNum = num;

    while (num != 0) {
        remainder = num % 10;
        sum += pow(remainder, 3);
        num /= 10;
    }

    if (sum == originalNum)
        cout << originalNum << " is an Armstrong number." << endl;
    else
        cout << originalNum << " is not an Armstrong number." << endl;

    return 0;
}
Output
Enter an integer: 153 153 is an Armstrong number.
🔍

Dry Run

Let's trace the input 153 through the code.

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

IterationRemainderSumNum after division
132715
251521
311530
💡

Why This Works

Step 1: Extract digits

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

Step 2: Calculate cube and sum

Each digit is raised to the power 3 using pow and added to sum to accumulate the total.

Step 3: Compare sum with original

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

🔄

Alternative Approaches

Using string conversion
cpp
#include <iostream>
#include <cmath>
#include <string>
using namespace std;

int main() {
    string numStr;
    cout << "Enter an integer: ";
    cin >> numStr;
    int sum = 0;
    int originalNum = stoi(numStr);

    for (char c : numStr) {
        int digit = c - '0';
        sum += pow(digit, 3);
    }

    if (sum == originalNum)
        cout << originalNum << " is an Armstrong number." << endl;
    else
        cout << originalNum << " is not an Armstrong number." << endl;

    return 0;
}
This approach uses string to iterate digits, which can be simpler but uses extra memory for the string.
Using function for power calculation
cpp
#include <iostream>
using namespace std;

int cube(int n) {
    return n * n * n;
}

int main() {
    int num, originalNum, remainder, sum = 0;
    cout << "Enter an integer: ";
    cin >> num;
    originalNum = num;

    while (num != 0) {
        remainder = num % 10;
        sum += cube(remainder);
        num /= 10;
    }

    if (sum == originalNum)
        cout << originalNum << " is an Armstrong number." << endl;
    else
        cout << originalNum << " is not an Armstrong number." << endl;

    return 0;
}
This method avoids <code>pow</code> function by using a custom cube function, which can be faster and simpler.

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 variables are used regardless of input size, so space complexity is O(1).

Which Approach is Fastest?

Using a custom cube function is faster than pow because it avoids floating-point operations, while string conversion uses extra memory.

ApproachTimeSpaceBest For
Using pow functionO(d)O(1)Simplicity and readability
String conversionO(d)O(d)Easy digit access but uses extra memory
Custom cube functionO(d)O(1)Performance and simplicity
💡
Use pow(digit, 3) to calculate cubes easily instead of manual multiplication.
⚠️
Forgetting to compare the sum with the original number instead of the modified number after digit extraction.