Check Armstrong Number

Check Armstrong Number

Hard C++ Control Flow 34 views
Explanation Complexity

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

Example

153
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 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.

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.

Input / Output Format

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)

Examples

Input:
153
Output:
Armstrong Number

Example Solution (Public)

C++
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;
    }
}

Official Solution Code

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;
    }
}
Please login to submit solutions.
Editor
Output

                                        
Please login to submit solutions.