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 Peterson number in Java #72

Merged
merged 1 commit into from
Feb 19, 2022
Merged
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
66 changes: 66 additions & 0 deletions Basics/PetersonNumber/PetersonNumber.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
package Basics.PetersonNumber;

import java.util.Scanner;

public class PetersonNumber {
/**
* Peterson Number is that sum of each digit's factorial equals to number
* ex) 145
* 1! + 4! + 5!
* 1 + 24 + 120 = 145
* Peteson Number!
*
* @author Royce
* @since 2022.02.19
*/

public static void main(String[] args) {
Scanner input = new Scanner(System.in);
System.out.print("Enter Number : ");

String userInput = input.nextLine();
try {
int number = Integer.parseInt(userInput);
checkPositive(number);
} catch (NumberFormatException e) {
System.out.println("Number must be integer more than 1");
return;
}

System.out.println(
isPetersonNumber(userInput)
);


}

private static void checkPositive(int number) {
if (number <= 0) {
throw new NumberFormatException();
}
}

private static String isPetersonNumber(String userInput) {
int userNumber = Integer.parseInt(userInput);
String[] digits = userInput.split("");
int sum = 0;
for (String digit : digits) {
sum += factorial(digit);
}

if (sum == userNumber) {
return userNumber + " is Peterson Number!";
}
return userNumber + " is not Peterson Number!";
}

private static int factorial(String digit) {
int num = Integer.parseInt(digit);
int ans = 1;
for (int i = num; i > 0; i--) {
ans *= i;
}
return ans;
}

}