Armstrong number the Python way

What is Armstrong number

Armstrong number of 3 digits is an integer such that the sum of the cubes of the digits is equal to the number itself.

Fun with Visual Studio

Let’s have fun generating Armstrong Number

 

We find the sum of the cube of  each digits

temp = num

while temp > 0:

digit = temp % 10

sum += digit ** 3

 

The Python logic

# Python program to check if the number provided by the user is an Armstrong number or not

 

# take input from the user

num = int(input(“Enter a number: “))

 

# initialise sum

sum = 0

 

# find the sum of the cube of each digit

temp = num

while temp > 0:

digit = temp % 10

sum += digit ** 3

temp //= 10

 

# display the result

if num == sum:

print(num,”is an Armstrong number”)

else:

print(num,”is not an Armstrong number”)

 

 

Open up Visual Studio

then open a Visual Studio project with Python

Screenshot (1390)

The program

Screenshot (1394)

Will try with 2 numbers

Let’s try 346

Screenshot (1391)

Screenshot (1392)

Now 371

It’s an Armstrong Number

Cubing numbers

3*3*3 + 7*7*7 + 1= 371 (Armstrong Number)

Screenshot (1393)

Conclusion

We will go through lots of tricky logic and simple one with Visual Studio in Python. We see it’s very easy to implement it.

 

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s