wap to print fibonnaci seriess
Answers
Answer:
include <stdio.h>
int main() {
int i, n, t1 = 0, t2 = 1, nextTerm;
printf("Enter the number of terms: ");
scanf("%d", &n);
printf("Fibonacci Series: ");
for (i = 1; i <= n; ++i) {
printf("%d, ", t1);
nextTerm = t1 + t2;
t1 = t2;
t2 = nextTerm;
}
return 0;
}
Explanation:
hope it helps you
Answer:
public class Fibonacci
{
public static void main(String[] args) {
int n = 10, t1 = 0, t2 = 1;
System.out.print("First " + n + " terms: ");
for (int i = 1; i <= n; ++i)
{
System.out.print(t1 + " + ");
int sum = t1 + t2;
t1 = t2;
t2 = sum;
}
}
}
Output
0 + 1 + 1 + 2 + 3 + 5 + 8 + 13 + 21 + 34 +
In the above program, first (t1) and second (t2) terms are initialized to the first two terms of the Fibonacci series 0 and 1 respectively.
Then, for loop iterates to n (number of terms) displaying the sum of the previous two terms stored in variable t1.
Explanation:
hope it helps a lot