Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.util.Scanner;
- public class Factorial {
- /**
- * Program: Factorial.java
- * Purpose: Yahoo! Answers. Find factorial of numbers between 1 and 12.
- * Notes: For bigger numbers, see my BigFactorial program
- * Creator: Chris Clarke
- * Created: 22.05.2012
- */
- public static void main(String[] args) {
- Scanner keyboard = new Scanner(System.in);
- System.out.print( "Enter a number (between 1 and 12) for factorial: ");
- int n = keyboard.nextInt();
- if (n>12) {
- System.out.println ("Number too big!");
- } else if (n<1) {
- System.out.println ("Number too small!");
- } else {
- System.out.println( n + "! = " + factorial(n));
- }
- }
- public static int factorial(int n) {
- return (n == 1) ? 1 : n * factorial(n - 1);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement