TheDeveloperBlog.com

Home | Contact Us

C-Sharp | Java | Python | Swift | GO | WPF | Ruby | Scala | F# | JavaScript | SQL | PHP | Angular | HTML

Program to print all Kaprekar numbers between 1 to 100

Program to print all Kaprekar numbers between 1 to 100 on fibonacci, factorial, prime, armstrong, swap, reverse, search, sort, stack, queue, array, linkedlist, tree, graph, pattern, string etc.

<< Back to PROGRAM

Program to print all Kaprekar numbers between 1 to 100

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

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 
Next Topic#




Related Links:


Related Links

Adjectives Ado Ai Android Angular Antonyms Apache Articles Asp Autocad Automata Aws Azure Basic Binary Bitcoin Blockchain C Cassandra Change Coa Computer Control Cpp Create Creating C-Sharp Cyber Daa Data Dbms Deletion Devops Difference Discrete Es6 Ethical Examples Features Firebase Flutter Fs Git Go Hbase History Hive Hiveql How Html Idioms Insertion Installing Ios Java Joomla Js Kafka Kali Laravel Logical Machine Matlab Matrix Mongodb Mysql One Opencv Oracle Ordering Os Pandas Php Pig Pl Postgresql Powershell Prepositions Program Python React Ruby Scala Selecting Selenium Sentence Seo Sharepoint Software Spellings Spotting Spring Sql Sqlite Sqoop Svn Swift Synonyms Talend Testng Types Uml Unity Vbnet Verbal Webdriver What Wpf