Java: Generate and show all Kaprekar numbers less than 1000
Java Numbers: Exercise-4 with Solution
Write a Java program to generate and show all Kaprekar numbers less than 1000.
In number theory, a Kaprekar number for a given base is a non-negative integer, the representation of whose square in that base can be split into two parts that add up to the original number again. For instance, 45 is a Kaprekar number, because 452 = 2025 and 20 + 25 = 45.
The first few Kaprekar numbers in base 10 are:
1, 9, 45, 55, 99, 297, 703, 999, 2223, 2728, 4879, 4950, 5050, 5292, …
Pictorial Presentation:
Sample Solution:
Java Code:
public class Example4 {
public static void main(String[] args){
int ctr = 0;
int base = (args.length > 0) ? Integer.parseInt(args[0]) : 10;
for(long n = 1; n <= 1000; n++){
String sqr_Str = Long.toString(n * n, base);
for(int j = 0; j < sqr_Str.length() / 2 + 1; j++){
String[] parts = split_num(sqr_Str, j);
long first_Num = Long.parseLong(parts[0], base);
long sec_Num = Long.parseLong(parts[1], base);
if(sec_Num == 0) break;
if(first_Num + sec_Num == n){
System.out.println(Long.toString(n, base) +
"\t" + sqr_Str + "\t " + parts[0] + " + " + parts[1]);
ctr++;
break;
}
}
}
System.out.println(ctr + " Kaprekar numbers.");
}
private static String[] split_num(String str, int idx){
String[] ans1 = new String[2];
ans1[0] = str.substring(0, idx);
if(ans1[0].equals("")) ans1[0] = "0";
ans1[1] = str.substring(idx);
return ans1;
}
}
Sample Output:
1 1 0 + 1 9 81 8 + 1 45 2025 20 + 25 55 3025 30 + 25 99 9801 98 + 01 297 88209 88 + 209 703 494209 494 + 209 999 998001 998 + 001 8 Kaprekar numbers.
Flowchart:

Java Code Editor:
Contribute your code and comments through Disqus.
Previous: Write a Java program to generate random integers in a specific range.
Next: Write a Java program to find the number of seed Lychrel number candidates and related numbers for n in the range 1..10000 inclusive. (With that iteration limit of 500).
What is the difficulty level of this exercise?
Test your Programming skills with w3resource's quiz.
Java: Tips of the Day
IsPowerOfTwo
Checks if a value is positive power of two.
To understand how it works let's assume we made a call IsPowerOfTwo(4).
As value is greater than 0, so right side of the && operator will be evaluated.
The result of (~value + 1) is equal to value itself. ~100 + 001 => 011 + 001 => 100. This is equal to value.
The result of (value & value) is value. 100 & 100 => 100.
This will value the expression to true as value is equal to value.
public static boolean isPowerOfTwo(final int value) { return value > 0 && ((value & (~value + 1)) == value); }
Ref: https://bit.ly/3sA5d4I
- Weekly Trends
- Python Interview Questions and Answers: Comprehensive Guide
- Scala Exercises, Practice, Solution
- Kotlin Exercises practice with solution
- MongoDB Exercises, Practice, Solution
- SQL Exercises, Practice, Solution - JOINS
- Java Basic Programming Exercises
- SQL Subqueries
- Adventureworks Database Exercises
- C# Sharp Basic Exercises
- SQL COUNT() with distinct
- JavaScript String Exercises
- JavaScript HTML Form Validation
- Java Collection Exercises
- SQL COUNT() function
- SQL Inner Join
We are closing our Disqus commenting system for some maintenanace issues. You may write to us at reach[at]yahoo[dot]com or visit us at Facebook