Tugas Pertemuan ke 6



1. Buatlah program untukmenghitung 10 deretbilangangenap dengan hasilnya :
2 + 4 + 6 + 8 + 10 + 12 + 14 + 16 + 18 + 20 = 110

importjava.util.Scanner;
public class bilangan_genap
{
public static void main(String[] args) {
@SuppressWarnings({ "resource", "unused" })
Scanner sclim = new Scanner(System.in);
System.out.println("DeretBilanganGenapantara 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);
}
}


Output :





2. Buatlah program untukmenghitung 10 deretbilanganganjil dengan hasilnya :
1 + 3 + 5 + 7 + 9 + 11 + 13 + 15 + 17 + 19 = 100

importjava.util.Scanner;
public class bilangan_ganjil
{
public static void main(String[] args) {
@SuppressWarnings({ "resource", "unused" })
Scanner sclim = new Scanner(System.in);
System.out.println("DeretBilanganGanjilantara 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);
}
}


Output:

3. Buatlah program untukmenampilkanderetfibonanci, sepertidibawahini :
1, 1, 2, 3, 5, 8, 13, 21


Output:


4. Buatlah program untukmenampilkanbilangan prima, sepertidibawahini :
2, 3, 5, 7, 11, 13, 17, 19

public class Bilangan_Prima
{
public static void main(String args[]){
int max = 19;
booleanisPrima = false;

for(inti=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 +" ");
 }
 }
 }
}


Output:


Komentar

Postingan populer dari blog ini

Menghitung Luas dan Keliling Lingkaran

GEROBAK FRIED CHICKEN

Pemograman Java