TheDeveloperBlog.com

Home | Contact Us

C-Sharp | Java | Python | Swift | GO | WPF | Ruby | Scala | F# | JavaScript | SQL | PHP | Angular | HTML

Python Check Armstrong Number

Python Check Armstrong Number for beginners and professionals with programs on basics, controls, loops, functions, native data types etc.

<< Back to PYTHON

Python Program to check Armstrong Number

Suppose we have a given number, "K", and we have to check whether the given number is an Armstrong number or not. In this tutorial, we will learn how to identify the given number is an Armstrong number or not using Python.

Definition:

A positive number of "p" number of digits is called an Armstrong number of order n (order is the number of digits) if:

abcde? = pow(a, p) + pow(b, p) + pow(c, p) + pow(d, p) + pow(e, p) + ?

Example:

Input:  407
Output: Yes, 407 is an Armstrong number
Solution:
(4 * 4 * 4) + (0 * 0 * 0) + (7 * 7 * 7) = 407 
Input:  1634
Output: Yes, 1634 is an Armstrong number
Solution:
(1 * 1 * 1) + (6 * 6 * 6) + (3 * 3 * 3) + (4* 4 * 4) = 1634
Input:  253
Output: No, 253 is not an Armstrong number
Solution:
(2 * 2 * 2) + (5 * 5 * 5) + (3 * 3 * 3) = 160
Input:  1
Output: Yes, 1 is an Armstrong number
Solution:
(1* 1) = 1

Example:

def power_1(A, B):
      
    if B == 0:
        return 1
    if B % 2 == 0:
        return power_1(A, B // 2) * power(A, B // 2)
          
    return A * power(A, B // 2) * power(A, B // 2)
  
# Function for calculating "order of the number"
def order_1(A):
  
    # Variable for storing the number
    N = 0
    while (A != 0):
        N = N + 1
        A = A // 10
          
    return N
  
# Function for checking if the given number is Armstrong number or not
def is_Armstrong(A):
      
    N = order_1(A)
    temp_1 = A
    sum_1 = 0
      
    while (temp_1 != 0):
        R_1 = temp_1 % 10
        sum_1 = sum_1 + power_1(R_1, N)
        temp_1 = temp_1 // 10
  
    # If the above condition is satisfied, it will return the result
    return (sum_1 == A)
  
# Driver code
A = int(input("Please enter the number to be checked: "))
print(is_Armstrong(A))

Output:

1#
Please enter the number to be checked:  0
True
2#
Please enter the number to be checked:  123
False
3#
Please enter the number to be checked:  1634
True

Conclusion

In this tutorial, we discussed how to verify whether the given number is an Armstrong number or not.






Related Links:


Related Links

Adjectives Ado Ai Android Angular Antonyms Apache Articles Asp Autocad Automata Aws Azure Basic Binary Bitcoin Blockchain C Cassandra Change Coa Computer Control Cpp Create Creating C-Sharp Cyber Daa Data Dbms Deletion Devops Difference Discrete Es6 Ethical Examples Features Firebase Flutter Fs Git Go Hbase History Hive Hiveql How Html Idioms Insertion Installing Ios Java Joomla Js Kafka Kali Laravel Logical Machine Matlab Matrix Mongodb Mysql One Opencv Oracle Ordering Os Pandas Php Pig Pl Postgresql Powershell Prepositions Program Python React Ruby Scala Selecting Selenium Sentence Seo Sharepoint Software Spellings Spotting Spring Sql Sqlite Sqoop Svn Swift Synonyms Talend Testng Types Uml Unity Vbnet Verbal Webdriver What Wpf