TUGAS AP3 ( JAVA )
TUGAS
1. Tugas Deret Ganjil ( Outputnya : 1 3 5 7 9 11 13 15 17 19 21 23 25 )
2. Deret fibonanci ( Outpunya : 0 1 1 2 3 5 8 13 21 34 )
Jawab :
public static void main(String[] args) {
int n;
for(n=1;n<26;n+=2)
System.out.print(" "+ n);
}
}
Output : ( 1 3 5 7 9 11 13 15 17 19 21 23 25 )
public class Fabonanci26 {
public static void main(String[] args) {
int a=0,b=1;
int n = 10; // input deret fibonacci
for (int i=1;i<=n;i++){
System.out.print(a+" ");
a=a+b;
b=a-b;
}
}
}
2. Deret fibonanci ( Outpunya : 0 1 1 2 3 5 8 13 21 34 )
Jawab :
1. Deret Ganjil
public class DeretGanjil26 {
public static void main(String[] args) {
int n;
for(n=1;n<26;n+=2)
System.out.print(" "+ n);
}
}
Output : ( 1 3 5 7 9 11 13 15 17 19 21 23 25 )
2. Deret Fabonanci
public class Fabonanci26 {
public static void main(String[] args) {
int a=0,b=1;
int n = 10; // input deret fibonacci
for (int i=1;i<=n;i++){
System.out.print(a+" ");
a=a+b;
b=a-b;
}
}
}
( Outpunya : 0 1 1 2 3 5 8 13 21 34 )
Komentar