1. Menghitung Bilangan Faktorial

Algoritma

Judul : BilanganFaktorial

Spesifikasi : Program akan menghitung jumlah bilangan faktorial dan akan menampilkan ke monitor

Deklarasi (kamus) :

input : String

nFaktorial : int

jumlahBilFaktorial : int

i : int

Deskripsi :

write(“Program Menghitung Bilangan Faktorial”)

read(“n = “, nFaktorial)

if nFaktorial = 1 then

jumlahBilFaktorial <– 1

else

for i = 1 to nFaktorial

jumlahBilFaktorial <– jumlahBilFaktorial * i

write(“Hasil = “, jumlahBilFaktorial)

Program

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package bilanganfaktorial;

/**
 *
 * @author eko rudiawan
 */
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.io.IOException;

public class BilanganFaktorial {

/**
 * @param args the command line arguments
 */
 public static void main(String[] args) throws IOException {
 // TODO code application logic here
 BufferedReader dataIn = new BufferedReader(new InputStreamReader(System.in));
 String input;
 int nFaktorial = 0;
 int jumlahBilFaktorial = 1;
 int i;
 System.out.println("Program Menghitung Bilangan Faktorial");
 System.out.print("n = ");
 try {
 input = dataIn.readLine();
 nFaktorial = Integer.parseInt(input);
 } catch (NumberFormatException error) {
 System.err.println("Input hanya berupa bilangan bulat !!");
 }
 if (nFaktorial == 1){
 jumlahBilFaktorial=1;
 }
 else {
 for(i=1; i<=nFaktorial; i++){
 jumlahBilFaktorial=jumlahBilFaktorial*(i);
 }
 }
 System.out.println("Hasil = " + jumlahBilFaktorial);
 }
}

2. Bilangan Febonacci

Algoritma

Judul : Menampilkan Bilangan Febonacci

Spesifikasi :

Deklarasi (kamus) :

banyakBilangan : int

a : int

b : int

c : int

i : int

Deskripsi :

banyakBilangan <– 0

a <– 0

b <– 1

write(“Bilangan Febonacci”)

read(“Masukkan banyak bilangan = “, banyakBilangan)

write(“Bilangan Febonacci = “, a , “ “, b)

for i = 0 to banyakBilangan – 2

c  <– a+b

write(c,” “ )

a <– b

b <– c

Program

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package menghitungfibonacci;

/**
 *
 * @author eko rudiawan
 */
import java.io.*;

public class MenghitungFibonacci {

/**
 * @param args the command line arguments
 */
 public static void main(String[] args) throws IOException {
 // TODO code application logic here
 BufferedReader dataInput = new BufferedReader(new InputStreamReader(System.in));
 String input;
 int banyakBilangan = 0;
 int a = 0;
 int b = 1;
 int c;
 int i;
 System.out.println("Bilangan Fibonacci");
 System.out.print("Masukkan banyak bilangan = ");
 try {
 input = dataInput.readLine();
 banyakBilangan = Integer.parseInt(input);
 } catch (NumberFormatException error) {
 System.out.println("Error !!");
 }
 System.out.print("Bilangan Fibonacci = " + a + " " + b + " ");
 for (i = 0; i < banyakBilangan - 2; i++) {
 c = a + b;
 System.out.print(c + " ");
 a = b;
 b = c;
 }
 System.out.println("");
 }
}

3. Menentukan Polindrom

Algoritma

Judul : Menentukan Polindrom

Spesifikasi :

Deklarasi :

kata1[] : char

panjangString : int

polindrom : Boolean

i : int

j : int

Deskripsi :

write(“Program Menentukan Polindrom”)

read(“Masukkan kata = “,input)

panjangString <– input1.length

kata1 <– input1.toCharArray

j <– panjangString-1

polindrom <– true

for i = 0 to panjangString/2

if kata[i] != kata[j] then

polindrom <– false

if polindrom = true then

write(“POLINDROM”)

else

