contoh soal algoritma pemrograman java

1:26 AM

LIST SOAL ALPRO JAVA

lagi bingung mau ngapain jadi nulis ini aja

1. Nampilin text hello world

public class text {
public static void main (String[] args){
System.out.print("hello world");
}
}



2. program buat kotak pakai symbol
...
public static void main(String[] args){
for(int i=0; i<5; i++){
for(int j=0; j<5; j++){
System.out.print("X");
}
System.out.println("");
}
...

hasil
XXXXX
XXXXX
XXXXX
XXXXX
XXXXX

penjelasan : for kedua(j) digunain buat nyetak huruf X dan for pertama(i) dipake buat "enter" biar ganti baris,



3.program buat segitiga
...
 for(int i=0;i<5;i++){
            for(int j=0;j<=i;j++){
                System.out.print("x");
            }
            System.out.println("");
        }
...

hasil :
x
xx
xxx
xxxx
xxxxx




4. Program kalkulator sederhana
...
 public static void main(String[] args){
        double a,b;
        System.out.println("masukkan angka pertama");
        a=input();
        System.out.println("masukkan angka kedua");
        b=input();
        double c=a+b;
        double d=a-b;
        double e=a*b;
        double f;
        f=a/b;
        System.out.println("a+b = "+c);
        System.out.println("a-b = "+d);
        System.out.println("a*b = "+e);
        System.out.println("a:b = "+f);
    }
    public static int input(){
        Scanner a=new Scanner(System.in);
        int b=a.nextInt();
        return b;
        
    }
...

hasil:
run:
masukkan angka pertama
2
masukkan angka kedua
4
a+b = 6.0
a-b = -2.0
a*b = 8.0
a:b = 0.5

You Might Also Like

2 comments

  1. mantap mas.. jreatif.. kalo dipisah2 bisa jadi banyak postingan lloo

    ReplyDelete
  2. makasih gan, lagi iseng aja. .

    ReplyDelete