In the previous article, we have discussed about Java Program to Find the Product of All Perfect Divisors of a Number Using Recursion
In this article we are going to test divisibility of a number by 11 and 9 using recursion by Java programming language.
Java Program to Test Divisibility of a Number by 11 and 9 Using Recursion
As per the problem statement you have to check the divisibility of a number by both 11 and 9.
For example:
Suppose there are 2 numbers 55 and 99. 55 is divisible by 11 but no divisible by 9. 99 is divisible by both 11 and 9.
Let’s see the program to understand it more clearly.
- Java Program to Test Divisibility of a Number by 11 and 9 By Using Recursion and Static Input Value
- Java Program to Test Divisibility of a Number by 11 and 9 By Using Recursion and User Input Value
Method-1: Java Program to Test Divisibility of a Number by 11 and 9 By Using Recursion and Static Input Value
Approach:
- Declare an integer variable and initialize the value for it.
- Check for divisibility by 9 and 11 using the user-defined method.
- Divisibility by 9 calculates the sum of digits and checks if it is divisible by 9.
- Divisibility by 11 uses two variables that store alternate digit sums and then checks if their difference is divisible by 11.
- Print the result.
Program:
import java.util.*; // Main class public class Main { // Recursive method to check divisibility by 11 public static int divisibleBy11(int num) { int sum1=0, sum2=0,diff; // Checks if the number has reached zero if(num == 0) return 1; // Checks if the number is less than 10 if(num < 10) return 0; // Loops while the number is non-zero while(num>0) { sum1 += num%10; num /=10; sum2 += num%10; num /= 10; } // calculates the difference between the sums and calls the function diff = sum1>sum2 ? (sum1-sum2) : (sum2-sum1); return divisibleBy11(diff); } // Recursive method to check divisibility by 9 public static int divisibleBy9(int num ) { int digitsSum; // if number becomes 9 it is divisible by 9 if(num==9) return 1; // if number becomes less than 9 it is not divisible by 9 if(num<9) return 0; digitsSum=0; // Loop to find the sum of digits while(num>0) { digitsSum += num%10; num/=10; } // Recusrsive call on the sum of digits return divisibleBy9(digitsSum); } public static void main(String[] args) { int num = 55; // Check if the number is divisible by 9 int res = divisibleBy9(num); // Print the result if(res==1) System.out.println(num+" is divisible by 9."); else System.out.println(num+" is not divisible by 9."); // Check if the number is divisible by 11 res = divisibleBy11(num); // Print the result if(res==1) System.out.println(num+" is divisible by 11."); else System.out.println(num+" is not divisible by 11."); } }
Output: 55 is not divisible by 9. 55 is divisible by 11.
Method-2: Java Program to Test Divisibility of a Number by 11 and 9 By Using Recursion and User Input Value
Approach:
- Declare an integer variable and ask the user to enter value for it.
- Check for divisibility by 9 and 11 using the user-defined method.
- Divisibility by 9 calculates the sum of digits and checks if it is divisible by 9.
- Divisibility by 11 uses two variables that store alternate digit sums and then checks if their difference is divisible by 11.
- Print the result.
Program:
import java.util.*; // Main class public class Main { // Recursive method to check divisibility by 11 public static int divisibleBy11(int num) { int sum1=0, sum2=0,diff; // Checks if the number has reached zero if(num == 0) return 1; // Checks if the number is less than 10 if(num < 10) return 0; // Loops while the number is non-zero while(num>0) { sum1 += num%10; num /=10; sum2 += num%10; num /= 10; } // calculates the difference between the sums and calls the function diff = sum1>sum2 ? (sum1-sum2) : (sum2-sum1); return divisibleBy11(diff); } // Recursive method to check divisibility by 9 public static int divisibleBy9(int num ) { int digitsSum; // if number becomes 9 it is divisible by 9 if(num==9) return 1; // if number becomes less than 9 it is not divisible by 9 if(num<9) return 0; digitsSum=0; // Loop to find the sum of digits while(num>0) { digitsSum += num%10; num/=10; } // Recusrsive call on the sum of digits return divisibleBy9(digitsSum); } public static void main(String[] args) { Scanner sc = new Scanner(System.in); // Asks the user for input System.out.println("Enter a number"); int num = sc.nextInt(); // Check if the number is divisible by 9 int res = divisibleBy9(num); // Print the result if(res==1) System.out.println(num+" is divisible by 9."); else System.out.println(num+" is not divisible by 9."); // Check if the number is divisible by 11 res = divisibleBy11(num); // Print the result if(res==1) System.out.println(num+" is divisible by 11."); else System.out.println(num+" is not divisible by 11."); } }
Output: Enter a number 65 65 is not divisible by 9. 65 is not divisible by 11.
Don’t stop learning now. Get hold of all the important Java fundamentals with the Simple java program example guide and practice well.
Related Java Programs: