×
FREE ASSISTANCE FOR THE INQUISITIVE PEOPLE
Tutorial Topics
X
softetechnologies
Write a program to display the possible sum Consecutive numbers to developed a given number. Check A Date Valid or not
Kaprekar number - Java
32800    Arnab De    25/06/2018

Kaprekar number

Write a program to check a number is Kaprekar number or not. A Kaprekar number for a given base is a non-negative integer, the representation of whose square in that base can be split into two parts that add up to the original number again. For instance, 45 is a Kaprekar number, because 452 = 2025 and 20+25 = 45.



import java.util.*;
class Kaprekar
{
    public static void main()
    {
        Scanner sc = new Scanner(System.in);
        System.out.println("Enter the number:");
        int num = sc.nextInt();
        int p = 0;
        int digit=0,sq,sum=0;
        sq = num*num;
        p=sq;
        while(sq>0)
        {
            digit++;
            sq=sq/10;
        }
        int k=(int)Math.pow(10,(digit/2));
        while(p>0)
        {
            sum=sum+(p%k);
            p=p/k;
        }
        if(sum==num){
        System.out.println("Is a kaprekar number");
        }
        else{
        System.out.println("Is not a kaprekar number");
        }
    }
}
        
softetechnologies

Output:
Enter the number:
9 Is a kaprekar number

Write a program to display the possible sum Consecutive numbers to developed a given number. Check A Date Valid or not
softetechnologies
Author Details
Arnab De
I have over 16 years of experience working as an IT professional, ranging from teaching at my own institute to being a computer faculty at different leading institute across Kolkata. I also work as a web developer and designer, having worked for renowned companies and brand. Through tutorialathome, I wish to share my years of knowledge with the readers.
Enter New Comment
Comment History
03/11/2021: Hu... U can do it in easy way also.... But this is also right.... GOOD JOB
By: Hanta Killer [hanukumar2007@gmail.com]
Thank You very much
02/02/2023: 297 is a Kaprekar no. but above code will show that its not.
By: ashish [ashish_rc@ymail.com]
Swami Vivekananda
You cannot believe in God until you believe in yourself.
Swami Vivekananda
209
50.57
Today So Far
Total View (Lakh)
softetechnologies
26/05/2018     41324
01/01/2018     35754
28/06/2017     33753
25/06/2018     32800
02/08/2017     32076
06/07/2017     26582
01/08/2017     26545
15/05/2017     26186
14/07/2017     21517
21/04/2018     20497
softetechnologies