Tutorialspoint
Problem
Solution
Submissions

Check Armstrong number

Certification: Basic Level Accuracy: 100% Submissions: 2 Points: 5

Write a C# program to determine whether a given number is an Armstrong number. An Armstrong number (also known as a narcissistic number, pluperfect digital invariant, or pluperfect number) is a number that is equal to the sum of its own digits each raised to the power of the number of digits.

Example 1
  • Input: n = 153
  • Output: true
  • Explanation:
    • 153 has 3 digits, so we need to check if 1^3 + 5^3 + 3^3 = 153
    • 1^3 = 1
    • 5^3 = 125
    • 3^3 = 27
    • 1 + 125 + 27 = 153
    • Since the sum equals the original number, 153 is an Armstrong number.
Example 2
  • Input: n = 1634
  • Output: true
  • Explanation:
    • 1634 has 4 digits, so we need to check if 1^4 + 6^4 + 3^4 + 4^4 = 1634
    • 1^4 = 1
    • 6^4 = 1296
    • 3^4 = 81
    • 4^4 = 256
    • 1 + 1296 + 81 + 256 = 1634
    • Since the sum equals the original number, 1634 is an Armstrong number.
Constraints
  • 1 ≤ n ≤ 10^9
  • Time Complexity: O(log n)
  • Space Complexity: O(1)
NumberVariables and Data TypesWalmartPhillips
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

  • Convert the number to a string to easily count the number of digits.
  • Alternatively, use mathematical operations to extract digits.
  • Use a loop to calculate the sum of each digit raised to the power of the number of digits.
  • Compare the final sum with the original number.


Submitted Code :