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 ; Recompute square as it was changed ; 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 method
using System ; class GFG { static bool iskaprekar ( int n ) { if ( n == 1 ) return true ; int sq_n = n * n ; int count_digits = 0 ; while ( sq_n != 0 ) { count_digits ++ ; sq_n /= 10 ; } sq_n = n * n ; for ( int r_digits = 1 ; r_digits < count_digits ; r_digits ++ ) { int eq_parts = ( int ) Math . 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 ; } public static void Main ( ) { Console . WriteLine ( " Printing ▁ first ▁ few ▁ " + " Kaprekar ▁ Numbers ▁ using ▁ iskaprekar ( ) " ) ; for ( int i = 1 ; i < 10000 ; i ++ ) if ( iskaprekar ( i ) ) Console . Write ( i + " ▁ " ) ; } }