Program to check Number is Armstrong or Not
Definition & Explanation
If you have a number, the goal is to determine if it’s an Armstrong number. An Armstrong number is one that meets certain conditions. To figure this out, you need to write a code that checks these conditions. If the conditions are met, then the number is considered an Armstrong number.
Example:
Input: 153
Output: It’s an Armstrong Number.
Detailed explanation
Let’s take the number 153 as an example. It has three digits. Now, if you raise each digit to the power of 3 (the total number of digits) and add them together:
1^3 + 5^3 + 3^3 = 1 + 125 + 27 = 153.
Since the result is the same as the original number (153), 153 is an Armstrong number.
C
Method 1 :
#include <stdio.h>
#include <math.h>
int order(int x)
{
int len = 0;
while (x)
{
len++;
x = x/10;
}
return len;
}
int getArmstrongSum(int num, int order){
if(num == 0)
return 0;
int digit = num % 10;
return pow(digit, order) + getArmstrongSum(num/10, order);
}
// Driver Code
int main ()
{
int num, len;
num=1634;
printf("The number is:%d\n",num);
// function to get order(length)
len = order(num);
// check if Armstrong
if (num == getArmstrongSum(num, len))
printf("%d is Armstrong", num);
else
printf("%d is Not Armstrong", num);
}
Output :
The number is: 1634
1634 is Armstrong
Method 2 : Recursion
#include <stdio.h>
#include <math.h>
int order(int x)
{
int len = 0;
while (x)
{
len++;
x = x/10;
}
return len;
}
int armstrong(int num, int len){
int sum = 0, temp, digit;
temp = num;
// loop to extract digit, find power & add to sum
while(temp != 0)
{
// extract digit
digit = temp % 10;
// add power to sum
sum = sum + pow(digit,len);
temp /= 10;
};
return num == sum;
}
// Driver Code
int main ()
{
//variables initialization
int num = 1634, len;
// function to get order(length)
len = order(num);
// check if Armstrong
if (armstrong(num, len))
printf("%d is armstrong\n",num);
else
printf("%d is not armstrong\n",num);
return 0;
}
Output :
The number is: 1634
1634 is Armstrong
C++
Method 1 :
#include<bits/stdc++.h>
#include<math.h>
using namespace std;
// Armstrong number is any number following the given rule
// abcd... = a^n + b^n + c^n + d^n + ...
// Where n is the order(length/digits in number)
// Example = 153 (order/length = 3)
// 153 = 1^3 + 5^3 + 3^3 = 1 + 125 + 27 = 153
// Example = 1634 (order/length = 4)
// 1634 = 1^4 + 6^4 + 3^4 + 4^4 = 1 + 1296 + 81 + 256 = 1634
// number of digits in a number is order
int order(int x)
{
int len = 0;
while (x)
{
len++;
x = x/10;
}
return len;
}
bool armstrong(int num, int len){
int sum = 0, temp, digit;
temp = num;
// loop to extract digit, find power & add to sum
while(temp != 0)
{
// extract digit
digit = temp % 10;
// add power to sum
sum = sum + pow(digit,len);
temp /= 10;
};
return num == sum;
}
// Driver Code
int main ()
{
//variables initialization
int num = 407, len;
// function to get order(length)
len = order(num);
// check if Armstrong
if (armstrong(num, len))
cout << num << " is armstrong";
else
cout << num << " is not armstrong";
return 0;
}
Output :
407 is armstrong
JAVA
Method 1 : Using Iteration
public class Main
{
public static void main (String[]args)
{
int num = 407, len;
// function to get order(length)
len = order (num);
// check if Armstrong
if (armstrong (num, len))
System.out.println(num + " is armstrong");
else
System.out.println(num + " is armstrong");
}
static int order (int x)
{
int len = 0;
while (x != 0 )
{
len++;
x = x / 10;
}
return len;
}
static boolean armstrong (int num, int len)
{
int sum = 0, temp, digit;
temp = num;
// loop to extract digit, find power & add to sum
while (temp != 0)
{
// extract digit
digit = temp % 10;
// add power to sum
sum = sum + (int)Math.pow(digit, len);
temp /= 10;
};
return num == sum;
}
}
Output :
407 is armstrong
Method 2 : Using Recursion
import java.util.*;
import java.lang.*;
class Main {
// Driver code
public static void main(String[] args)
{
//variables initialization
int num = 1634, reverse = 0;
int len = order(num);
if (num == getArmstrongSum(num, len))
System.out.println(num + " is an Armstrong Number");
else
System.out.println(num + " is not an Armstrong Number");
}
private static int getArmstrongSum(int num, int order) {
if(num == 0)
return 0;
int digit = num % 10;
return (int) Math.pow(digit, order) + getArmstrongSum(num/10, order);
}
private static int order(int num) {
int len = 0;
while (num!=0)
{
len++;
num = num/10;
}
return len;
}
}
Output :
1634 is an Armstrong Number
Python
Method 1 : Using Iteration
number = 371
num = number
digit, sum = 0, 0
length = len(str(num))
for i in range(length):
digit = int(num%10)
num = num/10
sum += pow(digit,length)
if sum==number:
print("Armstrong")
else:
print("Not Armstrong")
Output :
Armstrong
Method 2 : Using Recursion
number = 371
num = number
sum =0
length = len(str(num))
def checkArmstrong(num,length,sum):
if num==0:
return sum
sum+=pow(int(num%10),length)
return checkArmstrong(num/10,length,sum)
if checkArmstrong(num,length,sum)==number:
print('Armstrong')
else:
print("Not Armstrong")
Output :
Armstrong