Tutorial Gateway

  • C
  • C#
  • Java
  • Python
  • SQL
  • MySQL
  • Js
  • BI Tools
    • Informatica
    • Talend
    • Tableau
    • Power BI
    • SSIS
    • SSRS
    • SSAS
    • MDX
    • R Tutorial
    • Alteryx
    • QlikView
  • More
    • C Programs
    • C++ Programs
    • Python Programs
    • Java Programs

Python Program For Armstrong Number

by suresh

How to Write Python Program For Armstrong Number Using While Loop, For Loop, Functions, and Recursion?. We also show you, Python program to print Armstrong Numbers between 1 to n.

Python Armstrong Number

If the given number is equal to the sum of the Nth power of each digit present in that integer, then that number can be an Armstrong Number in Python. For example, 370 is Armstrong Number.

Number of individual digits in 370 = 3

370 = 3³ + 7³ + 0³

= 27 + 343 + 0 = 370

The below steps show you the common approach to check for the Armstrong Number in Python

Python Armstrong Number Steps:

  1. The user has to enter any number.
  2. Count the Number of individual digits (For Example, 370 means 3).
  3. Divide the given number into individual digits (For Example, Divide 370 into 3, 7, and 0).
  4. Calculate the power of n for each individual and add those numbers.
  5. Compare the original value with Sum value.
  6. If they exactly matched, then it is an Armstrong number else it is not Armstrong.

Python Program for Armstrong Number Using While Loop

This Python program allows the user to enter any positive integer. And then, this Python program checks whether a number is Armstrong Number or Not using the While Loop

# Python Program For Armstrong Number using While Loop

Number = int(input("\nPlease Enter the Number to Check for Armstrong: "))

# Initializing Sum and Number of Digits
Sum = 0
Times = 0
           
# Calculating Number of individual digits
Temp = Number
while Temp > 0:
           Times = Times + 1
           Temp = Temp // 10

# Finding Armstrong Number
Temp = Number
while Temp > 0:
           Reminder = Temp % 10
           Sum = Sum + (Reminder ** Times)
           Temp //= 10
if Number == Sum:
           print("\n %d is Armstrong Number.\n" %Number)
else:
           print("\n %d is Not a Armstrong Number.\n" %Number)

OUTPUT

Python Program for Armstrong Number Using While Loop

ANALYSIS

This Python Armstrong number program allows the user to enter any positive integer and then, that number assigned to variable Number.

Next, We assign the original value to the Temp variable. It helps to preserve our original value and then do all the manipulation on the Temp variable.

Below While Loop makes sure that the given number is greater than 0, statements inside the while loop split the numbers and count number of individual digits inside the given number. If you don’t understand the Python program logic, Please refer Python Program to find Number Of Digits in a Number article.

while Temp > 0:
           Times = Times + 1
           Temp = Temp // 10

The Python Second While loop makes sure that the given number is greater than 0. Let us see the working principle of this while loop in iteration wise

while Temp > 0: Reminder = Temp % 10 Sum = Sum + (Reminder ** Times) Temp //= 10

User Entered value for this Python Program For Armstrong Number : Number = 9474 and Sum = 0

Temp = Number
Temp = 9474

First Iteration

Reminder = Temp %10
Reminder = 9474 % 10 = 4

Sum = Sum + pow (Reminder, Times)

For this Python example, Times =4 because number of digits in 9474 = 4. So,

Sum = Sum + (Reminder * Reminder * Reminder * Reminder)
Sum = 0 + (4 * 4 * 4 * 4)
Sum = 0 + 256
Sum = 256

Temp = Temp /10
Temp = 9474 /10
Temp = 947

NOTE: If the number of digits count is 5, then Reminder multiplied by 5 times.

Second Iteration

From the Python Program For Armstrong Number first Iteration, the values of both Temp and Sum changed as Temp = 163 and Sum = 256

Reminder = Temp %10
Reminder = 947 % 10 = 7

Sum = 256 + (7 * 7 * 7 * 7)
Sum = 256 + 2401
Sum = 2657

Temp = Temp /10
Temp = 163 /10
Temp = 94

Third Iteration

From the Third Iteration, the values of Temp = 94 and Sum = 2657.

Reminder = Temp %10
Reminder = 94 % 10 = 4

