Armstrong number(after Michael F. Armstrong) or a plus perfect number)
Armstrong is a number that is the sum of its own digits each raised to the power of
the number of digits.
Example :
Given Number : 153 (It is a Three Digit Number)
=> (1*1*1) + (5*5*5) + (3*3*3) -> Sum of Cubes of its digits
=> 1 + 125 + 27
=> 153
153 is an Armstrong Number Example :
Given Number : 371 (It is a Three Digit Number)
=> (3*3*3) + (7*7*7) + (1*1*1) -> Sum of Cubes of its digits
=> 27 + 343 + 1
=> 371
371 is an Armstrong Number Example :
Given Number : 1634 (It is a Three Digit Number)
=> (1*1*1*1) + (6*6*6*6) + (3*3*3*3)+(4*4*4*4) -> sum of digits each raised to the power of
the number of digits.
=> 1 + 81 + 1296 + 256
=> 1634
1634 is an Armstrong Number To check if a number is an Armstrong or Not in Python
you can use the following algorithm:
- Read the input number.
- Create a copy of the input number.
- Initialize a variable sum to 0.
- While the number is not equal to 0, perform the following steps:
- Get the last digit of the number by using the modulo operator (%).
- Raise the last digit to the power three (3).
- Add the result to the sum.
- Divide the number by 10 to remove the last digit.
- Check if the sum is equal to copy of the original number.
- If they are equal, the number is an Armstrong Number.
Otherwise, it is not. - Print the result.
In this algorithm raises each digit to the power of three(3) and adds them to the sum. Finally, it checks if the sum is equal to the copy of the original number to determine if it is an Armstrong Number.
Please note that: this implementation assumes that the input number is a positive integer. You may need to add additional checks for negative numbers or other cases depending on your requirements.
Source Code
num = int( input("Enter a Number To Check : "))
Sum = 0
ncopy=num
while num!=0:
Rd = num % 10
Sum = Sum + Rd * Rd * Rd
num = num // 10
print("Sum of Digits(each raised to the power of the number of digits) : ",Sum)
if ncopy==Sum:
print("It is an Armstrong Number . ")
else:
print("It is Not an Armstrong Number . ")
Output
Sample Input :
Enter a Number To Check : 153
Sample Output:
Sum of Digits(each raised to the power of the number of digits) :153
It is an Armstrong Number
Sample Input :
Enter a Number To Check : 125
Sample Output:
Sum of Digits(each raised to the power of the number of digits) :134
It is Not an Armstrong Number
To Check Any Digit Number Whether it is an Armstrong number or Not ?
To check if a number is an Armstrong or Not in Pythonyou can use the following algorithm:
- Read the input number.
- Create a copy of the input number.
- Initialize a variable sum and count to 0.
- Calculate the number of digits in the input number.
While the number is not equal to 0, perform the following steps: - Iterate through the given number, dividing it by 10 until it becomes 0.
- Increment a counter variable for each iteration.
- Reset the number to the copy of original value.
- While the number is not equal to 0, perform the following steps:
- Get the last digit of the number by using the modulo operator (%).
- Raise the last digit to the power of the number of digits.
- Add the result to the sum.
- Divide the number by 10 to remove the last digit.
- Check if the sum is equal to copy of the original number.
- If they are equal, the number is an Armstrong Number.
Otherwise, it is not. - Print the result.
In this algorithm, the input number is processed to calculate the number of digits using a loop that divides the number by 10 until it becomes 0. Then, the algorithm raises each digit to the power of the number of digits and adds them to the sum. Finally, it checks if the sum is equal to the copy of the original number to determine if it is an Armstrong Number.
Please note that: this implementation assumes that the input number is a positive integer. You may need to add additional checks for negative numbers or other cases depending on your requirements.
Source Code
num = int( input("Enter a Number To Check : "))
Count=0
Sum = 0
ncopy=num
while num!=0:
num = num // 10
Count=Count+1
num=ncopy
while num!=0:
Rd = num % 10
Sum = Sum + Rd**Count
num = num // 10
print("Sum of Digits(each raised to the power of the number of digits) : ",Sum)
if ncopy==Sum:
print("It is an Armstrong Number . ")
else:
print("It is Not an Armstrong Number . ")
Output
Sample Input :
Enter a Number To Check : 1634
Sample Output:
Sum of Digits(each raised to the power of the number of digits) :1634
It is an Armstrong Number
Sample Input :
Enter a Number To Check : 123
Sample Output:
Sum of Digits(each raised to the power of the number of digits) :36
It is Not an Armstrong Number