1. Membuat program Java *Deret angka ganjil*
Public class DeretAngkaGanjil
public static void main(String[]args){
int i = -1;
int x = 0;
System.out.print("Deret Angka Ganjil= ");
while(i<=18)
{
x = i + 2;
i++;
i++;
System.out.print(" "+ x +", ");
}
System.out.println();
}
public static void main(String[]args){
int i = -1;
int x = 0;
System.out.print("Deret Angka Ganjil= ");
while(i<=18)
{
x = i + 2;
i++;
i++;
System.out.print(" "+ x +", ");
}
System.out.println();
}
Output = 1,3,5,7,9,11,13,15,17,19
2. Membuat program java *Deret angka fibonansi*
2. Membuat program java *Deret angka fibonansi*
public class DeretAngkaFibonansi
public static void main(String[] args) {
int c=1,d=1;
int n = 10;
System.out.print("Deret angka fibonanci:");
for (int i=1;i<n;i++){
System.out.print(c+" ");
c=c+d;
d=c-d;
}
System.out.println();
}
}
Output: 1 2 3 5 8 13 21 34 55
Tidak ada komentar:
Posting Komentar