Menghitung Nilai Factorial

Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 6

1.

Menghitung Nilai Faktorial

2. package tugas1;
3. import java.util.Scanner;
4. public class No01 {
5. public static void main(String[] args) {
6. //Menghitung nilai faktorial suatu bilangan (n)

7. Scanner in = new Scanner(System.in);


8. System.out.print("n = ");
9. int n = in.nextInt();
10. int fak=1;
11. System.out.print (n + "!= ");

12. for ( int i = 2; i<= n ; i++) {


13. fak = fak*i;

14. if (i<n) {
15. System.out.print( i +"x") ; }

16. else {
17. System.out.println( i+ " ");
18. System.out.print( n+ "!= " + fak);
}
}
}
}
19. Menghitung Nilai Fibonacci
package tugas1;
import java.util.Scanner;
package tugas1;
import java.util.Scanner;
public class No2 { // Menghitung nilai fibonacci
public static void main(String[] args) {
Scanner sc= new Scanner(System.in);
int fib1=0, fib2=1, fib3, n;
System.out.print("Mencetak fibonacci hingga elemen ke = ");
n = sc.nextInt();

System.out.println("Fib1 = " + fib1);


System.out.println("Fib2 = " + fib2);

for ( int i = 2; i<n ;) {

fib3 = fib1+fib2;
fib1 = fib2;
fib2 = fib3;
i = i+1;
if (i==2)
System.out.println("fib3 = " + fib3);

else {}
System.out.println("fib" + i + " = "+fib3);
}
}
}
3. Menentukan suatu bilangan prima atau tidak

package tugas1;
import java.util.Scanner;
public class No3 { // menentukan suatu bilangan prima atau tidak
public static void main(String[] args) {
// TODO Auto-generated method stub
String hasil = "Bilangan Prima";
int bil, bagi ;
Scanner sc= new Scanner(System.in);
System.out.print("Bil = ");
bil = sc.nextInt();

for ( bagi=2; bagi<bil ; bagi++) {


if (bil%bagi==0) {
bagi = bagi++;
hasil = "Bukan bilangan Prima" ;
System.out.println(hasil); }
else {
System.out.println(hasil );
}
break ;
}
}
}
4. Menghitung banyaknya pecahan mata uang untuk penggajian

package tugas1;
import java.util.Scanner;
public class No04 { // Menghitung banyaknya pecahan mata uang untuk penggajian
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int gaji, seratusribuan, limapuluhribuan, duapuluhribuan, sepuluhribuan,
limaribuan, duaribuan, seribuan, limaratusan,seratusan;

System.out.print("gaji =");
gaji = sc.nextInt();

seratusribuan = gaji/100000; gaji = gaji%100000;


limapuluhribuan = gaji/50000; gaji = gaji%50000;
duapuluhribuan = gaji/20000; gaji = gaji%20000;
sepuluhribuan = gaji/10000; gaji = gaji%10000;
limaribuan = gaji/5000; gaji = gaji%5000;
duaribuan = gaji/2000; gaji = gaji%2000;
seribuan = gaji/1000; gaji = gaji%1000;
limaratusan = gaji/500; gaji = gaji%500;
seratusan = gaji/100; gaji = gaji%100;

System.out.println("Seratusribuan = " + seratusribuan);


System.out.println("Limapuluhribuan = " + limapuluhribuan);
System.out.println("Duapuluhribuan = " + duapuluhribuan);
System.out.println("Sepuluhribuan = " + sepuluhribuan);
System.out.println("Limaribuan= " + limaribuan);
System.out.println("Duaribuan = " + duaribuan);
System.out.println("Seribuan = " + seribuan);
System.out.println("Limaratusan = " + limaratusan);
System.out.println("Seratusan = " + seratusan);
}
}
package tugas1;

5. Mengkonversi mata uang rupiah

package tugas1;
import java.util.Scanner;
public class No05 { // Mengkonversi mata uang rupiah
public static void main(String[] args) {

Scanner in = new Scanner(System.in);


float rp, usd, ugd, aud, rp_to_usd, rp_to_aud, rp_to_ugd;
System.out.print("1 USD = Rp ");
usd= in.nextInt();

System.out.print("1 UGD = Rp ");


ugd = in.nextInt();

System.out.print("1 AUD = Rp ");


aud = in.nextInt();

System.out.print("Rp");
rp = in.nextInt();

rp_to_usd = rp/usd;
rp_to_ugd = rp/ugd;
rp_to_aud = rp/aud;

System.out.println("Rp " + (rp) + " setara dengan USD $ " + rp_to_usd);


System.out.println("Rp " + (rp) + " setara dengan UGD $ " + rp_to_ugd);
System.out.println("Rp " + (rp) + " setara dengan AUD $ " + rp_to_aud);
}
}

You might also like