Kaprekar Number Program in Java and Python
Kaprekar Number program with algorithm, explanation, Java solution and simple Python solution for ICSE and ISC students.
Question:
Write a program to input a number and check whether it is a Kaprekar number or not.
Note: A positive whole number ‘n’ that has ‘d’ number of digits is squared and split into two pieces, a right-hand piece that has ‘d’ digits and a left-hand piece that has remaining ‘d’ or ‘d-1’ digits.
If the sum of the two pieces is equal to the number, then ‘n’ is a Kaprekar number. The first few Kaprekar numbers are: 9, 45, 297 ……..
Example 1: 9
92 = 81, right-hand piece of 81 = 1 and left hand piece of 81 = 8
Sum = 1 + 8 = 9, i.e. equal to the number. Hence, 9 is a Kaprekar number.
Example 2: 45
452 = 2025, right-hand piece of 2025 = 25 and left hand piece of 2025 = 20
Sum = 25 + 20 = 45, i.e. equal to the number. Hence, 45 is a Kaprekar number.
Example 3: 297
2972 = 88209, right-hand piece of 88209 = 209 and left hand piece of 88209 = 88
Sum = 209 + 88 = 297, i.e. equal to the number. Hence, 297 is a Kaprekar number.
Algorithm:
Step 1: Start.
Step 2: Accept the number n.
Step 3: Find sq = n × n.
Step 4: Convert sq to a string so it can be split at different digit positions.
Step 5: Initialize a flag to 0.
Step 6: For every possible split position in the square string, form the left part and the right part.
Step 7: Convert the left and right parts into numbers, treating an empty left part as 0.
Step 8: Add the two parts and compare the sum with n.
Step 9: If the sum is equal to n, set the flag and stop checking further splits.
Step 10: Display whether n is a Kaprekar number according to the flag.
Step 11: Stop.
Explanation:
A Kaprekar number is checked by squaring the number and splitting the square into two parts. The number is Kaprekar if the sum of those parts is equal to the original number.
The program first calculates the square and converts it to a string. A string is useful here because the square must be split at different digit positions.
For every possible split, the left part and right part are converted back to numbers. Empty left parts are treated as 0 so that small squares can also be handled.
If any split gives a sum equal to the original number, the flag is set and the number is reported as Kaprekar. Otherwise, after all splits fail, it is not a Kaprekar number.
The important concept is that a Kaprekar number is tested through its square, not through the number alone. After squaring the number, the square is split into two parts. The right part normally contains as many digits as the original number, while the left part contains the remaining digits. The two parts are then added. If their sum gives the original number, the number is Kaprekar. The program must be careful with powers of 10 or string lengths so that the split happens at the correct position, especially for numbers with different digit counts.
Java Program:
/**
* The class Kaprekar inputs a number and checks whether it is a Kaprekar Number or not
* @author : www.guideforschool.com
* @Program Type : BlueJ Program - Java
*/
import java.util.Scanner;
class Kaprekar
{
public static void main(String args[])
{
Scanner sc = new Scanner(System.in);
System.out.print("Enter a Number : ");
int n = sc.nextInt(); //Inputting the number
int sq = n*n; //finding the square of the number
String s = Integer.toString(sq); //converting the square into a String
if(sq<=9)
s = "0"+s; //Adding a zero in the beginning if the square is of single digit
int l = s.length(); //finding the length (i.e. no. of digits in the square).
int mid = l/2; //finding the middle point
String left=s.substring(0,mid); //extracting the left digits from the square
String right=s.substring(mid); //extracting the right digits from the square
int x = Integer.parseInt(left); //converting the left String into Integer
int y = Integer.parseInt(right); //converting the right String into Integer
//if sum of left and right numbers is equal to the original number then it is a Kaprekar number
if(x+y == n)
System.out.println(n+" is a Kaprekar Number");
else
System.out.println(n+" is Not a Kaprekar Number");
}
}Equivalent Python Program:
# Read the number and keep any required copy for digit or divisor processing.
# Loops and conditions implement the number-property test step by step.
# Display the result according to the flag/counter/calculated value.
n = int(input("Enter a number: "))
square = n * n
s = str(square)
if square <= 9:
s = "0" + s
mid = len(s) // 2
left = s[0:mid]
right = s[mid:]
x = int(left)
y = int(right)
if x + y == n:
print(n, "is a Kaprekar Number")
else:
print(n, "is not a Kaprekar Number")Output:
Leave a Reply
Your email address will not be published. Comments are reviewed before appearing publicly.