Program 4
Program 4
Question: A positive natural number with p digits is said to be Kaprekar number if the
representation of its square can be split into two parts (where the second part has p digits),
that add up to the original number.
Example: 452 = 2025 and 20 + 25 = 45. Hence 45 is a Kaprekar number
2972 = 88209 and 88 + 209 = 297 Hence 297 is a Kaprekar number
Write a program to generate and show all Kaprekar numbers less than 1000.
Algorithm:
Step 1: START
Step 2: create a mono-parameterized boolean method named isKaprekar
Sub-Steps:
2.1: Set s = “” + a2
2.2: Set n1 = last n digits, where n=no. of digits of original no. & n2 = preceding digits
2.3: If n1 + n2 = a, return true, else false
Step 3: In main, print “All kaprekar no. less than 1000”
Step 4: Start a loop for i=4to i<1000, i increased by 1 every iteration, and if condition is true
print the no.
Step 5: END
Source code:
import java.util.Scanner;
for(int i=4;i<1000;i++)
if(isKaprekar(i)) //Checking whether kaprekar or not
System.out.println(i);
} //end of main
} //end of class
Variable Description: