Capricorn or Kaprekar Number Program in Java

A number is called Capricorn or Kaprekar number whose square is divided into two parts in any conditions and parts are added, the additions of parts is equal to the number, is called Capricorn or Kaprekar number.

Example: 45

45 =  (45)2 = 2025

2025
All parts for 2025:- 
202 + 5 = 207 (not 45)
20 + 25 = 45 
2 + 025 = 27 (not 45)

Above we can see one combination is equal to number so that 45 is Capricorn or Kaprekar number.

import java.util.Scanner;

public class CapricornNumber
{

    public static void main(String[] args)
    {
        Scanner scanner = new Scanner(System.in);
        System.out.print("Enter a number=");
        int n = scanner.nextInt();
         boolean isCapricorn = false;
         
        int square = n * n;
        int temp = square;
        int contDigits = 0;
        
        while (temp > 0)
        {
            contDigits++;
            temp /= 10;
        }
       
        for (int i = 1; i < contDigits; i++)
        {
            int divisor = (int) Math.pow(10, i);
            int quotient = square / divisor;
            int remainder = square % divisor;
            if (quotient + remainder == n)
            {
                isCapricorn = true;
            }
        }
        if (isCapricorn)
        {
            System.out.println("Capricorn/Kaprekar number");
        } else
        {
            System.out.println("Not Capricorn/Kaprekar number");
        }
    }
}


Output:

Enter a number=297
Capricorn/Kaprekar number
What is Kaprekar Number?
A number is called Capricorn or Kaprekar number whose square is divided into two parts in any conditions and parts are added, the additions of parts is equal to the number, is called Capricorn or Kaprekar number.
Example:

45 =  (45)2 = 2025 =20 + 25 = 45
1 = 12 = 01 = 0 + 1 = 1
java java programs number based program