Kaprekar Number | C ++ program to check if a number is Kaprekar number or not ; Returns true if n is a Kaprekar number , else false ; Count number of digits in square ; Split the square at different poitns and see if sum of any pair of splitted numbers is equal to n . ; To avoid numbers like 10 , 100 , 1000 ( These are not Karprekar numbers ; Find sum of current parts and compare with n ; compare with original number ; Driver code
#include <bits/stdc++.h> NEW_LINE using namespace std ; bool iskaprekar ( int n ) { if ( n == 1 ) return true ; int sq_n = n * n ; int count_digits = 0 ; while ( sq_n ) { count_digits ++ ; sq_n /= 10 ; } for ( int r_digits = 1 ; r_digits < count_digits ; r_digits ++ ) { int eq_parts = pow ( 10 , r_digits ) ; if ( eq_parts == n ) continue ; int sum = sq_n / eq_parts + sq_n % eq_parts ; if ( sum == n ) return true ; } return false ; } int main ( ) { cout << " Printing ▁ first ▁ few ▁ Kaprekar ▁ Numbers " " ▁ using ▁ iskaprekar ( ) STRNEWLINE " ; for ( int i = 1 ; i < 10000 ; i ++ ) if ( iskaprekar ( i ) ) cout << i << " ▁ " ; return 0 ; }