Prerequisite: Recursion in Java
In the previous article, we have discussed about Java Program to Find Greatest Common Divisor (GCD) of Two Numbers by Using Recursion
In this program we are going to check Armstrong number by using recursion in Java programming language.
Java Program to Check Armstrong Number by Using Recursion
Armstrong Number:
A number is called as Armstrong number if the sum of cube of each digit of the number is equal to the original number. For example- 153, 370, 407 etc. are called as Armstrong number.
Now let’s see different ways to check Armstrong number by using recursion.
Method-1: Java Program to Check Armstrong Number By Using Static Input and Recursion
Approach:
- Declare and initiate a static integer variable with zero for storing the result value.
- Declare and initiate an integer variable say ‘
n
‘ with some value. - Define an user defined method
Armstrong_Number()
and passn
as parameter. - Inside the user defined method find sum of cube of each digit of the number by calling the method recursively.
- Then in main method, match the returned result by recursive method with original number if it is same or not. If same then it is Armstrong else not Armstrong number.
- Print the result.
Program:
import java.lang.Math; public class Main { //Declare and initiate a static integer variable say 'res' with zero value int res=0; public static void main(String[] args) { //Declare and initiate an integer with some value int n=370; //Create an object of Main class Main ob=new Main(); //call the user defined method Armstrong_Number() //and check the condition returned result of the method is same as original number or not if(ob.Armstrong_Number(n)==n) System.out.print(n+" is an Armstrong Number."); else System.out.print(n+" is not an Armstrong Number."); } //Armstrong_Number() method to find sum of cube of each digit of the number int Armstrong_Number(int n) { //continue till n!=0 if(n!=0) { res+=Math.pow(n%10,3); //calling the same method recursively Armstrong_Number(n/10); } return res; } }
Output: 370 is an Armstrong Number.
Method-2: Java Program to Check Armstrong Number By Using User Input and Recursion
Approach:
- Declare and initiate a static integer variable with zero for storing the result value.
- Declare and initiate an integer variable say ‘
n
‘ and prompt the user to enter a number as it’s value. - Define an user defined method
Armstrong_Number()
and passn
as parameter. - Inside the user defined method find sum of cube of each digit of the number by calling the method recursively.
- Then in main method, match the returned result by recursive method with original number if it is same or not. If same then it is Armstrong else not Armstrong number.
- Print the result.
Program:
import java.lang.Math; import java.util.Scanner; public class Main { //Declare and initiate a static integer variable say 'res' with zero value int res=0; public static void main(String[] args) { //create object of scanner class Scanner sc=new Scanner(System.in); System.out.print("Enter a number:"); //Declare an integer and prompt the user to enter a value int n=sc.nextInt(); //Create an object of Main class Main ob=new Main(); //call the user defined method Armstrong_Number() //and check the condition returned result of the method is same as original number or not if(ob.Armstrong_Number(n)==n) System.out.print(n+" is an Armstrong Number."); else System.out.print(n+" is not an Armstrong Number."); } //Armstrong_Number() method to find sum of cube of each digit of the number int Armstrong_Number(int n) { //continue till n!=0 if(n!=0) { res+=Math.pow(n%10,3); //calling the same method recursively Armstrong_Number(n/10); } return res; } }
Output: Case-1 Enter a number:153 153 is an Armstrong Number. Case-2 Enter a number:445 445 is not an Armstrong Number.
Provided list of Simple Java Programs is specially designed for freshers and beginners to get familiarize with the concepts of Java programming language and become pro in coding.
Related Java Programs: