Problem: Kaprekar Number
Consider an n-digit number k . Square it and add the right n digits to the left n or n-1 digits. If the resultant sum is k, then k is called a Kaprekar number.
For example, 9 is a Kaprekar number since
9^2 = 81, 8+1=9
and 297 is a Kaprekar number since
297^2=88 209, 88+209=297
The first few are 1, 9, 45, 55, 99, 297, 703, ...
Console: Not a kaprekar
-@D
Consider an n-digit number k . Square it and add the right n digits to the left n or n-1 digits. If the resultant sum is k, then k is called a Kaprekar number.
For example, 9 is a Kaprekar number since
9^2 = 81, 8+1=9
and 297 is a Kaprekar number since
297^2=88 209, 88+209=297
The first few are 1, 9, 45, 55, 99, 297, 703, ...
Solve:
We consider a number in our case : 19044
Lets see the following Code:
Lets see the following Code:
// ADLabs public class Kaprekar { public static void main(String[] args) { Kaprekar kaprekar = new Kaprekar(); int inputNumber=19044; // input Number int inputNumberLength = kaprekar.length_of_num(inputNumber); int squareNumber = inputNumber*inputNumber; //int squareNumberLenght = kaprekar.length_of_num(squareNumber); int operator =(int) Math.pow(10, inputNumberLength); int rightDigits = squareNumber%(operator); int leftDigits = squareNumber/(operator); int sumDigits = rightDigits+leftDigits; if(inputNumber==sumDigits){ System.out.println(inputNumber +" is Kaprekar Number"); }else{ System.out.println("Not a kaprekar"); } } public int length_of_num(int num) { int length=0; while(num>0){ num /= 10; length++; } return length; } }
Console: Not a kaprekar
-@D