write(“TIDAK POLINDROM”)

Program

</pre>
/*

* To change this template, choose Tools | Templates

* and open the template in the editor.

*/

package menentukanpolindrom;

&nbsp;

/**

*

* @author eko rudiawan

*/

import java.io.*;

&nbsp;

public class MenentukanPolindrom {

&nbsp;

/**

* @param args the command line arguments

*/

public static void main(String[] args) throws IOException {

// TODO code application logic here

BufferedReader dataInput = new BufferedReader(new InputStreamReader(System.in));

String input1;

char kata1[];

int panjangString;

boolean polindrom;

System.out.println("Program Menentukan Polindrom");

System.out.println("Masukkan Kata = ");

input1 = dataInput.readLine();

panjangString = input1.length();

kata1 = input1.toCharArray();

int j = panjangString - 1;

polindrom = true;

for (int i = 0; i < panjangString / 2; i++) {

if (kata1[i] != kata1[j]) {

polindrom = false;

}

j--;

}

if (polindrom) {

System.out.println("POLINDROM");

} else {

System.out.println("TIDAK POLINDROM");

}

}

}
<pre>

4. Menghitung Luas Segitiga, Lingkaran, dan Persegi

Algoritma

Judul : Menghitung Luas Segitiga, Lingkaran, dan Persegi

Spesifikasi :

Deklarasi (kamus) :

pilihan : int

luas : double

panjang : int

lebar : int

jariJari : int

alas : int

tinggi : int

Deskripsi :

write(“Program Mencari Luas Segitiga”)

write(“1. Luas Segitiga”)

write(“2. Luas Lingkaran”)

write(“3. Luas Persegi”)

read(“Masukkan Pilihan”, pilihan)

if pilihan = 1 then

read(“Masukkan nilai alas = “, alas)

read(“Masukkan nilai tinggi = “,tinggi)

luas <– 0.5 * alas * tinggi

else if pilihan = 2 then

read(“Masukkan nilai R = “, jariJari)

luas <– 3.14 * jariJari * jariJari

else if pilihan = 3 then

read(“Masukkan nilai panjang = “, panjang)

read(“Masukkan nilai lebar = “, lebar)

luas <– panjang * lebar

else

write(“Pilihan Tidak Tersedia”)

write(“Luas = “,luas)

Program

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package menghitungluas;

/**
 *
 * @author eko rudiawan
 */
import java.io.*;

public class MenghitungLuas {

/**
 * @param args the command line arguments
 */
 public static void main(String[] args) throws IOException {
 // TODO code application logic here
 BufferedReader dataInput = new BufferedReader(new InputStreamReader(System.in));
 String input;
 int pilihan = 0;
 double luas = 0;
 int panjang = 0;
 int lebar = 0;
 int jariJari = 0;
 int alas = 0;
 int tinggi = 0;
 System.out.println("Program Mencari Luas");
 System.out.println("1. Luas Segitiga");
 System.out.println("2. Luas Lingkaran");
 System.out.println("3. Luas Persegi");
 System.out.print("Masukkan Pilihan : ");
 try {
 input = dataInput.readLine();
 pilihan = Integer.parseInt(input);
 } catch (NumberFormatException error) {
 System.out.println("Input hanya berupa angka !!");
 }
 if (pilihan == 1) {
 System.out.print("Masukkan nilai alas = ");
 try {
 input = dataInput.readLine();
 alas = Integer.parseInt(input);
 } catch (NumberFormatException error) {
 System.out.println("Input hanya berupa angka !!");
 }
 System.out.print("Masukkan nilai tinggi = ");
 try {
 input = dataInput.readLine();
 tinggi = Integer.parseInt(input);
 } catch (NumberFormatException error) {
 System.out.println("Input hanya berupa angka !!");
 }
 luas = 0.5 * alas * tinggi;
 } else if (pilihan == 2) {
 System.out.print("Masukkan nilai R = ");
 try {
 input = dataInput.readLine();
 jariJari = Integer.parseInt(input);
 } catch (NumberFormatException error) {
 System.out.println("Input hanya berupa angka !!");
 }
 luas = 3.14 * jariJari * jariJari;
 } else if (pilihan == 3) {
 System.out.print("Masukkan nilai panjang = ");
 try {
 input = dataInput.readLine();
 panjang = Integer.parseInt(input);
 } catch (NumberFormatException error) {
 System.out.println("Input hanya berupa angka !!");
 }
 System.out.print("Masukkan nilai lebar = ");
 try {
 input = dataInput.readLine();
 lebar = Integer.parseInt(input);
 } catch (NumberFormatException error) {
 System.out.println("Input hanya berupa angka !!");
 }
 luas = panjang * lebar;
 } else {
 System.out.println("Pilihan Tidak Tersedia !");
 }
 System.out.println("Luas = " + luas);
 }
}

