=====Latihan / Tugas=====
a. Buatlah program untuk menghitung 10 deret bilangan genap dengan hasilnya :
2 + 4 + 6 + 8 + 10 + 12 + 14 + 16 + 18 + 20 = 110
2 + 4 + 6 + 8 + 10 + 12 + 14 + 16 + 18 + 20 = 110
b. Buatlah program untuk menghitung 10 deret bilangan ganjil dengan hasilnya :
1 + 3 + 5 + 7 + 9 + 11 + 13 + 15 + 17 + 19 = 100
c. Buatlah program untuk menampilkan deret fibonanci, seperti dibawah ini :
1, 1, 2, 3, 5, 8, 13, 21
d. Buatlah program untuk menampilkan bilangan prima, seperti dibawah ini :
2, 3, 5, 7, 11, 13, 17, 19
=====Source code Latihan =====
A.Source Code :
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);
}
}
B.Source Code :
import java.util.Scanner;
public class ganjil {
public static void main(String[] args) {
@SuppressWarnings({ "resource", "unused" })
Scanner sclim = new Scanner(System.in);
System.out.println("Deret Bilangan Ganjil antara 1 - 19 = ");
System.out.println("-------");
int sum = 0, number = 1;
while(number<=19){
if((number!=0)&&(number%2==1))
sum= sum + number;
if((number!=0)&&(number%2==1))
System.out.println(number);
number++;
}
System.out.println("-------");
System.out.printf("Jumlah = %d " , sum);
}
}
C.Source Code :
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++;
}
}
}
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.Source Code :
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 +" ");
}
}
}
}
"SEKIAN POST BLOG SAYA HARI INI"
Tidak ada komentar:
Posting Komentar