Welcome back, In this tutorial, we are going to learn how to write a program in Python to check whether a given input number is Armstrong or not.
So before moving directly on to the writing program, We will first know about what is Armstrong number.
What is Armstrong Number?
- It is a number that is equal to the sum of its own digits raised to the power of the number of digits.
- For eg: 153, is an Armstrong number because it has 3 digits, and 1^3 + 5^3 + 3^3 = 1 + 125 + 27 = 153
Lets see it practically by calculating it,
Suppose I am taking 153 for an example
First calculate the cube of its each digits
1^3 = (1*1*1) = 1
5^3 = (5*5*5) = 125
3^3= (3*3*3) = 27
Now add the cube
1+125+27 = 153
It means 153 is an Armstrong number.
Some examples of Armstrong numbers
- 153 is an Armstrong number because:
- 1^3 + 5^3 + 3^3 = 153
- 371 is an Armstrong number because:
- 3^3 + 7^3 + 1^3 = 371
- 407 is an Armstrong number because:
- 4^3 + 0^3 + 7^3 = 407
- 1634 is an Armstrong number because:
- 1^4 + 6^4 + 3^4 + 4^4 = 1634
- 8208 is an Armstrong number because:
- 8^4 + 2^4 + 0^4 + 8^4 = 8208
In this post, we will explore different logic to write a Python program that checks if a given number is an Armstrong number.
Python Program to check Number is Armstrong or not
Program 1: Check Armstrong Using a While Loop
In this method, we are using a while loop to extract each digit of the input number. Also, all the power calculation of a number and sum is done in this loop. After calculating the sum, We then compare the total sum to the input number. If a match is found then the input is an Armstrong number other not an Armstrong number. Below is the code:
# Armstrong number program using while loop
num = int(input("Please give a number: "))
sum = 0
temp = num
# count the number of digits in input
count = len(str(num))
# loop on each digit and calculate the sum
while temp > 0:
digit = temp % 10
sum += digit ** count
temp //= 10
# check if the number is an Armstrong or not
if num == sum:
print("Given ",num, "is an Armstrong number")
else:
print("Given ",num, "is not an Armstrong number")
Output:
Please give a number: 153
Given 153 is an Armstrong number
Explanation:
- In the above program, we first take the input from the user using the
input()
function. - After taking input, converted to an integer using the
int()
function. - We created a temporary variable
temp
and assigned an input number to it. Also taken and initializedsum
with 0. Computed the number of digits in the input number using thelen()
function. - Using a while loop to extract each digit of the input number by taking the modulo % of the number with 10. We then raise the digit to the power of the number of digits using the ** operator and add it to the running total.
- We remove the last digit of the input number using integer division
//
. We repeat this process until all the digits of the input number have been processed. - Finally, we compare the total
sum
to the input numbernum
to check if it is an Armstrong number. If they are equal, we print a “number is an Armstrong number” or else “number is not an Armstrong number”.
Program 2: Armstrong Using List Comprehension
In this method, we are using the list comprehension concept of Python to extract each digit of the input number. Also, it is used to find the power of the number of digits. To compute the sum we are using the sum()
function. Here’s the code:
# Armstrong number using list comprehension
num = int(input("Please Enter a Number: "))
digits = [int(digit) for digit in str(num)]
count = len(digits)
sum = sum([digit ** count for digit in digits])
if num == sum:
print("Given ", num, "is an Armstrong number")
else:
print("Given ", num, "is not an Armstrong number")
Output:
Please Enter a Number: 371
Given 371 is an Armstrong number
Explanation:
- The above code takes the input number from the user is converted it to an integer using the
int()
function. - We have used list comprehension to extract each digit of the input number by converting it to a string using the
str()
function. After that, we convert each character back to an integer using theint()
function. - We computed the number of digits in the input number. Also, we stored the digits in a list called digits. After that, we computed the sum of the digits raised to the power of the number of digits using list comprehension and the
sum()
function. - After computation, we compare the final sum output to the input number. If they are equal, then print number is an Armstrong number otherwise print number is not an Armstrong number.
Conclusion:
In this post, we have seen various approach to writing Python program that checks if a given number is an Armstrong number. The first method uses a while loop, and the second method uses list comprehension. Hope you loved this tutorial.
Similar Reads
-
Find middle element of a linked list in single pass
Linked lists are a fundamental data structure used in computer science for organizing and managing data. A linked list is… -
Python Program to Find Last 3rd element in Singly Linked List
In this tutorial, we are going to learn the writing python program to Find 3rd element of Linked List from… -
Most important JavaScript Interview Questions To Prepare
In this Page we have collected and explained Most important Javascript Interview Questions and Answers for begineers, freshers as well… -
Sum of digits of Given Number in Java
In this tutorial we will learn writing Java program to calculate the sum of its digit. We will also see… -
Hibernate Interview Questions for 2+ years of experience
Certainly! Here's a list of commonly asked interview questions on Hibernate for candidates with 2+ years of experience: Basic Hibernate… -
68 Most Important Microservices Interview Questions
Certainly, here's an extended list of 50 commonly asked interview questions on microservices for candidates with 2+ years of experience:… -
60 Most Important Git Interview Questions
Certainly! Here is a list of commonly asked interview questions on Git for candidates with fresher or having of experience… -
50+ Most important Java Interview Questions for 5+ Years Exp
1. Explain the SOLID principles in Java. Provide examples of how you have applied these principles in your projects. SOLID… -
60+ Spring Boot interview questions for 4+ years Exp.
1. What is Spring Boot and how does it differ from the Spring framework? Spring Boot is a framework designed… -
60+ Mostly Asked Spring Boot Interview Questions for 3+ Yrs
Here is a list of 60+ Spring Boot interview questions for candidates with 3+ years of experience: 1. What is…