5. Konversi Jam Menit Detik

Algoritma

Judul : KonversiJam

Spesifikasi :

Deklarasi (kamus) :

detik : int

menit : int

jam : int

sisa : int

masukanAngka : int

Deskripsi :

write(”Program Konversi Angka ke Jam”)

read(“Masukkan angka = “, masukanAngka)

jam <– masukanAngka / 3600

sisa <– masukanAngka % 3600

menit <– sisa / 60

detik <– sisa % 60

write(jam,” Jam”,menit,” Menit”,detik,” Detik”)

Program

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package konversijam;

/**
 *
 * @author eko rudiawan
 */
import java.io.*;

public class KonversiJam {

/**
 * @param args the command line arguments
 */
 public static void main(String[] args) throws IOException {
 // TODO code application logic here
 BufferedReader dataInput = new BufferedReader(new InputStreamReader(System.in));
 String input;
 int detik;
 int menit;
 int jam;
 int masukanAngka = 0;
 int sisa;
 System.out.println("Program Konversi Angka ke Jam");
 System.out.print("Masukkan angka = ");
 try {
 input = dataInput.readLine();
 masukanAngka = Integer.parseInt(input);
 } catch (NumberFormatException error) {
 System.out.println("Input hanya berupa angka");
 }
 jam = masukanAngka / 3600;
 sisa = masukanAngka % 3600;
 menit = sisa / 60;
 detik = sisa % 60;
 System.out.println(jam + " Jam " + menit + " Menit " + detik + " Detik ");
 }
}

6. Mengurutkan Data Ascending dan Descending

Algoritma

Judul : Sorting Data

Spesifikasi :

Deklarasi (rumus) :

data[10] : int

ascending[10] : int

descending[10] : int

i : int

x : int

Deskripsi :

write(“Program Mengurutkan Bilangan Bulat”)

write(“Masukkan bilangan sebanyak 10 kali”)

for i=0 to 10

read(“a[“,i,”]”)

write(“Data sebelum disorting = “)

for i = 0 to 10

write(data[i],” “)

for j=0 to 10

for k=0 to 10

if data[j] < data[k] then

x <– data[j]

data[k] <– x

write(“Data Ascending = “)

for i=0 to 10

write(data[i],” “)

for j=0 to 10

for k=0 to 10

if data[j] > data[k] then

x <– data[j]

data[k] <– x

write(“Data Descending = “)

for i = 0 to 10

write(data[i],” “)

Program

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package sortingdata;

/**
 *
 * @author eko rudiawan
 */
import java.io.*;

