[java] Menentukan Faktorial suatu bilangan (n!)

METNUM

|Tugas METNUM: Halaman 130 no.5

Buatlah program yang meminta sebuah bilangan N, lalu menampilkan N! (factorial dari N) dengan N!= N * (N-1) * (N-2) * … “ 1 dan 0! = 1.

Jawab

//irwananwar.wordpress.com
import java.util.Scanner;
public class loop5_faktorial {
	public static void main(String[] args) {
		Scanner s=new Scanner(System.in);
		System.out.printf("=====faktorial=====\n");
		System.out.printf("Input sebuah bilangan : ");
		int n=s.nextInt();
		int b=1;
		if(n>0){
			for(int i=1;i<n;i++)
			b=b*(n-i);
			b=b*n;
		}
		else
			b=1;
		System.out.printf("\t"+b);
	}
}

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s