Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add Kaprekar Number Detector #210

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
52 changes: 52 additions & 0 deletions KaprekarNumber.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
import java.util.Scanner;

/*
* A Kaprekar number is a number whose square when divided into two parts
* and such that sum of parts is equal to the original number and none of the parts has value 0.
* For example, 45 is a Kaprekar number, because 45^2 = 2025 and 20 + 25 = 45.
*/
public class KaprekarNumber {
public static void main(String[] args) {
// Initialize the scanner class to take input
Scanner sc = new Scanner(System.in);

// Prompt the user for input
System.out.print("Enter number to check if it is Kaprekar or not : ");

// Take input
int num = sc.nextInt();

// Check if number is Kaprekar and print the desired message
if (checkKaprekar(num))
System.out.println("This number is an Kaprekar Number");
else
System.out.println("This number is not an Kaprekar Number");
}

public static boolean checkKaprekar(int number){
// 1. Find square of number
int square = number * number;

// 2. Count number of digits in square
int count = 0;

// 3. Find sum of digits from right side
while (square != 0){
count++;
square /= 10;
}

// 4. Find sum of digits from left side
square = number * number;

// 5. Check if sum of digits is equal to original number
for (int i = 1; i < count; i++){
int first = (int) (square / Math.pow(10, i));
int second = (int) (square % Math.pow(10, i));
if (first + second == number && first != 0 && second != 0){
return true;
}
}
return false;
}
}