diff --git a/C/README.md b/C/README.md index 9251eeaa..a80634a0 100644 --- a/C/README.md +++ b/C/README.md @@ -48,6 +48,7 @@ | Program-44 | Program for implementation of the approach | | Program-45 | Program to illustrate the above given pattern of numbers. | | Program-46 | Program to convert binary number to decimal | +| Program-47 | Program to print kaprekar number in a given range | diff --git a/C/program-47/kaprekar_number.c b/C/program-47/kaprekar_number.c new file mode 100644 index 00000000..37f77482 --- /dev/null +++ b/C/program-47/kaprekar_number.c @@ -0,0 +1,42 @@ +/* A kaprekar number is a number in base 10 whose square can be split such that the the sum of those numbers +add up to the original number for eg : (45)^2 = 2025 => 20+25=45 */ + + # include + # include + # include + # include + bool iskaprekarnum(int n) + { + if (n == 1) // If the number passed is 1 then it is also considered to be a kaprekar number + return true; + int square_number = n * n; // Finding the square + int count_digits = 0; //This will be the counter variable for the number of digits in the number + while (square_number != 0) + { + count_digits++; + square_number /= 10; + } + square_number = n*n; // Recalculating square as it was changed in the above while loop + for (int r_digits=1; r_digits