program java
PERTEMUAN 6
a. Buatlah program untuk menghitung 10 deret bilangan genap dengan hasilnya :
2 + 4 + 6 + 8 + 10 + 12 + 14 + 16 + 18 + 20 = 110
import java.util.Scanner;
public class genap {
public static void main(String[] args) {
@SuppressWarnings({ "resource", "unused" })
Scanner sclim = new Scanner(System.in);
System.out.println("Deret Bilangan Genap antara 2 - 20 = ");
System.out.println("-----");
int sum = 0, number = 1;
while(number<=20){
if((number!=1)&&(number%2==0))
sum= sum + number;
if((number!=1)&&(number%2==0))
System.out.println(number);
number++;
}
System.out.println("-----");
System.out.printf("Jumlah = %d " , sum);
}
}
Hasil Output
b. Buatlah program untuk menghitung 10 deret bilangan ganjil dengan hasilnya :
1 + 3 + 5 + 7 + 9 + 11 + 13 + 15 + 17 + 19 = 100
import java.util.Scanner;
public class ganjil {
HASIL OUTPUTNYA :
C. Buatlah program untuk menampilkan deret fibonanci, seperti dibawah ini :
1, 1, 2, 3, 5, 8, 13, 21
import javax.swing.JOptionPane;
public class Fibonacci
{
public static void main(String[]args)
{
int a = Integer.parseInt(JOptionPane.showInputDialog("Masukkan nilai awal Deret Fibonacci"));
int b = a;
int c = Integer.parseInt(JOptionPane.showInputDialog("Masukkan deret Fibonacci yang Anda Inginkan: "));
int d = c-1;
int e = 1;
System.out.print(a + " ");
while(e <=d)
{
System.out.print(a +" ");
a = a+b;
b = a-b;
e++;
}
}
}
Outputnya public class Fibonacci
{
public static void main(String[]args)
{
int a = Integer.parseInt(JOptionPane.showInputDialog("Masukkan nilai awal Deret Fibonacci"));
int b = a;
int c = Integer.parseInt(JOptionPane.showInputDialog("Masukkan deret Fibonacci yang Anda Inginkan: "));
int d = c-1;
int e = 1;
System.out.print(a + " ");
while(e <=d)
{
System.out.print(a +" ");
a = a+b;
b = a-b;
e++;
}
}
}
d. Buatlah program untuk menampilkan bilangan prima, seperti dibawah ini :
2, 3, 5, 7, 11, 13, 17, 19
public class BilPrima {
public static void main(String args[]){
int max = 19;
boolean isPrima = false;
for(int i=2; i<=max; i++){
isPrima = true;
for(int j=2; j<i; j++){
if(i % j == 0){
isPrima = false;
break;
}
}
if(isPrima){
System.out.print(i +" ");
}
}
}
}
Outputnya public static void main(String args[]){
int max = 19;
boolean isPrima = false;
for(int i=2; i<=max; i++){
isPrima = true;
for(int j=2; j<i; j++){
if(i % j == 0){
isPrima = false;
break;
}
}
if(isPrima){
System.out.print(i +" ");
}
}
}
}
SOURCE CODE :
import java.util.*;
class TugasPertemuan7
{
public static
void main (String[] args) throws Exception
{
String[]
kode = new String[10];
String[]
jenis = new String[10];
int
i,j;
double
jumlah=0 ,pjk=0, total=0;
int[]
potong = new int[10];
int[]
harga = new int[10];
int[]
jml = new int[10];
Scanner
input = new Scanner(System.in);
System.out.println("|-------------------------------|");
System.out.println("|NAMA :
AZURA|");
System.out.println("|KELAS :
14S01 |");
System.out.println("|-------------------------------|");
System.out.println();
System.out.println("|-------------------------|");
System.out.println("| GEROBAK
FRIED CHICKEN |");
System.out.println("|-------------------------|");
System.out.println("|
Kode | Jenis | Harga |");
System.out.println("|-------|-------|----------|");
System.out.println("| D |
Dada | Rp.2500 |");
System.out.println("| P |
Paha | Rp.2000 |");
System.out.println("| S |
Sayap | Rp.1500 |");
System.out.println("|-------------------------|");
System.out.println();
System.out.print("Banyak
Jenis : ");
j=input.nextInt();
for(i=0;
i<j; i++)
{
System.out.println("\nJenis
Ke - " + (i+1));
System.out.print("Jenis
Potong [D/P/S] : ");
kode[i]=
input.next();
if
("D".equals(kode[i]) || "d".equals(kode[i]))
{
jenis[i]="Dada";
harga[i]=2500;
}
else
if ("P".equals(kode[i]) || "p".equals(kode[i]))
{
jenis[i]="Paha";
harga[i]=2000;
}
else
{
jenis[i]="Sayap";
harga[i]=1500;
}
System.out.print("Banyak
Potong : ");
potong[i]
= input.nextInt();
jml[i]=harga[i]*potong[i];
}
System.out.println("\n\tGEROBAK
FRIED
CHICKEN ");
System.out.println("---------------------------------------------");
System.out.println("No.\tJenis\tHarga\tBanyak\tJumlah");
System.out.println("\tPotong\tSatuan\tBeli\tHarga
");
System.out.println("---------------------------------------------");
for(i=0;i<j;i++)
{
System.out.println(+(i+1)+
"\t" +jenis[i]+ "\t" +harga[i]+ "\t" +potong[i]+
"\t\t" +jml[i]);
jumlah=jumlah+jml[i];
}
System.out.println("---------------------------------------------");
pjk=jumlah*0.1;
total=jumlah+pjk;
System.out.println("\tJumlah
Bayar\t\t\t" +jumlah);
System.out.println("\tPajak
10%\t\t\t\t" +pjk);
System.out.println("\tTotal
Bayar\t\t\t\t" +total);
}
}
Outputnya
Komentar
Posting Komentar