KAPREKAR NUMBER OR NOT

Consider the following I/O samples.



SAMPLE INPUT: 45

SAMPLE OUTPUT:  KAPREKAR NUMBER


A KAPREKAR NUMBER is a number whose square divided into two parts and the sum of the parts is equal to the original number.

45^2 = 2025  ---  20 + 25 =45




C PROGRAM:

#include <stdio.h>

#include<math.h>

#include<stdbool.h>

bool kap(int n)

{
    
    int sqr,i,equal,sum,ctr=0;
    
    if(n==1)
    
        return true;
        
    sqr=n*n;
    
    ctr=0;
    
    while(sqr)
    
    {
        
        ctr++;
        
        sqr/=10;
    
        
    }
    
    sqr=n*n;
    
    for(i=1;i<ctr;i++)
    
    {
        
        equal=pow(10,i);
        
        if(equal == n)
        
            continue;
            
        sum=sqr/equal + sqr%equal;
        
        if(sum == n)
        
            return true;
        
    }
    
    return false;
    
}

int main()

{

    int n;
    
    scanf("%d",&n);
    
    if(kap(n) == true)
    
        printf("KAPREKAR NUMBER");
        
    else
    
        printf("NOT A KAPREKAR NUMBER");

    return 0;
    
}


OUTPUT:


           





The given source code in C program "FOR KAPREKAR NUMBER OR NOT" is short and simple to understand. The source code is well tested in DEV-C++ and is completely error free. 
If you have any feedback about this article and want to improve this, please comment in the comment section.

Comments

Popular posts from this blog

INVERTED PYRAMID STAR PATTERN

C PROGRAM TO PRINT ALL LEAP YEARS IN A GIVEN RANGE

Design a way to sort the list of positive integers in the descending order according to frequency of elements. The elements with integers with higher frequency come before with lower frequency elements with same frequency come in the same order as they appear the values.