mirror of
https://github.com/TheAlgorithms/Java.git
synced 2025-07-06 09:06:51 +08:00
Remove main function, improve docstring, add JUnit tests for KrishnamurthyNumber
. (#5881)
This commit is contained in:
@ -1,31 +1,38 @@
|
||||
package com.thealgorithms.maths;
|
||||
|
||||
/* This is a program to check if a number is a Krishnamurthy number or not.
|
||||
A number is a Krishnamurthy number if the sum of the factorials of the digits of the number is equal
|
||||
to the number itself. For example, 1, 2 and 145 are Krishnamurthy numbers. Krishnamurthy number is
|
||||
also referred to as a Strong number.
|
||||
/**
|
||||
* Utility class for checking if a number is a Krishnamurthy number.
|
||||
*
|
||||
* A Krishnamurthy number (also known as a Strong number) is a number whose sum of the factorials of its digits is equal to the number itself.
|
||||
*
|
||||
* For example, 145 is a Krishnamurthy number because 1! + 4! + 5! = 1 + 24 + 120 = 145.
|
||||
* <b>Example usage:</b>
|
||||
* <pre>
|
||||
* boolean isKrishnamurthy = KrishnamurthyNumber.isKrishnamurthy(145);
|
||||
* System.out.println(isKrishnamurthy); // Output: true
|
||||
*
|
||||
* isKrishnamurthy = KrishnamurthyNumber.isKrishnamurthy(123);
|
||||
* System.out.println(isKrishnamurthy); // Output: false
|
||||
* </pre>
|
||||
*/
|
||||
import java.io.BufferedReader;
|
||||
import java.io.IOException;
|
||||
import java.io.InputStreamReader;
|
||||
|
||||
public final class KrishnamurthyNumber {
|
||||
|
||||
private KrishnamurthyNumber() {
|
||||
}
|
||||
|
||||
// returns True if the number is a Krishnamurthy number and False if it is not.
|
||||
|
||||
public static boolean isKMurthy(int n) {
|
||||
// initialising the variable s that will store the sum of the factorials of the digits to 0
|
||||
int s = 0;
|
||||
// storing the number n in a temporary variable tmp
|
||||
/**
|
||||
* Checks if a number is a Krishnamurthy number.
|
||||
*
|
||||
* @param n The number to check
|
||||
* @return true if the number is a Krishnamurthy number, false otherwise
|
||||
*/
|
||||
public static boolean isKrishnamurthy(int n) {
|
||||
int tmp = n;
|
||||
int s = 0;
|
||||
|
||||
// Krishnamurthy numbers are positive
|
||||
if (n <= 0) {
|
||||
return false;
|
||||
} // checking if the number is a Krishnamurthy number
|
||||
else {
|
||||
} else {
|
||||
while (n != 0) {
|
||||
// initialising the variable fact that will store the factorials of the digits
|
||||
int fact = 1;
|
||||
@ -43,15 +50,4 @@ public final class KrishnamurthyNumber {
|
||||
return tmp == s;
|
||||
}
|
||||
}
|
||||
|
||||
public static void main(String[] args) throws IOException {
|
||||
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
|
||||
System.out.println("Enter a number to check if it is a Krishnamurthy number: ");
|
||||
int n = Integer.parseInt(br.readLine());
|
||||
if (isKMurthy(n)) {
|
||||
System.out.println(n + " is a Krishnamurthy number.");
|
||||
} else {
|
||||
System.out.println(n + " is NOT a Krishnamurthy number.");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
Reference in New Issue
Block a user