Sitemap
Vipul Kaitke

Vipul Kaitke

Developer

Armstrong problem in Python

3 min readFeb 13, 2024
  • Write a program that will check whether the number is armstrong number or not.

So by default there is easy to solve 3 digit problem or 4 digit problem.

I’m here to explain you guys how to solve same problem in nth terms, so to solve following problem in nth terms there are two methods like by converting given number to string so basically string is a datatype in python and calculate it’s length and give the formula like

sum = sum + (r** num_str)

And by

num = 1634
len_digit = 0
while num > 0:
len_digit = len_digit + 1
num = num // 10
print(len_digit)

this method also but interesting thing is like if we are solving the same problem in various methods we have built our own logic in various kinds, so basically i was solving this problem since morning and I’m posting an article in night so you guys can check how much time I have needed to solve this problem…….

We are going to discuss problem which I have solved in both the matters.

num = int(input("Enter any number : "))
temp = num
num_str = str(num)
sums = 0
num_digits = len(num_str)
while (num > 0):
r = num % 10
sums = sums + (r** num_digits)
num = num//10
if (temp==sums):
print("Armstrong Number:")
else:
print("Not Armstrong Number")

Here is break down of the above code

Get Vipul Kaitke’s stories in your inbox

Join Medium for free to get updates from this writer.

The user is prompted to enter a number using input().
The entered number is stored in the variable num as an integer.
The variable temp is used to store the original value of num for later comparison.
The number is converted to a string (num_str) to find the number of digits using len().
The variable sums is initialized to 0 to store the sum of each digit raised to the power of the number of digits.
A while loop is used to iterate through each digit of the number.
Inside the loop, the remainder (r) when the number is divided by 10 is calculated, and sums is updated accordingly.
The number is then divided by 10 (using num = num // 10) to move on to the next digit in the next iteration.
After the while loop, the original value of num is compared with the calculated sum (sums) to determine if it’s an Armstrong number.
If they are equal, it prints “Armstrong Number,” otherwise, it prints “Not Armstrong Number.”

Here’s an example of how the program works:

Enter any number : 153
Armstrong Number
In this example, 153 is an Armstrong number because 1³ + 5³ + 3³ = 153.

Next Code is like as follows…..

num = int(input("Enter any number : "))
sums = 0
temp = num
len_digit = 0

while (num > 0):
len_digit += 1
num = num // 10
num = temp

print(len_digit)
print (num)
while (num > 0):
r = num % 10
sums = sums + (r** len_digit)
num = num//10
if (temp==sums):
print("Armstrong Number:")
else:
print("Not Armstrong Number")

Here’s a breakdown of the modified code:

The user is prompted to enter a number using input().
The entered number is stored in the variable num as an integer.
The variable temp is used to store the original value of num for later comparison.
The variable len_digit is initialized to 0 and is used to count the number of digits in the number.
The first while loop is used to count the number of digits in the given number (len_digit) by continuously dividing the number by 10 until it becomes 0.
After the loop, the original value of num is restored.
The second while loop is similar to the previous code, calculating the sum of each digit raised to the power of the number of digits.
Finally, it compares the original value of num with the calculated sum (sums) to determine if it’s an Armstrong number and prints the result.
Here’s an example of how the modified program works:

Enter any number : 1634
4
1634
Armstrong Number

In this example, 1634 is an Armstrong number because 1⁴ + 6⁴ + 3⁴ + 4⁴ = 1634.

But now using this two methods we can solve any digit numbers.

#VEV CODE LAB

--

--