JavaAlgorithms/Factorial.java
zacharyjones123 03dbfa78c8 - Closed all Scanner and Buffered Readers
- Added Some Java Doc

- ReverseString - took space out of between each letter so "Zachary" to
"yrahcaZ" instead of "y r a h c aZ"

- bfs - Trying to repair this class but need to research bfs more

- dfs - Trying to repair this class but need to research dfs more

- Added new Classes
1) OctalToBinary - Not done
2) BinaryToOctal - Not done
3) OctalToDecimal - Not done
4) Graphs

-Added the dataStructure Graphs (Unfinished)
2017-04-20 11:56:21 -07:00

53 lines
1.1 KiB
Java

import java.util.Scanner;
/**
* This program will print out the factorial of any non-negative
* number that you input into it.
*
* @author Unknown
*
*/
public class Factorial{
/**
* The main method
*
* @param args Command line arguments
*/
public static void main(String[] args){
Scanner input = new Scanner(System.in);
//Prompt user to enter integer
System.out.print("Enter a non-negative integer: ");
//Proceed with factorial calculation only if inputted number is not negative
if(input.hasNextInt()){
int number = input.nextInt();
if (number < 0){
System.out.print("Cannot execute. Please enter a non-negative integer: ");
number = input.nextInt();
} else {
//Output of factorial for any non-negative number
System.out.println("The factorial of "+number+" will yield: "+factorial(number));
}
}
input.close();
}
/**
* Recursive Factorial Method
*
* @param n The number to factorial
* @return The factorial of the number
*/
public static long factorial(int n){
if (n==0){
return 1;
} else if (n==1){
return 1;
} else {
return n * factorial(n-1);
}
}
}