Python Program to Find Armstrong Number codigence

Python Program to Find Armstrong Number codigence

Python Program to Find Armstrong Number codigence

  • Python Program to find Armstrong Number using For Loop.
  • Armstrong Number in Python using While Loop.

Overview of Armstrong Number in Python

A positive integer of n digits is called an Armstrong number of order n (order is number of digits) if. 

abcd… = pow(a,n) + pow(b,n) + pow(c,n) + pow(d,n) + …. 

All the 1 digit numbers (1-9) are Armstrong number because

1*1=1 which is equals to number (1) itself,

2*1=2 which is equals to number(2) itself so on for all the 1 digit numbers (1-9).

There are no 2 digit Armstrong numbers.

An Armstrong number of three digit is a number such that that sum of the cubes of it's digits is equal to the number itself. so to check if a 3 digit number is an armstrong number or not we have to multiply all the three digits with itself 3 times.

For example 153 is an Armstrong number because cube of 1 is 1(1x1x1=1) + cube of 5 is 125(5*5*5=125) + cube of 3 is 27(3*3*3=27). Now add all the cubes 1+125+27=153 which is equals to number itself

Python Program to find Armstrong Number using For Loop

  • Define a function to check Armstrong No.
  • Let's have a function named 'is_armstrong' and take the argument 'no' for number to check.
  • Now Inside the Function,
  • copy the value of no into a temporary variable,say dup_no.
  • Now get the no. of digits in a variable (to get the power 'n' for digits) say, no_of_digits.
  • Now initialize a variable,say 'sum' to zero.This will store the sum of d^n.
  • Using for loop calculate the sum.
  • Use if statement to check if number is equal to sum or not.If equal then return True else return False.
  • Now, Using for loop check all armstrong numbers below 10000
  • Print the armstrong numbers.

#python program to check armstrong no

#using for loop
def is_armstrong(no: int):
    dup_no = no #duplicating no
    no_of_digits = len(str(no)) #no of digits
    sum = 0
    for i in range(no_of_digits):
        digit = dup_no % 10
        sum+=digit**no_of_digits
        dup_no//=10
    if(sum == no):
        return True
    else:
        return False

#printing all armstrong no below 10000
print("Armstrong numbers below 10000 are:")
for i in range(1,10000):
    if(is_armstrong(i)):
        print(i)

Output


Armstrong numbers below 10000 are:
1
2
3
4
5
6
7
8
9
153
370
371
407
1634
8208
9474

Python Program to find Armstrong Number using While Loop

  • Define a function to check Armstrong No.
  • Let's have a function named 'is_armstrong' and take the argument 'no' for number to check.
  • Now Inside the Function,
  • copy the value of no into a temporary variable,say dup_no.
  • Now get the no. of digits in a variable (to get the power 'n' for digits) say, no_of_digits.
  • Now initialize a variable,say 'sum' to zero.This will store the sum of d^n.
  • Using While loop calculate the sum.
  • Use if statement to check if number is equal to sum or not.If equal then return True else return False.
  • Now, Using for loop check all armstrong numbers below 10000
  • Print the armstrong numbers.

#using while loop
def is_armstrong(no: int):
    dup_no = no #duplicating no
    no_of_digits = len(str(no)) #no of digits
    sum = 0
    while(dup_no > 0):
        digit = dup_no % 10
        sum+=digit**no_of_digits
        dup_no//=10
    if(sum == no):
        return True
    else:
        return False

#printing all armstrong no below 10000
print("Armstrong numbers below 10000 are:")
for i in range(1,10000):
    if(is_armstrong(i)):
        print(i)

Output


Armstrong numbers below 10000 are:
1
2
3
4
5
6
7
8
9
153
370
371
407
1634
8208
9474

Conclusion

A Armstrong number is a mathematical concept that has its own applications. Python allows us to implement armstrong number checking programs quickly and effectively through its multiple features.