write a program in Java to input a number and check whether it is a kaprekar number or not
Answers
Answered by
7
A positive whole number ‘n’ that has ‘d’ number of digits is squared and split into two pieces, a right-hand piece that has ‘d’ digits and a left-hand piece that has remaining ‘d’ or ‘d-1’ digits.
If the sum of the two pieces is equal to the number, then ‘n’ is a Kaprekar number. The first few Kaprekar numbers are: 9, 45, 297 ……..
Example 1:
92 = 81, right-hand piece of 81 = 1 and left hand piece of 81 = 8
Sum = 1 + 8 = 9, i.e. equal to the number. Hence, 9 is a Kaprekar number.
Example 2:
452 = 2025, right-hand piece of 2025 = 25 and left hand piece of 2025 = 20
Sum = 25 + 20 = 45, i.e. equal to the number. Hence, 45 is a Kaprekar number.
Example 3:
2972 = 88209, right-hand piece of 88209 = 209 and left hand piece of 88209 = 88
Sum = 209 + 88 = 297, i.e. equal to the number. Hence, 297 is a Kaprekar number.
Program :-
import java.util.*;
class Kaprekar
{
public static void main(String args[]) throws IOException
{
Scanner sc = new Scanner(System.in);
System.out.print("Enter a Number : ");
int n = sc.nextInt(); //Inputting the number
int sq = n*n; //finding the square of the number
String s = Integer.toString(sq); //converting the square into a String
if(sq<=9)
s = "0"+s; //Adding a zero in the beginning if the square is of single digit
int l = s.length(); //finding the length (i.e. no. of digits in the square).
int mid = l/2; //finding the middle point
String left=s.substring(0,mid); //extracting the left digits from the square
String right=s.substring(mid); //extracting the right digits from the square
int x = Integer.parseInt(left); //converting the left String into Integer
int y = Integer.parseInt(right); //converting the right String into Integer
//if sum of left and right numbers is equal to the original number then it is a Kaprekar number
if(x+y == n)
System.out.println(n+" is a Kaprekar Number");
else
System.out.println(n+" is Not a Kaprekar Number");
}
}
Similar questions