Program to check Kaprekar Number In Java

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.

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.

In this tutorial we have learn about the Program to check Kaprekar Number In Java and its application with practical example. I hope you will like this tutorial.