Python Program to Check Armstrong

Python Armstrong Number Program
Python Armstrong Number Program

Problem Statement

Write a program to determine if the given number is an Armstrong number or not. Print true if the number is Armstrong, otherwise print false.

Input Format

 Integer N

Output Format

true or false

Examples

Sample Input 1 :

9474

Sample Output 1 :

true

Sample Input 2 :

123

Sample Output 2 :

false

Solution Approach

An Armstrong number of order n is a number that is equal to the sum of its own digits each raised to the power n. Steps:

  1. Read the number N.
  2. Find the total number of digits.
  3. Compute the sum of digits raised to the power of digit count.
  4. If the sum equals the original number, it's an Armstrong number.

Python Code


N = int(input())

# Step 1: Count digits
digits = len(str(N))

# Step 2: Compute Armstrong sum
total = 0
temp = N
while temp > 0:
    d = temp % 10
    total += d ** digits
    temp //= 10

# Step 3: Compare with original number
if total == N:
    print("true")
else:
    print("false")

Try it Online

Previous Post Next Post