In this tutorial you will learn about the Program to check Kaprekar Number In Java and its application with practical example.
In this tutorial, we will learn to create a Java Program to check Kaprekar Numbers in Java using Java programming.
Prerequisites
Before starting with this tutorial we assume that you are best aware of the following Java programming topics:
- Java Operators.
- Basic Input and Output function in Java.
- Class and Object in Java.
- Basic Java programming.
- If-else statements in Java.
- For loop in Java.
What is Kaprekar Number.
Capricorn or Kaprekar number is a number whose square is divided into two parts and add the additions of parts is equal to the number.
Example of “kaprekar numbers”:- 45
The square of 45 = 2025
20 + 25 = 45, so 45 is a kaprekar number.
Program to check Kaprekar Number In Java
In this program we would find the given number is a Kaprekar or Capricorn number or not.first of all we would take a value from user and find the Capricorn number.let have a look at the code.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 |
// java program to find Kaprekar number.. import java.util.Scanner; public class Kaprekar_no { public static boolean Kaprekar(int num) { // declare variable and take value from user.. int sqr = 0; int temp = 0; int cDigit = 0; int fH = 0; int sH = 0; int sum = 0; // calculate square of number.. sqr = num * num; // count number of digits .. temp =sqr; while(temp!=0) { cDigit++; temp /= 10; } // divide square into two parts and // check it's sum is equal to the number? for(int i=cDigit-1; i>0; i--) { // find first part fH = sqr / (int)Math.pow(10, i); // find second part sH = sqr % (int)Math.pow(10, i); // check have any part only 0 if(fH == 0 || sH == 0) continue; // find sum value sum = fH + sH; // compare sum and number if( sum == num ) return true; } return false; } public static void main(String[] args) { // declare variables int no = 0; // read the input Scanner scan = new Scanner(System.in); System.out.print("Enter an any number"); no = scan.nextInt(); // check the number is kaprekar number or not if(Kaprekar(no)) System.out.println(no+" is a" +"kaprekar number"); else System.out.println(no+" is not a" + "kaprekar number"); // close Scanner class object scan.close(); } } |
Output
Kaprekar Number.
Not a Kaprekar Number.
In the above program, we have first declared and initialized a set variables required in the program.
- no= it will hold entered number.
- temp= it will hold temp the value of a number.
- sqr= it wil hold square of a number.
- cDigit= it will count digit.
- fH and sH= fH holds first half and sH holds Second half.
- sum =it will hold sum of values.
After that we take a number from user and find given number is a Kaprekar number or not
after getting a value from user we will pass the number to function if(Kaprekar(no)) where we will check condition for Kaprekar number that given number is Kaprekar number or not.
Procedure that check the given number is kaprekar number or not.
1) Take a number from user.
2) Find the square of a number.
3) Count all the digit of a squared number.
4) Iterator variable i start from digits of square – 1.
5) Ge first half, by dividing the square value by 10.
6) And second part as remainder value after dividing 10.
7) Check value of half’s if any part having only 0, if found skip the iteration if not.
8) Calculate sum of hF+sH.
9) Comparing sum and number,
if both are equal then function returns true and if not function returns False.
and finally we will print number is Kaprekar or not.