### Here is given c program for Kaprekar number.

But before what exactly is a Kaprekar number?

A Kaprekar number is a number whose square can be broken/partitioned in such a manner that the sum of the partitions adds up to bring the number itself.

Example: 99^{2} = 9801 : 98+01 = 99 (hence kaprekar)

// This program is however limited to check untill 99999 because long int has a range of 0 – 4294967296 and 99999^{2} is greater than that

**Also Check: Java program for kaprekar number**

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 |
#include <stdio.h> unsigned long int digitcount(long int x) { unsigned long int digit=; while(x) { digit++; x/=10; } return digit; } unsigned long int return_A(long int x) { while(x%10==) x/=10; return x; } unsigned long int div(long int x) { unsigned long int num=1; while(x--) num*=10; return num; } int main() { unsigned long int n,A,B,digits,sq,temp; printf("Enter number to find whether it is a Kaprekar number or not : "); scanf("%ld",&n); sq=n*n; //count the number of digits in the square of that number digits=digitcount(sq); //now partition the square number using X=Ab^n + B ; where b=10 (decimal number system) // partitioning theory is written above A= sq/ div(digits/2); //But this will get Ab^n so remove b^n:- A=return_A(A); B= sq% div(digits/2); temp=A+B; if(temp==n) printf("\n It is a Kaprekar number"); else printf("\n Not a kaprekar number"); return 1; } |

OUTPUT:

Incoming terms: C program for kaprekar number, c program to check whether it is a kaprekar number or not.