NPM : 22110690
Kelas : 2KB 01
Buat baris dan deret :
1. Output = 1,3,5,7,9,11,13,.... (bilangan ganjil)
2. Output = 1,1,2,3,5,8,13,......
Jawaban:
1. Coding:
public class DeretGanjil
{
public static void main (String[]args)
{
int x,y;
y = 15;
for(x=1;x<y;x++)
{
if(x==1)
System.out.print(x);
else
System.out.print(","+x);
x=x+1;
}
}
}
Output: 1,3,5,7,9,11,13
2. Coding:
public class DeretFibo
{
public static void main(String[]args)
{
int x=1,y=1;
int z = 8; // input deret fibonacci
System.out.print(x+" ");
for (int n=1;n<z;n++)
{
System.out.print(x+", ");
x=x+y;
y=x-y;
}
System.out.print("...");
}
}
Output = 1 1, 2, 3, 5, 8, 13, 21, ...