Skip to main content

Python program to check whether the given number is an armstrong or not.

 

Given an integer,Write a python program to check if an integer is an Armstrong or not.

Armstrong

A given a number is said to be armstrong if the number of order or length n is digit*n times and the sum is equal to the number given

example - 153 = 1*1*1 + 5*5*5 +3*3*3

Example:

1. 153 - Armstrong

2. 345 - Not armstrong

3. 1634 - Armstrong

Python Program

#Check whether the given integer is an armstrong or not
#function for armstrong
def armstrong(n):
    #convert into str to calculate the order of the digit
    a = str(n)
    temp,sum= n,0
    while temp>0:
        #modulo by 10 to get each and every digit of a number
        digit = temp%10
        #multiply each digit with the order of the digit and sum the values
        sum+=digit**len(a)
        temp//=10
    #Print the result
    print("Armstrong") if sum==n else print("Not armstrong")
n = int(input())
#calling the armstrong function
armstrong(n)

 

Input:

153

Output:

Armstrong

Input:

345

Output:

Not armstrong

Comments

Popular posts from this blog

Best office computer accesories

 Here are some of the best computer accesories for your office at an affordable price in India. 1. Oxyura Gaming Mouse Pad Click here to see the Mouse pad https://amzn.to/3pVY8KL 2. Zebronics Zeb-Alex Wired Usb optical mouse Click here to see and buy the Mouse https://amzn.to/3ILT2t6 3. Usb wired keyboard Click here to see and buy the keyboard https://amzn.to/3mmzjqv 4.Laptop stand Click here to see and buy the laptop stand https://amzn.to/3dTgkih All link are from amazon so go ahead and check it out it is at an afordable price.

Top 5 mistake i made as a computer science engineer.

Top 5 mistake i made as a computer science engineer In this blog i will walk you through my mistake as a computer science engineer Yes as a person we make a lot of mistake and for me as a computer science engineer i have made a lot of mistake but in this blog I'm going to share my top 5 mistake. 1. Involve in a tutorial hell Tutorial hell is a way of learning in which we get stuck in a tutorial without learning outside of the tutorial, it is like learning from the tutorial and build the exact same project which the instructor have taught us and also it may be just copy and paste the code, after completing the tutorial we start another tutorial and repeat the same thing again and again, without adding our feature to the project or build it without any help from the instructor. 2. Trying to learn everything Yes as a computer science engineer student i try to learn everything and it is a huge mistake because i learn a bit of python and then jump to javascript and then a bit of javascr...

Python programe to find the factorial of a number

 Python program to find the factorial of a number Factorial number of a number is a product from 1 to the number which is from 1 to n where n is the number Example : factorial of 5 = 1*2*3*4*5 = 120 Code: #function to find factorial number def facto(n):     factorial = 1     for i in range(1,n+1):         factorial = factorial * i     return factorial #Take the input from the user and store in n n = int(input()) if n == 0: #factorial of 0 is always 1     print(1) else:     print(facto(n)) #other than 0 we have to find the factorial number by calling the function Input : 5 Output : 120 There is another method to find factorial number using recursion. Feel free to check the code in a code editor and try it out and also i am welcome to any suggestion. Here is the previous post : https://shotwithcode.blogspot.com/2020/12/python-automation-with-sele...