×
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
43818    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]
Leo Tolstoy
However diffcult life may seem, there is always something you can do, and succeed at. It matters that you don not just give up.
Leo Tolstoy
1973
73.53
Today So Far
Total View (Lakh)
softetechnologies
26/05/2018     52003
25/06/2018     43818
01/01/2018     42877
28/06/2017     40621
02/08/2017     39426
01/08/2017     33625
06/07/2017     33437
15/05/2017     32735
14/07/2017     28993
11/09/2018     28866
softetechnologies