public class SortingData {

/**
 * @param args the command line arguments
 */
 public static void main(String[] args) throws IOException {
 // TODO code application logic here
 int[] data = new int[10];
 int[] ascending = new int[10];
 int[] descending = new int[10];
 int i;
 int x; // menyimpan variabel sementara
 String input;
 BufferedReader dataInput = new BufferedReader(new InputStreamReader(System.in));
 System.out.println("Program Mengurutkan Bilangan Bulat");
 System.out.println("Masukkan bilangan sebanyak 10 kali");
 for (i = 0; i < 10; i++) {
 System.out.print("a[" + i + "] = ");
 try {
 input = dataInput.readLine();
 data[i] = Integer.parseInt(input);
 } catch (NumberFormatException error) {
 System.out.println("Input harus bilangan bulat");
 }
 }
 System.out.print("Data Sebelum Disorting = ");
 for (i = 0; i < 10; i++) {
 System.out.print(data[i] + " ");
 }
 System.out.println("");

for (int j = 0; j < 10; j++) {
 for (int k = 0; k < 10; k++) {
 if (data[j] < data[k]) {
 x = data[j];
 data[j] = data[k];
 data[k] = x;
 }
 }
 }
 System.out.print("Data Ascending = ");
 for (i = 0; i < 10; i++) {
 System.out.print(data[i] + " ");
 }
 System.out.println("");
 for (int j = 0; j < 10; j++) {
 for (int k = 0; k < 10; k++) {
 if (data[j] > data[k]) {
 x = data[j];
 data[j] = data[k];
 data[k] = x;
 }
 }
 }
 System.out.print("Data Descending = ");
 for (i = 0; i < 10; i++) {
 System.out.print(data[i] + " ");
 }
 System.out.println("");
 }
}

7. Perkalian Matrix

Algoritma

Judul : Perkalian Matrix

Spesifikasi :

Deklarasi :

      barisMatrix1 : int

      kolomMtrix1 : int

      barisMatrix2 : int

      kolomMatrix2 : int

      matrix1[][] : int

      matrix2[][] : int

      matrixHasil[][] : int

      barisHasil : int

      kolomHasil : int

      data : int

      input : String

      write(“Program Perkalian Matrix”)

      read(“Masukkan jumlah baris matrix 1 = “, barisMatrix1)

      read(“Masukkan jumlah kolom matrix 1 = “, kolomMatrix1)

      read(“Masukkan jumlah baris matrix 2 = “, barisMatrix2)

      read(“Masukkan jumlah kolom matrix 2 = “, kolomMatrix2)

      if barisMatrix1 != kolomMatrix2

                  write(“Perkalian tidak dapat dilakukan”)

      else

                  read(“Masukkan data matrix 1 = “, matrix1)

                  read(“Masukkan data matrix 2 = “, matrix2)

                  write(“Matrix 1 = “, matrix1)

                  write(“Matrix 2 = “, matrix2)

                  barisHasil <– barisMatrix1

                  kolomHasil <– kolomMatrix2

                  for I = 0 to barisHasil

                              for j=0 to kolomHasil

                              for k=0 to barisMatrix2

                              matrixHasil[i][j] <– matrixHasil[i][j] + matrix1[i][k] * matrix2[k][j]

                  write(“Hasil = “,matrixHasil)

Program

</pre>
/*

* To change this template, choose Tools | Templates

* and open the template in the editor.

*/

package perkalianmatriks;

&nbsp;

/**

*

* @author eko rudiawan

*/

import java.io.*;

&nbsp;

