Kaprekar number in C++

Hello guys, In this tutorial we are going to learn, what are kaprekar numbers? How can we check whether the given number is kaprekar or not in C++.
So let’s answer our first question, Kaprekar number is a positive number whose square can be split into two numbers whose sum is the original number itself. Kaprekar numbers are named after the famous Indian mathematician D. R. Kaprekar.
Please note that-

  • One is a kaprekar number.
  • And the split parts of its square representation should not be equal to zero.

Let’s take some examples;

  1. 45, it’s square is 2025 which can be split into 20 and 25 whose sum is 45. So, 45 is a kaprekar number.
  2. 100, it’s square is 10000 which can not be split into such numbers. Hence, without a doubt, it is not a kaprekar number.

Now let’s answer our second question;

Check whether the given number is Kaprekar or not in C++

Algorithm

  • First, we will take the square of the given number.
  • Then we will count the number of digits in its square.
  • Finally using a while loop we will split the square into two numbers at each position and will check their sum.
  • And if the sum is equal to the original number then the number is kaprekar and we will break the loop.
  • Otherwise, we will continue and if we will iterate the whole loop then it is obvious that the number is not kaprekar.

Code

#include<bits/stdc++.h>
using namespace std;

// function used to calcualate 10 to the power i
int power(int i){
    int k=1;
    while(i--){
        k*=10;
    }
    return k;
}

// tells whether n is kaprekar number or not
void kaprekar_number(int n) 
{ 
    // As 1 is kaprekar number and edge case
    if(n==1){
        cout<<"Yes\n";
    }
    else{ 
        int n_square=n*n; // taking square
        int no_of_digits=0; // stores no of digits in the square of n
        int m=n*n;
        // used to calculate no_of_digits
        while(m){ 
            no_of_digits++; 
            m/=10;
        } 
        int i=1;
        int flag=0;// used to mark yes or no for a given number
        while(i<no_of_digits){
            int k=power(i); // calculate 10 to the power i
            // if a part is zero then number is not kaprekar
            // this will take care of that
            if(n_square%k==0){
                i++;
                continue;
            }
            //storing the sum of split numbers n_square/k and n_square%k
            int sum_of_split_parts=(n_square/k)+(n_square%k); 
            //prints yes as no is kaprekar
            if(sum_of_split_parts==n){
                cout<<"Yes\n";
                flag=1; //mark the flag to yes
                break;
            }
            i++;
        } 
        // if not marked to yes prints no
        if(flag==0){
            cout<<"No\n";
        }
    } 
} 

// driver function
int main(){
    // numbers to be checked
    vector<int> v={1,9,45,100,10,31,55};
    // printing answers to the corresponding numbers
    for(auto i:v){
        // calling function to check whether number is kaprekar or not
        kaprekar_number(i);
    }
    return 0;
}

Input

1 9 45 100 10 31 55

Output

Yes
Yes
Yes
No
No
No
Yes

Also, note that 6174 is known as Kaprekar constant.

This is it for this tutorial.
You can also check these posts.

Peace.

Leave a Reply

Your email address will not be published. Required fields are marked *