Definition Of Armstrong Number:- Armstrong Number is a number that is equal to the sum of cubs its digits.
Example:- 153=1^3 +5^3+3^3=1+125+27=153
407=4^3+0^3+7^3=64+0+343=407
Problem statement:- Program to check Armstrong number or not using recursion.
Data requirement:-
Input Data:- num
Output Data:-String Output
Output Data:-String Output
Program in C
Here is the source code of the C Program to check Armstrong number or not using recursion.
Code:
#include<stdio.h>
#include<math.h>
int check_ArmstrongNumber(int num)
{
if(num>0)
return (pow(num%10,3) +check_ArmstrongNumber(num/10));
}
int main()
{
int num;
printf("Enter a number:");
scanf("%d",&num);
if(check_ArmstrongNumber(num)==num)
printf("It is an Armstrong Number");
else
printf("It is not an Armstrong Number");
}
Enter a number:153
It is an Armstrong Number
Program in C++
Here is the source code of the C++ Program to check Armstrong number or not using recursion.
Code:
#include<iostream>
#include<cmath>
using namespace std;
int check_ArmstrongNumber(int num)
{
if(num>0)
return (pow(num%10,3) +check_ArmstrongNumber(num/10));
}
int main()
{
int num;
cout<<"Enter a number:";
cin>>num;
if(check_ArmstrongNumber(num)==num)
cout<<"It is an Armstrong Number";
else
cout<<"It is not an Armstrong Number";
}
Enter a number:225
It is not an Armstrong Number
Program in Java
Here is the source code of the Java Program to check Armstrong number or not using recursion.
Code:
import java.util.Scanner;
public class ArmstrongNumber {
int sum=0;
int check_ArmstrongNumber(int num)
{
if(num!=0)
{
sum+=Math.pow(num%10,3);
check_ArmstrongNumber(num/10);
}
return sum;
}
public static void main(String[] args) {
Scanner cs=new Scanner(System.in);
int num;
System.out.print("Enter a number:");
num=cs.nextInt();
ArmstrongNumber ob=new ArmstrongNumber();
if(ob.check_ArmstrongNumber(num)==num)
System.out.print("It is an Armstrong Number.");
else
System.out.print("It is not an Armstrong Number.");
cs.close();
}
}
Enter a number:370
It is an Armstrong Number.
Program in Python
Here is the source code of the Python Program to check Armstrong number or not using recursion.
Code:
sum=0
def check_ArmstrongNumber(num):
global sum
if (num!=0):
sum+=pow(num%10,3)
check_ArmstrongNumber(num//10)
return sum
num=int(input("Enter a number:"))
if (check_ArmstrongNumber(num) == num):
print("It is an Armstrong Number.")
else:
print("It is not an Armstrong Number.")
Enter a number:407
It is an Armstrong Number.
Most Recommend Questions:-
More Questions:-
0 Comments
Please do not Enter any spam link in the comment box