How to Find Factorial of Given Number in Java

This is the simple java exercise of math problem. The java program asked user for any number and then finds its factorial e.g. 5! = 1 * 2 * 3 * 4 * 5 = 120

Source Code  

/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package newpackage.more;
import java.util.Scanner;

/**
 *
 * @author Shan
 */
public class factorial {

/**
     * @param args the command line arguments
     */
public static void main(String[] args) {
int yourNember, incrementor, factorial = 1;

System.out.println("Enter your number ");
Scanner in = new Scanner(System.in);

yourNember = in.nextInt();

if ( yourNember < 0 )
System.out.println("-Negative Number Error");
else
{
for ( incrementor = 1 ; incrementor <= yourNember ; incrementor++ )
factorial = factorial*incrementor;
System.out.println("Factorial of "+yourNember+" is = "+factorial);
}
}
}

Output o the Program

Find Factorial of Given Number in Java


Share on Google Plus

About Asad

Asad Niazi is Software Engineer , Programmer, Web Developer and a young mentor of BloggersTown and PProgramming. Asad Love to writes about Technology, Programming, Blogging and make money online.
    Blogger Comment
    Facebook Comment

0 comments:

Post a Comment