A non-negative integer having base(10) is said to be the Kaprekar number if the representation of its square in its base can be split into two parts that add up to the original number, with the condition that the part formed from the low-order digits of the square must be non-zero-however, it is allowed to include leading zeroes.
Examples:
45 = (45)2 = 2025 =20 + 25 -45
1 = 12 = 01 = 0 + 1 = 1
Algorithm
main()
- STEP 1: START
- STEP 2: REPEAT STEP 3 UNTIL (i<10)
- STEP 3: if(Kaprekar(i)) then PRINT i
- STEP 4: END
Kaprekar(int n)
- STEP 1: START
- STEP 2: if(n==1) RETURN true
- STEP 3: sq_n = n*n
- STEP 4: SET count_digits = 0
- STEP 5: REPEAT STEP 6 and 7 UNTIL (sq_n!=0)
- STEP 6: count_digits++
- STEP 7: sq_n/=10
- STEP 8: sq_n = n*n
- STEP 9: REPEAT STEP 10 to 13 UNTIL r_digits<count_digits
- STEP 10: eq_parts = (10)r_digits
- STEP 11: if(eq_parts==n) then continue
- STEP 12: sum = sq_n/eq_parts + sq_n%eq_parts
- STEP 13: if(sum==n)
then RETURN true
- STEP 14: RETURN false.
- STEP 15: END
Java Program
Output:
Python Program
Output:
C Program
Output:
C# Program:
Output:
PHP program
Output: