Tugas Java Keenam

a. Buatlah program untuk menghitung 10 deret bilangan genap dengan hasilnya :

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


****************************************************************

 DO NOT COPY WITHOUT PERMISSION OF ADMIN

****************************************************************


Pembahasan :

A. Program untuk menghitung 10 deret bilangan genap

__________________________________________

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);
}
}

__________________________________________

output :

B. Program untuk menghitung 10 deret bilangan ganjil

__________________________________________

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);
}
}

__________________________________________

output :

C. Program untuk menampilkan deret fibonanci

__________________________________________

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++;
  }
}
}

__________________________________________

output :

masukan nilai awal 1 dan deret fibonacci 8

Hasilnya Seperti ini :


D. Program untuk menampilkan bilangan prima

__________________________________________

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 +" ");
 }
 }
 }
}

__________________________________________

output :





**************BELLARMINO W P***************

Comments

1968 Respond for "Tugas Java Keenam"

New Comments











Please refresh...

Smiley

aplikasi youtube downloader