public class PerkalianMatriks {

&nbsp;

/**

* @param args the command line arguments

*/

public static void main(String[] args) throws IOException {

// TODO code application logic here

BufferedReader dataInput = new BufferedReader(new InputStreamReader(System.in));

int barisMatrix1 = 0;

int kolomMatrix1 = 0;

int barisMatrix2 = 0;

int kolomMatrix2 = 0;

int matrix1[][] = new int[10][10];

int matrix2[][] = new int[10][10];

int matrixHasil[][] = new int[20][20];

int barisHasil;

int kolomHasil;

int data;

String input;

System.out.println("Program Perkalian Matriks");

System.out.print("Masukkan jumlah baris matrix 1 = ");

try {

input = dataInput.readLine();

barisMatrix1 = Integer.parseInt(input);

} catch (NumberFormatException error) {

System.out.println("Input hanya berupa bilangan bulat !");

}

System.out.print("Masukkan jumlah kolom matrix 1 = ");

try {

input = dataInput.readLine();

kolomMatrix1 = Integer.parseInt(input);

} catch (NumberFormatException error) {

System.out.println("Input hanya berupa bilangan bulat !");

}

System.out.print("Masukkan jumlah baris matrix 2 = ");

try {

input = dataInput.readLine();

barisMatrix2 = Integer.parseInt(input);

} catch (NumberFormatException error) {

System.out.println("Input hanya berupa bilangan bulat !");

}

System.out.print("Masukkan jumlah kolom matrix 2 = ");

try {

input = dataInput.readLine();

kolomMatrix2 = Integer.parseInt(input);

} catch (NumberFormatException error) {

System.out.println("Input hanya berupa bilangan bulat !");

}

if (barisMatrix1 != kolomMatrix2) {

System.out.println("Perkalian tidak bisa dilakukan, Baris Matrix1 tidak sama dengan Kolom Matrix2");

} else {

System.out.println("Masukkan data matriks 1");

for (int i = 0; i < barisMatrix1; i++) {

for (int j = 0; j < kolomMatrix1; j++) {

System.out.print("A[" + i + "][" + j + "] = ");

try {

input = dataInput.readLine();

data = Integer.valueOf(input);

matrix1[i][j] = data;

} catch (NumberFormatException error) {

System.out.println("Input hanya bilangan bulat");

}

}

}

System.out.println("Masukkan data matriks 2");

for (int i = 0; i < barisMatrix2; i++) {

for (int j = 0; j < kolomMatrix2; j++) {

System.out.print("B[" + i + "][" + j + "] = ");

try {

input = dataInput.readLine();

matrix2[i][j] = Integer.parseInt(input);

} catch (NumberFormatException error) {

System.out.println("Input hanya bilangan bulat");

}

}

}

System.out.println("Matrix 1 = ");

for (int i = 0; i < barisMatrix1; i++) {

for (int j = 0; j < kolomMatrix1; j++) {

System.out.print(matrix1[i][j] + "\t");

}

System.out.println("");

}

System.out.println("Matrix 2 = ");

for (int i = 0; i < barisMatrix2; i++) {

for (int j = 0; j < kolomMatrix2; j++) {

System.out.print(matrix2[i][j] + "\t");

}

System.out.println("");

}

int h;

int l;

barisHasil = barisMatrix1;

kolomHasil = kolomMatrix2;

for (int i = 0; i < barisHasil; i++) {

for (int j = 0; j < kolomHasil; j++) {

matrixHasil[i][j] = 0;

for (int k = 0; k < barisMatrix2; k++) {

matrixHasil[i][j] += matrix1[i][k] * matrix2[k][j];

}

}

System.out.println("");

}

System.out.println("Hasil = ");

for (int i = 0; i < barisHasil; i++) {

for (int j = 0; j < kolomHasil; j++) {

System.out.print(matrixHasil[i][j] + "\t");

}

System.out.println("");

}

}

}

}

8. Searching Data

Algoritma

Judul : Searching Data

Spesifikasi :

Deklarasi :

namaBuah[] : string

input : string

i : int

ketemu : boolean

Deskripsi :

ketemu <– false

write(“Program Searching Data”)

write(“Data Berupa Nama Buah”)

read(“Masukkan Nama Buah”, input)

for i = 0 to namaBuah.length

if namaBuah[i] = input then

write(“Nama buah ketemu pada indeks ke “, i)

ketemu <– true

break

if ketemu = false then

write(“Nama buah tidak ditemukan”)

Program

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package searchingdata;

/**
 *
 * @author eko rudiawan
 */
import java.io.*;

