Write a program in java to accept any positive number, 'n' and check whether it is a Kaprekar Number
or not. Display "ts a Kaprekar Number f n is a Kaperkar Number, otherwise display "it's not a Kaprekar
Number. If the number entered is not a positive number, then display an error message "Invalid Number
and ask the user to re-enter the number once again, your program should keep on accepting number
from the user until the user enters a positive number.
Answers
Mark As Brainly, it was a tought program for me.
A number whose square divided into two parts (none of the parts have only 0) and the sum of the parts is equal to the original number then it is called kaprekar number. The kaprekar number is named after D. R. Kaprekar.
Example of kaprekar numbers:- 45
The square of 45 = 2025
20 + 25 = 45, so 45 is a kaprekar number.
Here-"sopln" is abbreviated for System.out.println
import java.util.*
class kaprekar
{
void main()
{
Scanner sc=new Scanner (System.in);
int n , sq , d , p1 , p2 , tmp , c=0 ;
Sopln("Enter a number");
n=sc.nextInt();
while(n<0)
{
Sopln("Invalid Number. Enter a positive number");
n=sc.nextInt();
}
sq= Math.pow(n,2);
sq=tmp;
while(tmp>0)
{
d=tmp%10;
c++;
tmp=tmp/10
}
if (c%2==0)
{
p1=sq%(Math.pow(10,(c/2)));
p2=sq%(Math.pow(10,c)) - p1;
if(p1+p2=n)
{
Sopln("It is a Kaprekar Number");
}
else
{
Sopln("It is not a Kaprekar Number");
}
}
else
{
Sopln("It is not a Kaprekar Number");
}