1
1
package com .thealgorithms .maths ;
2
2
3
- /* This is a program to check if a number is a Krishnamurthy number or not.
4
- A number is a Krishnamurthy number if the sum of the factorials of the digits of the number is equal
5
- to the number itself. For example, 1, 2 and 145 are Krishnamurthy numbers. Krishnamurthy number is
6
- also referred to as a Strong number.
3
+ /**
4
+ * Utility class for checking if a number is a Krishnamurthy number.
5
+ *
6
+ * 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.
7
+ *
8
+ * For more information, refer to the
9
+ * <a href="https://en.wikipedia.org/wiki/Strong_number">Krishnamurthy number</a> Wikipedia page.
10
+ *
11
+ * <b>Example usage:</b>
12
+ * <pre>
13
+ * boolean isKrishnamurthy = KrishnamurthyNumber.isKrishnamurthy(145);
14
+ * System.out.println(isKrishnamurthy); // Output: true
15
+ *
16
+ * isKrishnamurthy = KrishnamurthyNumber.isKrishnamurthy(123);
17
+ * System.out.println(isKrishnamurthy); // Output: false
18
+ * </pre>
7
19
*/
8
- import java .io .BufferedReader ;
9
- import java .io .IOException ;
10
- import java .io .InputStreamReader ;
11
-
12
20
public final class KrishnamurthyNumber {
21
+
13
22
private KrishnamurthyNumber () {
14
23
}
15
24
16
- // returns True if the number is a Krishnamurthy number and False if it is not.
17
-
18
- public static boolean isKMurthy (int n ) {
19
- // initialising the variable s that will store the sum of the factorials of the digits to 0
20
- int s = 0 ;
21
- // storing the number n in a temporary variable tmp
25
+ /**
26
+ * Checks if a number is a Krishnamurthy number.
27
+ *
28
+ * @param n The number to check
29
+ * @return true if the number is a Krishnamurthy number, false otherwise
30
+ */
31
+ public static boolean isKrishnamurthy (int n ) {
22
32
int tmp = n ;
33
+ int s = 0 ;
23
34
24
- // Krishnamurthy numbers are positive
25
35
if (n <= 0 ) {
26
36
return false ;
27
- } // checking if the number is a Krishnamurthy number
28
- else {
37
+ } else {
29
38
while (n != 0 ) {
30
39
// initialising the variable fact that will store the factorials of the digits
31
40
int fact = 1 ;
@@ -43,15 +52,4 @@ public static boolean isKMurthy(int n) {
43
52
return tmp == s ;
44
53
}
45
54
}
46
-
47
- public static void main (String [] args ) throws IOException {
48
- BufferedReader br = new BufferedReader (new InputStreamReader (System .in ));
49
- System .out .println ("Enter a number to check if it is a Krishnamurthy number: " );
50
- int n = Integer .parseInt (br .readLine ());
51
- if (isKMurthy (n )) {
52
- System .out .println (n + " is a Krishnamurthy number." );
53
- } else {
54
- System .out .println (n + " is NOT a Krishnamurthy number." );
55
- }
56
- }
57
55
}
0 commit comments