public class SearchingData {

/**
 * @param args the command line arguments
 */
 public static void main(String[] args) throws IOException {
 // TODO code application logic here
 String namaBuah[] = {"pisang", "pepaya", "jeruk", "mangga", "nanas", "apel", "semangka"};
 String input = null;
 int i;
 boolean ketemu = false;
 System.out.println("Program Serching Data");
 System.out.println("Data Berupa Nama Buah");
 BufferedReader dataInput = new BufferedReader(new InputStreamReader(System.in));
 System.out.print("Masukkan nama buah = ");
 try {
 input = dataInput.readLine();

} catch (NumberFormatException error) {
 System.out.println("Input hanya berupa string !!");
 }

for (i = 0; i < namaBuah.length; i++) {
 if (namaBuah[i].equals(input)) {
 System.out.println("Nama buah ketemu pada indeks ke " + i);
 ketemu = true;
 break;
 }
 }
 if (!ketemu) {
 System.out.println("Nama buah tidak ditemukan !!");
 }
 }
}

9. Pengecekan Bilangan Prima

Algoritma

Judul : Pengecekan Bilangan Prima

Spesifikasi :

Deklarasi :

input : String

bilangan : int

loop : boolean

bilPrima : boolean

Deskripsi :

loop <– true

while loop = true

read(“Masukkan bilangan bulat = “, bilangan)

if bilangan = 1 then

bilPrima = false

else

bilPrima = true

for i = 2 to bilangan

if bilangan mod i = 0 then

bilPrima = false

if bilPrima = true then

write(bilangan,”merupakan bilangan prima”)

else

write(bilangan,”bukan bilangan prima”)

read(“Apakah anda ingin mengulangi (ya/tidak) ? “, input)

if input.equals(“ya”) then

loop = true

else if input.equals(“tidak”) then

loop = false

else

write(“Jawaban hanya ya dan tidak”)

Program

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package pengecekanbilanganprima;

/**
 *
 * @author eko rudiawan
 */
import java.io.*;

public class PengecekanBilanganPrima {

/**
 * @param args the command line arguments
 */
 public static void main(String[] args) throws IOException {
 // TODO code application logic here
 BufferedReader dataInput = new BufferedReader(new InputStreamReader(System.in));
 String input;
 int bilangan = 0;
 boolean loop = true;
 boolean bilPrima;
 while (loop) {
 System.out.print("Masukkan bilangan bulat = ");
 try {
 input = dataInput.readLine();
 bilangan = Integer.parseInt(input);
 } catch (NumberFormatException error) {
 System.out.println("Input hanya bilangan bulat");
 }
 if (bilangan == 1) {
 bilPrima = false;
 } else {
 bilPrima = true;
 for (int i = 2; i < bilangan; i++) {
 if (bilangan % i == 0) {
 bilPrima = false;
 }
 }
 }
 if (bilPrima) {
 System.out.println(bilangan + " Merupakan Bilangan Prima");
 } else {
 System.out.println(bilangan + " Bukan Merupakan Bilangan Prima");
 }
 System.out.println("Apakah Anda Ingin Mengulangi (ya/tidak) ? ");
 input = dataInput.readLine();
 if (input.equals("ya")) {
 loop = true;
 } else if (input.equals("tidak")) {
 loop = false;
 } else {
 System.out.println("Jawaban hanya ya/tidak");
 }
 }
 }
}

10. Parse String

Algoritma

Judul : Memisah String

Spesifikasi :

Deskripsi :

data : String

hasil = string

i : int

Deskripsi :

data <– “1|GA|18:55|10:00|Gate6|delay”

hasil <– data.split(“[ | ]+”)

for int i=0 to hasil.length

write(hasil[i])

Program

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package memisahstring;

/**
 *
 * @author eko rudiawan
 */
//import java.io.*;
public class MemisahString {

/**
 * @param args the command line arguments
 */
 public static void main(String[] args) {
 // TODO code application logic here
 // "1|GA|18:55|10:00|Gate6|delay"
 String data = "1 | GA | 18:55 | 10:00 | Gate6 | delay";
 String[] hasil = data.split("[ | ]+");
 for(int i=0; i<hasil.length; i++){
 System.out.println(hasil[i]);
 }
 }
}
Advertisements