Minggu, 16 Oktober 2011

A&P 3

1. Bilangan Deret Ganjil

public class coba1
{
public static void main(String[] args)
 {
   for (int c=1;c<=20;c++)
   {
     if(c==1)
     {
       System.out.print(c);
     }
    else if (c%2==1) {
    System.out.print(", "+c);
    }
   }
  }
}

OutPut : 1, 3, 5, 7, 9, 11, 13, 15, 17, 19

2. Bilangan Class Fibonacci

public class coba2
{

public static void main(String[] args)
 {
   int x=0,y=1;
   int n = 10;
   for (int i=1;i<=n;i++)
    {
       System.out.print(x+" ");
       x=x+y;
       y=x-y;
    }
  }
}

OutPut : 0 1 1 2 3 5 8 13 21 34