C-Sharp | Java | Python | Swift | GO | WPF | Ruby | Scala | F# | JavaScript | SQL | PHP | Angular | HTML
Program to print all Kaprekar numbers between 1 to 100A 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 Algorithmmain()
Kaprekar(int n)
Java Programpublic 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 Programdef 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#
|