Repository: TheAlgorithms/Java
Pull request #2881 (2021-12-14 18:59:50)

Added KaprekarNumbersTest
### **Describe your change:**

- [ ] Add an algorithm?
- [ ] Fix a bug or typo in an existing algorithm?
- [ ] Documentation change?

#### References

<!-- Add any reference to previous pull-request or issue -->

### **Checklist:**

<!-- Remove items that do not apply. For completed items, change [ ] to [x]. -->

- [ ] I have read [CONTRIBUTING.md](https://github.com/TheAlgorithms/Java/blob/master/CONTRIBUTING.md).
- [ ] This pull request is all my own work -- I have not plagiarized.
- [ ] I know that pull requests will not be merged if they fail the automated tests.
- [ ] This PR only changes one algorithm file. To ease review, please open separate PRs for separate algorithms.
- [ ] All new Java files are placed inside an existing directory.
- [ ] All filenames are in all uppercase characters with no spaces or dashes.
- [ ] All functions and variable names follow Java naming conventions.
- [ ] All new algorithms have a URL in its comments that points to Wikipedia or other similar explanation.
- [ ] If this pull request resolves one or more open issues then the commit message contains `Fixes: #{$ISSUE_NO}`.



Patch:
diff --git a/src/main/java/com/thealgorithms/maths/KaprekarNumbers.java b/src/main/java/com/thealgorithms/maths/KaprekarNumbers.java
new file mode 100644
index 00000000..c922ab25
--- /dev/null
+++ b/src/main/java/com/thealgorithms/maths/KaprekarNumbers.java
@@ -0,0 +1,27 @@
+package com.thealgorithms.maths;
+
+public class KaprekarNumbers {
+
+	/* This program demonstrates if a given number is Kaprekar Number or not.
+	Kaprekar Number: A Kaprekar number is an n-digit number which its square can be split into two parts where the right part has n
+	digits and sum of these parts is equal to the original number. */
+
+	// Checks whether a given number is Kaprekar Number or not
+
+	public static boolean isKaprekarNumber(long number) {			
+		long numberSquared = number * number;
+		if(Long.toString(number).length() == Long.toString(numberSquared).length()){
+			return (number == numberSquared);
+		}
+		else{
+			long leftDigits1 = 0, leftDigits2 = 0;
+			if(Long.toString(numberSquared).contains("0")){
+				leftDigits1 = Long.parseLong(Long.toString(numberSquared).substring(0, Long.toString(numberSquared).indexOf("0")));
+			}
+			leftDigits2 = Long.parseLong(Long.toString(numberSquared).substring(0, (Long.toString(numberSquared).length() - Long.toString(number).length())));
+			long rightDigits = Long.parseLong(Long.toString(numberSquared).substring(Long.toString(numberSquared).length() - Long.toString(number).length()));
+			return (number == (leftDigits1 + rightDigits)) || (number == (leftDigits2 + rightDigits));
+		}		
+	}
+
+}


Test patch:
diff --git a/src/test/java/com/thealgorithms/maths/KaprekarNumbersTest.java b/src/test/java/com/thealgorithms/maths/KaprekarNumbersTest.java
new file mode 100644
index 00000000..04f8cc09
--- /dev/null
+++ b/src/test/java/com/thealgorithms/maths/KaprekarNumbersTest.java
@@ -0,0 +1,58 @@
+package com.thealgorithms.maths;
+
+import org.junit.jupiter.api.Test;
+
+import static org.junit.jupiter.api.Assertions.*;
+
+public class KaprekarNumbersTest {
+
+	@Test
+	void testFor1() 
+	{
+		assertTrue(KaprekarNumbers.isKaprekarNumber(1));
+	}
+
+	@Test
+	void testFor45() 
+	{
+		assertTrue(KaprekarNumbers.isKaprekarNumber(45));
+	}
+
+	@Test
+	void testFor297() 
+	{
+		assertTrue(KaprekarNumbers.isKaprekarNumber(297));
+	}
+
+	@Test
+	void testFor2223() 
+	{
+		assertTrue(KaprekarNumbers.isKaprekarNumber(2223));
+	}
+
+	@Test
+	void testFor857143() 
+	{
+		assertTrue(KaprekarNumbers.isKaprekarNumber(857143));
+	}
+
+
+	@Test
+	void testFor3() 
+	{
+		assertFalse(KaprekarNumbers.isKaprekarNumber(3));
+	}
+
+	@Test
+	void testFor26() 
+	{
+		assertFalse(KaprekarNumbers.isKaprekarNumber(26));
+	}
+
+	@Test
+	void testFor98() 
+	{
+		assertFalse(KaprekarNumbers.isKaprekarNumber(98));
+	}
+
+}
\ No newline at end of file