Write a program to find the factorial of number ?

This java program finds factorial of a number. Entered number is checked first if its negative then an error message is printed.


								
/**
 *
 * @author Mindclues
 */
public class Factorial {

    public static void main(String args[]) {
        int n, c, factorial = 1;

        System.out.println("Enter an integer(number) to calculate factorial.");
        Scanner in = new Scanner(System.in);

        n = in.nextInt();

        if (n < 0) {
            System.out.println("You are enter negative number.");
        } else {
            for (c = 1; c <= n; c++) {
                factorial = factorial * c;
            }

            System.out.println("Factorial of " + n + " is = " + factorial);
        }
    }
}
							

								
Enter an integer(number) to calculate factorial.
6
Factorial of 6 is = 720
							

Related Articles

post a comment