speecash

bisnis online, jual beli online, sistem pembayaran, pembayaran online, bisnis online

laman

Selasa, 06 Desember 2011

Prak Algo Modul 2 (java)





1.    Buatlah flowchart dan program pemilihan dengan switch case dengan semua jumlah batasan merupakan inputan dari keyboard !
Ø  Menampilkan deret bilangan ganjil dan menghitung jumlah seluruh deret yang ditampilkan
Ø  Program untuk menampilkan deret fibonaci.
OPI
Output :  Tampilan berupa menu pilihan :
1.      Menampilkan deret bilangan ganjil dan menghitung jumlah seluruh deret.
2.      Menampilkan deret fibonaci.
Setelah user memilih salah satu menu akan terjadi tampilan sesuai dengan menu yang dipilih
Proses :   Pilihan 1:
                     total = 0
                     diulang i = 1 sampai dengan N
                           diuji jika (N%2=1) maka
                                 cetak i, total = total +1
                     cetak total
               Pilihan 2 :
                     a = 1, b = 1
                     cetak a,b
                     d = N-2
                     selama (d>0)
                           c=a+b
                           cetak c
                           a=b,b=c
                           d- -
Input :    N dan pilihan
 
Flowchart IPO


Analisa
Input :    N = 8            pilihan = 1 , 2


Proses:    pilihan 1 à proses menampilkan deret bil ganjil dan jumlah deret
               total = 0
               proses literasi :
i
i%2=1
cetak i
total=total+i
1
Ya
ya
1
2
Tdk
tdk
-
3
Ya
ya
4
4
Tdk
tdk
-
5
Ya
ya
9
6
Tdk
tdk
-
7
Ya
ya
15
8
Tdk
tdk
-
              








               Deret = 1,3,5,7
               total = 16


               Pilihan à 2 proses menampilkan deret fibonaci
               a = 1, b =1
               d = N-2
               cetak a dan b
               proses literasi :         
d
d>=1
a
b
c
Catak c
6
ya
1
1
2
Ya
5
ya
1
2
3
Ya
4
ya
2
3
5
Ya
3
ya
3
5
8
Ya
2
ya
5
8
13
Ya
1
ya
8
13
21
ya
               Deret = 1,1,2,3,5,8,13,21

Source code
import java.io.*;
class soal1
{
                    public static void main(String[] args)throws Exception
                    {
                                DataInputStream input = new DataInputStream (System.in);
                                System.out.print("masukkan nilai = ");
                                String snilai = input.readLine();
                                int nilai;
                                int total=0;
                                nilai = Integer.parseInt(snilai);
                                System.out.println("menu pilihan = ");
System.out.println("\t1.menampilkan deret bilangan ganjil dan \n  \t  dan menghitung jumlah seluruh deret yang ditampilkan");
                                System.out.println("\t2.menampilkan deret fibonaci");
                                System.out.print("masukkan pilihan anda = ");
                                String spilihan = input.readLine();
                                int pilihan = Integer.parseInt(spilihan);
                                switch(pilihan)
                                {
                                                case 1:
                                                                int i;
                                                                for(i=1;i<=nilai;i++)
                                                                {
                                                                                if(i%2==1)
                                                                                {
                                                                                                System.out.print(+i);
                                                                                                System.out.print("\t");
                                                                                                total=total+i;
                                                                                }
                                                                }
                                                                System.out.println();
                                                                System.out.println("total nilai deret diatas adalah = "+total);
                                                                break;
                                                case 2:
                                                                int a = 1;
                                                                int b = 1;
                                                                int c;
                                                                System.out.print(+a);
                                                                System.out.print("\t");
                                                                System.out.print(+b);
                                                                System.out.print("\t");
                                                                int d=nilai-2;
                                                                while(d>=1)
                                                                {
                                                                                c=a+b;
                                                                                a=b;
                                                                                b=c;
                                                                                System.out.print(+c);
                                                                                System.out.print("\t");
                                                                                d--;
                                                                }
                                                                break;
                                                default :
                                                                System.out.println("pilihan yang anda masukkan salah");
                                                                break;
                                }
                    }
}

Tidak ada komentar:

Posting Komentar