Sum = 2657 + (4 * 4 * 4 * 4)
Sum = 2657 + 256
Sum = 2913

Temp = Temp /10
Temp = 94 /10
Temp = 9

Python Program For Armstrong Number Fourth Iteration

From the Fourth Iteration, the values of Temp = 9 and Sum = 2913

Reminder = Temp %10
Reminder = 9 % 10 = 0

Sum = Sum + (Reminder * Reminder * Reminder * Reminder)

Sum = 2913 + (9 * 9 * 9 * 9)
Sum = 2913 + 6561
Sum = 9474

Temp = Temp /10
Temp = 9/10
Temp = 0

Here Number = 0, so the while loop condition fails.

if ( Number == Sum ) – Condition check whether the user enter number is exactly equal to Sum number or not. If this condition is True, then it is Armstrong else the given number is not Armstrong number.

Let me check if ( Number == Sum )

if(9474 == 9474) –TRUE, Means Armstrong

NOTE: If you are finding the Python Armstrong number below 1000, remove the while loop to count the number of digits in a number, and then replace the below code

Sum = Sum + (Reminder ** Times);

With

Sum = Sum + (Reminder * Reminder * Reminder)

Python Program for Armstrong Number Using For Loop

This Python program allows the user to enter any positive integer and then, this program checks whether a number is Armstrong Number or Not using Python For Loop

# Python Program For Armstrong Number using For Loop

Number = int(input("\nPlease Enter the Number to Check for Armstrong: "))

# Initializing Sum and Number of Digits
Sum = 0
Times = 0
           
# Calculating Number of individual digits
Temp = Number
while Temp > 0:
           Times = Times + 1
           Temp = Temp // 10

# Finding Armstrong Number
Temp = Number
for n in range(1, Temp + 1):
           Reminder = Temp % 10
           Sum = Sum + (Reminder ** Times)
           Temp //= 10

if Number == Sum:
           print("\n %d is Armstrong Number.\n" %Number)
else:
           print("\n %d is Not a Armstrong Number.\n" %Number)

OUTPUT

Python Program for Armstrong Number Using For Loop

We just replaced the While loop in the above Python Armstrong number example with the For loop. If you don’t understand the for loop, then please refer For Loop article here: Python For Loop

Python Program for Armstrong Number Using Functions

This Python program allows the user to enter any positive integer. Then, this program checks whether a number is Armstrong Number or Not using Functions

# Python Program For Armstrong Number using Functions

def Armstrong_Number(Number):
           # Initializing Sum and Number of Digits
           Sum = 0
           Times = 0

           # Calculating Number of individual digits
           Temp = Number
           while Temp > 0:
                      Times = Times + 1
                      Temp = Temp // 10

           # Finding Armstrong Number
           Temp = Number
           for n in range(1, Temp + 1):
                      Reminder = Temp % 10
                      Sum = Sum + (Reminder ** Times)
                      Temp //= 10
           return Sum
#End of Function

#User Input
Number = int(input("\nPlease Enter the Number to Check for Armstrong: "))

if (Number == Armstrong_Number(Number)):
    print("\n %d is Armstrong Number.\n" %Number)
else:
    print("\n %d is Not a Armstrong Number.\n" %Number)

OUTPUT

Python Program for Armstrong Number Using Functions

ANALYSIS

In this Python Program For Armstrong Number example, we defined the following function to perform all the necessary calculations and return Sum.

def Armstrong_Number(Number):

When the compiler reaches the following code inside the If statement then, the compiler immediately jumps to the above-specified function.

Armstrong_Number(Number)

We already explained the LOGIC above example.

Python Program for Armstrong Number Using Recursion

This program allows us to enter any positive integer. Next, this Python program checks whether a number is Armstrong Number or Not using Recursion concept.

# Python Program For Armstrong Number using Recursion

# Initializing Number of Digits
Sum = 0
Times = 0

