Tutorialspoint
Problem
Solution
Submissions

Armstrong Number Check

Certification: Basic Level Accuracy: 33.33% Submissions: 6 Points: 10

Write a C++ program to determine if a given non-negative integer is an Armstrong number. An Armstrong number is a number that is equal to the sum of its digits each raised to the power of the number of digits.

Example 1
  • Input: number = 153
  • Output: 1
  • Explanation:
    • Step 1: Count the number of digits in 153. There are 3 digits.
    • Step 2: Calculate the sum of each digit raised to the power of the number of digits: 1^3 + 5^3 + 3^3 = 1 + 125 + 27 = 153.
    • Step 3: Since the sum equals the original number, 153 is an Armstrong number, so return 1.
Example 2
  • Input: number = 123
  • Output: 0
  • Explanation:
    • Step 1: Count the number of digits in 123. There are 3 digits.
    • Step 2: Calculate the sum of each digit raised to the power of the number of digits: 1^3 + 2^3 + 3^3 = 1 + 8 + 27 = 36.
    • Step 3: Since the sum does not equal the original number, 123 is not an Armstrong number, so return 0.
Constraints
  • 0 ≤ number ≤ 10^9
  • Output 1 if the number is an Armstrong number, 0 otherwise
  • Time Complexity: O(d), where d is the number of digits
  • Space Complexity: O(1)
NumberControl StructuresWiproDropbox
Editorial

Login to view the detailed solution and explanation for this problem.

My Submissions
All Solutions
Lang Status Date Code
You do not have any submissions for this problem.
User Lang Status Date Code
No submissions found.

Please Login to continue
Solve Problems

 
 
 
Output Window

Don't have an account? Register

Solution Hints

  • Calculate the number of digits in the given number.
  • For each digit, compute its value raised to the power of the number of digits and sum them.
  • Compare the computed sum to the original number to determine if it's an Armstrong number.

Steps to solve by this approach:

 Step 1: Handle edge cases (negative numbers and zero).

 Step 2: Store the original number for comparison later.
 Step 3: Count the number of digits in the number.
 Step 4: Calculate the sum of each digit raised to the power of the digit count.
 Step 5: Extract each digit using modulo and calculate its power.
 Step 6: Compare the sum with the original number.
 Step 7: Return true if they are equal, false otherwise.

Submitted Code :