C++ Program to Check Armstrong Number with Explanation
C++
Hard
Control Flow
33 views
1 min read
188 words
This problem helps you practice core C++ fundamentals in a practical way. It builds intuition around armstrong, cube, digit. Let’s break it down step by step so you can implement it confidently.
Problem Statement
Number equals sum of cubes of its digits. Example: 153 = 1³+5³+3³
Real Life: Special numbers in mathematics.
Input Format
An integer N.
Output Format
Print Armstrong Number
OR
Not an Armstrong Number
Constraints
• N ≥ 0
• Works for 3-digit numbers (cube of digits)
Concept Explanation
A number is called an Armstrong number if the sum of cubes of its digits
is equal to the number itself.
Example:
153 = 1³ + 5³ + 3³ = 153
Step-by-Step Logic
1.Take the number N.
2.Store original value in original.
3.Initialize sum = 0.
4.While N > 0:
• Extract last digit using digit = N % 10.
• Add cube of digit to sum.
• Remove last digit using N = N / 10.
5.Compare sum with original.
6.If both are equal:
• Print Armstrong Number.
7.Else:
• Print Not an Armstrong Number.
Code Solution
This explanation is written for learning purposes and to help beginners understand the concept clearly.
void control_q12_armstrong() {
int number = 153;
int original = number;
int sum = 0;
while(number > 0) {
int digit = number % 10; // Get last digit
sum = sum + (digit * digit * digit); // Add cube
number = number / 10; // Remove last digit
}
if(sum == original) {
cout << original << " is an Armstrong number" << endl;
} else {
cout << original << " is NOT an Armstrong number" << endl;
}
}
Common Mistakes
- Misreading input/output format.
- Not handling constraints and edge cases.
- Off-by-one errors in loops.
- Forgetting to reset variables between test cases (if any).
Solution Guide
Problem
Number equals sum of cubes of its digits. Example: 153 = 1³+5³+3³
Real Life: Special numbers in mathematics.
Input / Output
Output
Print Armstrong Number
OR
Not an Armstrong Number
Constraints
• N ≥ 0
• Works for 3-digit numbers (cube of digits)
Explanation
Concept Explanation
A number is called an Armstrong number if the sum of cubes of its digits
is equal to the number itself.
Example:
153 = 1³ + 5³ + 3³ = 153
Step-by-Step Explanation
1.Take the number N.
2.Store original value in original.
3.Initialize sum = 0.
4.While N > 0:
• Extract last digit using digit = N % 10.
• Add cube of digit to sum.
• Remove last digit using N = N / 10.
5.Compare sum with original.
6.If both are equal:
• Print Armstrong Number.
7.Else:
• Print Not an Armstrong Number.
Details
Common Mistakes
- Misreading input/output format.
- Not handling constraints and edge cases.
- Off-by-one errors in loops.
- Forgetting to reset variables between test cases (if any).
Official Solution
void control_q12_armstrong() {
int number = 153;
int original = number;
int sum = 0;
while(number > 0) {
int digit = number % 10; // Get last digit
sum = sum + (digit * digit * digit); // Add cube
number = number / 10; // Remove last digit
}
if(sum == original) {
cout << original << " is an Armstrong number" << endl;
} else {
cout << original << " is NOT an Armstrong number" << endl;
}
}
Solutions (0)
No solutions submitted yet. Be the first!