# Calculating Number of individual digits
def Count_Of_Digits(Number):
           global Times
           if(Number > 0):
                      Times = Times + 1
                      Count_Of_Digits(Number // 10)
           return Times
#End of Count Of Digits Function

# Finding Armstrong Number
def Armstrong_Number(Number, Times):
           global Sum
           if(Number > 0):
                      Reminder = Number % 10
                      Sum = Sum + (Reminder ** Times)
                      Armstrong_Number(Number //10, Times)
           return Sum
#End of Armstrong Function

#User Input
Number = int(input("\nPlease Enter the Number to Check for Armstrong: "))

Times = Count_Of_Digits(Number)
Sum = Armstrong_Number(Number, Times)
if (Number == Sum):
    print("\n %d is Armstrong Number.\n" %Number)
else:
    print("\n %d is Not a Armstrong Number.\n" %Number)

OUTPUT

Python Program for Armstrong Number Using Recursion

ANALYSIS

In this Python Program for Armstrong Number example, we defined two recursive functions. The following function accept integer values as parameter value and count the number of individual digits in a number recursively.

def Count_Of_Digits(Number):

The following function accepts two integer values as parameter values. And it performs all the necessary calculations and return Sum.

def Armstrong_Number(Number, Times):

The following Statement help to call the function Recursively with updated value. If you miss this statement, then after completing the first line it will terminate. For example,

Armstrong_Number(Number //10, Times)

Number = 153

Then the output = 27

Let’s see the If statement inside the above-specified functions

if (Number > 0) check whether the number is greater than 0 or not. For Recursive functions, it is essential to place a condition before using the function recursively. Otherwise, we end up in infinite execution (Same like infinite Loop).

Please be careful :)

Python Program to Find Armstrong Numbers between the 1 to n

This program allows you to enter minimum and maximum values. This Python program returns Armstrong Numbers between the Minimum and Maximum values.

# Python Program to Find Armstrong Numbers between the 1 to n

Minimum = int(input("Please Enter the Minimum Value: "))
Maximum = int(input("\n Please Enter the Maximum Value: "))

for Number in range(Minimum, Maximum + 1):
           # Initializing Sum and Number of Digits
           Sum = 0
           Times = 0
           
           # Calculating Number of individual digits
           Temp = Number
           while Temp > 0:
                      Times = Times + 1
                      Temp = Temp // 10

           # Finding Armstrong Number
           Temp = Number
           while Temp > 0:
                      Reminder = Temp % 10
                      Sum = Sum + (Reminder ** Times)
                      Temp //= 10
           if Number == Sum:
                      print(Number)

OUTPUT

Python Program to Find Armstrong Numbers between the 1 to n

ANALYSIS:

The following statements present in this Python Armstrong number program allows the user to enter minimum and maximum values.

Minimum = int(input("Please Enter the Minimum Value: "))
Maximum = int(input("\n Please Enter the Maximum Value: "))

The Python for Loop helps to iterate between Minimum and Maximum Variables. Iteration starts at the Minimum, and then it won’t exceed the Maximum variable.

for Number in range(Minimum, Maximum + 1):

if(Number == Sum) -– condition check whether the sum of the power N for each digit present in that integer is equal to a given number or not. When the condition is True, it is an Armstrong else the given number is not Armstrong number in Python.

If this condition is True, the below statement printed.

print(Number)

Placed Under: Python Examples

  • Python Hello World Program
  • Python add 2 numbers Program
  • Python Arithmetic Operations
  • Python Calendar Example
  • Python Cube of a Number
  • Python Calculate Electricity Bill
  • Python Calculate Simple Interest
  • Python Compound Interest
  • Python Largest of Two Numbers
  • Python Largest of 3 numbers
  • Python Print Natural Numbers
  • Python natural numbers reverse
  • Python Leap Year Program
  • Python Odd or Even Program
  • Python Even Numbers 1 to N
  • Python Odd Numbers 1 to N
  • Python Positive or Negative num
  • Python Profit or Loss Program
  • Python Square of a Number
  • Python Square root of a Number
  • Python Number Divisible by 5, 11
  • Python Find Power of a Number
  • Python Print Multiplication Table
  • Python Quadratic Equation roots
  • Python Student Grade Program
  • Python Total, Average, and Percentage of 5 Subjects
  • Python Sum of G.P Series
  • Python Sum of A.P Series
  • Python Sum of Series 1³+2³+.+n³
  • Python Sum of Series 1²+2²+.+n²
  • Python Natural num Sum & Avg
  • Python Sum of N natural nums
  • Python Sum of Odd Numbers
  • Python Sum of Even Numbers
  • Python Sum of Even & Odd
  • Python Armstrong number
  • Python Count Digits in a Number
  • Python Fibonacci Series program
  • Python Factorial of a Number
  • Python Factors of a Number
  • Python First Digit of a Number
  • Python GCD of Two Numbers
  • Python Strong Number Program
  • Python Prime Number Program
  • Python Prime Numbers 1 to 100
  • Python LCM of Two Numbers
  • Python natural number in reverse
  • Python Palindrome Program
  • Python Palindrome nums 1-100
  • Python find Perfect Number
  • Python Prime Factors of Number
  • Python Reverse number program
  • Python Strong Number Program
  • Python Strong Numbers 1 to 100
  • Python Sum of Digits of Number
  • Python Swap Two Numbers
  • Python Alphabet or not Program
  • Python Alphabet or Digit
  • Python Digit or not program
  • Python Lowercase or not
  • Python Uppercase or not
  • Python Lowercase or Uppercase
  • Python Vowel or Consonant
  • Python Alphabet digit or special
  • Python ASCII Value of Character
  • Python ASCII String Chars
  • Python Concatenate Strings
  • Python Convert String to Upper
  • Python Convert String to Lower
  • Python Copy a String Program
  • Python Count string words using Dictionary
  • Python Count Alphabets Digits and Special Characters in String
  • Python String Count Vowels and Consonants
  • Python Count Vowels in a String
  • Python Count total string chars
  • Python Count Char Occ in String
  • Python Count Total String words
  • Python Last Char Occur in String
  • Python First Char Occur in String
  • Python String Find All Char Occur
  • Python Palindrome String
  • Python Print String Characters
  • Python Replace Blank Space with Hyphen in a String
  • Python Replace String character
  • Python remove Odd string Chars
  • Python String Remove Odd Index Chars
  • Python Remove Last Char Occurrence in a String
  • Python Remove First Char Occurrence in a String
  • Python Reverse a String Program
  • Python String Length Program
  • Python Toggle String Char Case
  • Python List Arithmetic Operation
  • Python Program to Add two Lists
  • Python Count List +Ve & -Ve num
  • Python Even & Odd List nums
  • Python 2nd Largest List Number
  • Python Large & Small List Num
  • Python Largest Number in a List
  • Python List Length
  • Python List Negative Numbers
  • Python List Positive Numbers
  • Python Odd Numbers in a List
  • Python Even Numbers in a List
  • Python Print Elements in a List
  • Python Put Positive and Negative Numbers in Separate List
  • Python Program to Put Even and Odd Numbers in Separate List
  • Python Program to Reverse List
  • Python Sort List in Ascending
  • Python Smallest Number in a List
  • Python Sum of List Even & Odd
  • Python Sum of List Elements
  • Python add key-valuepair to Dict
  • Python Map 2 lists to dictionary
  • Python Create Dictionary of Numbers 1 to n in (x, x*x) form
  • Python Create Dictionary of keys and values are square of keys
  • Python key exists in Dictionary
  • Python remove dictionary Key
  • Python multiply dictionary items
  • Python Sum of Dictionary Items
  • Python Merge Two Dictionaries
  • Python Area Of Circle
  • Python Circle Area, Diam Circumf
  • Python Area Of a Triangle
  • Python Area of Tri-base,height
  • Python Area of a Trapezoid
  • Python Equilateral Triangle area
  • Python Area of a Rectangle
  • Python Area of Rect use len,width
  • Python right angle triangle area
  • Python Cylinder Vol & Surf Area
  • Python Cube Volume & Surface
  • Python Cone Volume & Surface
  • Python Cuboid volume, surface
  • Python check Triangle is Valid
  • Python Print Floyd’s Triangle
  • Python Invert Right Triangle Star
  • Python Program for Bubble Sort
  • C Tutorial
  • C# Tutorial
  • Java Tutorial
  • JavaScript Tutorial
  • Python Tutorial
  • MySQL Tutorial
  • SQL Server Tutorial
  • R Tutorial
  • Power BI Tutorial
  • Tableau Tutorial
  • SSIS Tutorial
  • SSRS Tutorial
  • Informatica Tutorial
  • Talend Tutorial
  • C Programs
  • C++ Programs
  • Java Programs
  • Python Programs
  • MDX Tutorial
  • SSAS Tutorial
  • QlikView Tutorial

Copyright © 2021 | Tutorial Gateway· All Rights Reserved by Suresh

Home | About Us | Contact Us | Privacy Policy