Question: 0 1 1 2 3 5 8 13 21 34
*Used while loop
import java.util.Scanner;
public class Main
{
public static void main(String[] args) {
Scanner scan = new Scanner(System.in);
System.out.print("Please enter number of terms: ");
int t=scan.nextInt();
//0 1 1 2 3 5 8 13 21 34
int i=0, j=1, k=2;
while (k <= t) {
System.out.print(" "+i + " "+j);
i=i+j;
j=i+j;
k+=2;
}
}
}
public class Main
{
public static void main(String[] args) {
Scanner scan = new Scanner(System.in);
System.out.print("Please enter number of terms: ");
int t=scan.nextInt();
//0 1 1 2 3 5 8 13 21 34
int i=0, j=1, k=2;
while (k <= t) {
System.out.print(" "+i + " "+j);
i=i+j;
j=i+j;
k+=2;
}
}
}
====================
*Used For Loop
import java.util.Scanner;
public class Main
{
public static void main(String[] args) {
Scanner scan = new Scanner(System.in);
System.out.print("Please enter number of terms: ");
int t=scan.nextInt();
//0 1 1 2 3 5 8 13 21 34
int i=0, j=1, sum;
for (int m=1;m<=t; ++m) {
System.out.print(i + " ");
sum=i+j;
i=j;
j=sum;
}
}
}
একটি মন্তব্য পোস্ট করুন