Sabtu, 01 Oktober 2016

Contoh-contoh koding Pemrograman Java Bagian-1

//Anak Ayam
  for (int i = 100; i > 0; i--) {
         System.out.println(" Anak Ayam "+i+" Mati 1 Tinggal "+(i-1));
        }}}


//Faktorial
   int batas = 5;
        int counter = 1;
        int faktorial = 1;

        for(counter=1; counter<=batas; counter++)    {
            faktorial = 1;
            for(int faktor=2; faktor<=counter; faktor++)    {
                faktorial*= faktor;}
            System.out.println(counter + "!" + " = " + faktorial);
        }}}


//54321
 for (int i=5;i>=1;i--) {   
            for (int j=i;j>=1;j--){
            System.out.print(j);}
        System.out.println("");}
    System.out.println("");
    }}


//Hasil:

54321
4321
321
21
1


//12345

            for (int i = 1; i <= 5; i++) {
            for (int j = 1;  j <= 5; j++) {
        System.out.print(i*j);}
        System.out.println("");}
    System.out.println("");
    }}


//Hasil:

12345
246810
3691215
48121620
510152025


//1-54321

for (int i = 1; i <=5; i++) {  
            for (int j = i; j >=1; j--) {
        System.out.print(j);}
        System.out.println("");}
    System.out.println("");
    }}

//Hasil:

1
21
321
4321
54321


//Menghitung genap atau ganjil
int a=4;
if (a%2 ==0)
            System.out.println(a +"genap");
else
            System.out.println(a +"ganjil");
    }}

hasil:
4genap


//perubahan (versi for)
 for (int a=1;a<=4;a++)
            System.out.println(a);}}
//perubahan(versi do while)
 int a=1;
 do {
     System.out.println(a);
     a++;
 }while (a<=4);
        System.out.println("");}}
//perubahan(versi while)
 int a=1;
 while (a<=4)
 {System.out.println(a);
  a++;}
     System.out.println("");}}



0 komentar:

Posting Komentar

 
Copyright © 2012. Era Terakhir - Posts · Comments
Post by ERA93 · Powered by Blogger