Tuesday, January 22, 2013

Nilai Faktorial (n!) pada Java

BY Populeritas No comments


Program  Menghitung Nilai Faktorial (n!), nilai n diinputkan oleh user melalui keyboard



import javax.swing.*;
class factorial{
public static void main(String [] args){
int angka=Integer.parseInt(JOptionPane.showInputDialog("Input angka : "));
int hasil =1;
for (int i=1; i<=angka; i++){
hasil = hasil * i;
if (i == angka){
System.out.print(i +"="+hasil);
}
else{
System.out.print(i +"x");
}
}
}
}


  
Output Program

  

Penjelasan Output Program


import javax.swing.*;
class a
{
public static void main(String [] args){
int angka=Integer.parseInt(JOptionPane.showInputDialog("Input angka : "));
int hasil =0; int x;
for (int i=0; i<=angka; x=i+2){
hasil = hasil + i;
if (i == angka){
System.out.print(i +"="+hasil);
}
else{
System.out.print(x +"x");
}
}
}
}

0 comments: