Q:

Program to print all Kaprekar numbers between 1 to 100

belongs to collection: Number Programs

0

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

All Answers

need an explanation for this answer? contact us directly to get an explanation for this answer

Java Program

public class Kaprekar_Number {  
    static boolean kaprekar(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 (String[] args)   
    {   
       for (int i=1; i<100; i++)   
            if (kaprekar(i))   
                 System.out.print(i + " ");   
    }   
}   

 

Output:

9  10  45  55  99 

 

Python Program

def print_Kaprekar_nums(start, end):  
   for i in range(start, end ):  
      sqr = i ** 2  
      digits = str(sqr)  
  
      length = len(digits)  
      for x in range(1, length):  
         left = int("".join(digits[:x]))  
         right = int("".join(digits[x:]))  
         if (left + right) == i:  
            print("%s "%(str(i)), end = " ");  
  
print_Kaprekar_nums(1, 100)  

 

Output:

9  10  45  55  99

 

C Program

# include <stdio.h>  
# include <string.h>  
# include <stdbool.h>  
# include <math.h>  
bool kaprekar(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) 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;   
    }    
  
  
int main()  
{  
for (int i=1; i<100; i++) {  
if (kaprekar(i))   
printf("%d  ",i );   
}  
return 0;  
}  

 

Output:

9  10  45  55  99 

 

C# Program:

using System;  
public class Kaprekar {   
          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()   
    {   
        for (int i = 1; i < 100; i++)   
            if (iskaprekar(i))   
                Console.Write(i + " ");   
    }   
}  

 

Output:

9  10  45  55  99

 

PHP program

<?php   
function kap($n)   
{   
    if ($n == 1)   
    return true;   
    $sq_n = $n * $n;   
    $count_digits = 0;   
    while ($sq_n)   
    {   
        $count_digits++;   
        $sq_n = (int)($sq_n / 10);   
    }   
    
    $sq_n1 = $n * $n;   
    for ($r_digits = 1;    
         $r_digits < $count_digits;    
         $r_digits++)   
    {   
        $eq_parts = pow(10, $r_digits);   
       if ($eq_parts == $n)   
            continue;   
    
        $sum = (int)($sq_n1 / $eq_parts) +   
                     $sq_n1 % $eq_parts;   
        if ($sum == $n)   
        return true;   
    }   
    return false;   
}   
  for ($i = 1; $i < 100; $i++)   
    if (kap($i))   
        echo $i . " ";   
?>  

 

Output:

9  10  45  55  99 

need an explanation for this answer? contact us directly to get an explanation for this answer

total answers (1)

Program to print all prime numbers between 1 and 1... >>
<< Program to print all abundant numbers between 1 an...