1.Coding Tugas deret angka ganjil(1,3,5,7,9,11,....)
public class DeretGanjil150 {// nama file nantinya akan menjadi DeretGanjil150.java
public static void main(String[] args) {
int n;
for(n=1;n<150;n+=2)
System.out.print(" "+ n);
}
}
output:(1,3,5,7,9,11,13,...,149)
----------------------------------------------------------
2.Coding Tugas deret fibonacci
public class DeretKedua {
public static void main(String[] args) {
int a=0,b=1;
int n = 16;//jumlah deret fibonacci yg ingin ditampilkan
for (int i=1;i<=n;i++){
System.out.print(a+" ");
a=a+b;
b=a-b;
}
}
}
output:( 0,1,1,2,3,5,8,13,21,34,55,89,144,233,377,610 )
Tidak ada komentar:
Posting Komentar