Wap in Java to enter a number a check it's Kaprekar number or not For eg- 45^2=2025, 20+25=45(Kaprekar)
Answers
Answer:
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");
}
}
#answerwithquality &#BAL
Answer:
import java.util.*;
public class kaprekar
{
public static void main()
{
Scanner Sc=new Scanner(System.in);
int n,s,x,c=0,a,lh,fh;
System.out.println("enter a no.");
n=Sc.nextInt();
s=(n*n);
x=s;
while(s!=0)
{
c++;
n/=10;
}
a=c/2;
if(c%2!=0)
{
fh=x%(int)math.pow(10,a);
lh=x/(int)math.pow(10,a);
}
else
{
fh=x%(int)math.pow(10,a+1);
lh=x/(int)math.pow(10,a+1);
}
if(fh+lh==n)
System.out.println("it is kaprekar no.");
else
System.out.println("it is not kaprekar no.");
}
}
Explanation:
it is the easiest way to do kaprekar no.
n=no.
s= square of the no.
fh=first half of the square
lh=last half of the square
x= for storiing the value of square
THANK YOU !!!!!