Rabu, 11 Mei 2011

Faktorial

import java.util.Scanner;
public class faktorial
{
    public static void main(String[] args)
    {
     int f;
     int n;
     Scanner input=new Scanner(System.in);
     System.out.print("masukkan nilai f: ");
     f=input.nextInt();
     System.out.print("masukkan nilai n: ");
     n=input.nextInt();

    
     for (int i=1 ; i<=n ; i++) f*=i;
     System.out.println(n+"! ="+f);
 }

}

for

public class tugas1
{
    public static void main(String[] args)
    {


        for(int i=2; i<=10; i+=2)
            System.out.println(i+","+"");


}
}

While

public class while2 {
     public static void main(String[] args)
     {
         int i=3;
         while (i<=18)
         {
             System.out.println(i+","+""); i+=3;

    }
}
}

While

public class while2 {
     public static void main(String[] args)
     {
         int i=3;
         while (i<=18)
         {
             System.out.println(i+","+""); i+=3;

    }
}
}

array 2 dimensi kamus

import java.util.Scanner;
public class array_2_dimensi
{public static void main (String[] args)
{ Scanner input=new Scanner(System.in);

  String [][] kata={
{"one","satu"},{"cow","sapi"},{"run","lari"},{"red","merah"},{"man","orang"}

  };
  String katadicari;

  System.out.print("Kata Yang Di Cari = ");
  katadicari=input.nextLine();
  int i,k;k=-1;
  for (i=0;i<5;i++)
          {
      if(kata[i][0].equals(katadicari))
      {
          k=i;
      }
    }
  if (k==-1)
  {System.out.println ("kata yang di cari tidak ditemukan");
    }
 else
  {
      System.out.println("terjemahan = "+kata [k][1]);
 }
    }
}

array 1 dimensi

public class array
{
    public static void main(String [] ar)

    {int [] um={4,5,2,3};
     String [] nm={"joko","Budi","Yeni","Nino"};
     System.out.println("No Nama Umur");
     for(int i=0 ; i<4;i++)
         System.out.println((i+1)+"  "+nm[i]+"  "+um[i]);
    }
}