Sum of Digits & Armstrong (Narcissistic) Number Check
Problem Statement
Given a positive integer N, compute the sum of its decimal digits and simultaneously check if N is a 3-digit Armstrong number (where the sum of the cubes of its digits equals the number itself, e.g., 153 = 1^3 + 5^3 + 3^3 = 1 + 125 + 27 = 153).
Variable Dictionary / Table
| Variable Name | Data Type | Role & Purpose |
|---|---|---|
| N | Integer | Input integer |
| Orig | Integer | Preserves original number |
| SumDigits | Integer | Sum of decimal digits |
| ArmstrongSum | Integer | Sum of cubes of decimal digits |
| Rem | Integer | Extracted digit (temp % 10) |
Step-by-Step Formal Algorithm
Step 1: Start.
Step 2: Read positive integer N.
Step 3: Set Orig = N, SumDigits = 0, ArmstrongSum = 0.
Step 4: While (N > 0):
Rem = N % 10
SumDigits = SumDigits + Rem
ArmstrongSum = ArmstrongSum + (Rem * Rem * Rem)
N = N / 10
Step 5: Print 'Sum of Digits:', SumDigits.
Step 6: If (ArmstrongSum == Orig):
Print Orig, 'is an Armstrong Number.'
Step 7: Else:
Print Orig, 'is NOT an Armstrong Number.'
Step 8: Stop.
Flowchart (Interactive Graphical Diagram)
SumDigits = 0
ArmstrongSum = 0"] Init --> Cond{"Is N > 0 ?"} Cond -- Yes --> Calc["Rem = N % 10
SumDigits += Rem
ArmstrongSum += Rem^3
N = N / 10"] Calc --> Cond Cond -- No --> OutSum[/"Print SumDigits"/] OutSum --> ChkArm{"Does ArmstrongSum == Orig ?"} ChkArm -- Yes --> OutYes[/"Print 'Armstrong Number'"/] OutYes --> Stop([Stop]) ChkArm -- No --> OutNo[/"Print 'Not Armstrong'"/] OutNo --> Stop
Flowchart (ANSI Terminal ASCII Chart)
+-----------------+
| [ START ] |
+-----------------+
|
v
+-----------------+
| Read N |
+-----------------+
|
v
+----------------------------+
| Orig = N |
| SumDigits = 0 |
| ArmstrongSum = 0 |
+----------------------------+
|
v
/ \
/ N > 0 ? \ ---NO---> [Print SumDigits]
\ / |
| YES v
v / \
+----------------------------+/ArmstrongSum\
| Rem = N % 10 |\ == Orig ? /
| SumDigits += Rem / YES/ \NO
| ArmstrongSum += Rem^3 / v v
| N = N / 10 / [Arm] [Not Arm]
+-------------------------+ | |
| +---+---+
+------> |
v
[STOP]
Dry-Run Execution Trace Table
| Execution Step / Case | Variables State 1 | Variables State 2 | Condition Evaluation | Output / Result |
|---|---|---|---|---|
| N = 153 | Orig = 153 | Digits: 3, 5, 1 | Sum = 3+5+1=9; Cubes = 27+125+1 = 153 == Orig | ARMSTRONG NUMBER |
| N = 370 | Orig = 370 | Digits: 0, 7, 3 | Sum = 10; Cubes = 0+343+27 = 370 == Orig | ARMSTRONG NUMBER |
| N = 120 | Orig = 120 | Digits: 0, 2, 1 | Sum = 3; Cubes = 0+8+1 = 9 != 120 | NOT ARMSTRONG |
Interactive C++ Compiler & Runner
Live In-BrowserEdit the code directly with the Edit Code button, provide custom inputs, and press Run Code to compile and execute in the live terminal console below!
#include <iostream>
using namespace std;
int main() {
int n;
cout << "Enter a positive integer: ";
if (!(cin >> n) || n < 0) {
cout << "Invalid input! Please enter a non-negative integer.\n";
return 1;
}
int original = n;
int sumOfDigits = 0;
int armstrongSum = 0; // Sum of cubes of digits
int temp = n;
while (temp > 0) {
int digit = temp % 10;
sumOfDigits += digit;
armstrongSum += (digit * digit * digit);
temp /= 10;
}
cout << "\n========================================\n";
cout << "Original Number : " << original << "\n";
cout << "Sum of Digits : " << sumOfDigits << "\n";
cout << "Sum of Cubed Digits : " << armstrongSum << "\n";
cout << "----------------------------------------\n";
if (armstrongSum == original) {
cout << "Result: " << original << " is a 3-DIGIT ARMSTRONG (Narcissistic) NUMBER!\n";
} else {
cout << "Result: " << original << " is NOT an Armstrong number.\n";
}
cout << "========================================\n";
return 0;
}
Input: 153 Output: ======================================== Original Number : 153 Sum of Digits : 9 Sum of Cubed Digits : 153 ---------------------------------------- Result: 153 is a 3-DIGIT ARMSTRONG (Narcissistic) NUMBER! ========================================
Famous 3-digit Armstrong numbers are 153, 370, 371, and 407. Preserving the original number in a temporary variable before mutating it inside the loop is a crucial exam programming pattern.