create a C program to print Fibonacci series upto a specified number N using while loop.
Question
Create a C program to print Fibonacci series up to a specified number N using while loop.
Solution
Sure, here is a simple C program that prints the Fibonacci series up to a specified number N using a while loop:
#include <stdio.h>
int main() {
int N, first = 0, second = 1, next;
printf("Enter the number of terms\n");
scanf("%d", &N);
printf("First %d terms of Fibonacci series are:\n", N);
int i = 0;
while (i < N) {
if (i <= 1)
next = i;
else {
next = first + second;
first = second;
second = next;
}
printf("%d\n", next);
i++;
}
return 0;
}
This program first initializes the first two terms of the Fibonacci series, 0 and 1. It then enters a while loop, which continues until it has printed N terms. In each iteration of the loop, it calculates the next term by adding the previous two terms, and then updates the two previous terms to be the second and the new term, respectively. It then prints the new term.
Similar Questions
Write a C program to print the Fibonacci series using recursion and without using recursion.
Generate the Fibonacci sequence up to the 10th term and calculate the value of the 10th term.
write a program to print integer no, from 1 to n where n is the input from the user using do while loop in c language use getch
Plot the Fibonacci numbers from n = 1 to 14, together with F(n) = n^2 use clion by c codes
What will be the output of the given Python code?n=7c=0while(n): if(n > 5): c=c+n-1 n=n-1 else: breakprint(n)print(c)
Upgrade your grade with Knowee
Get personalized homework help. Review tough concepts in more detail, or go deeper into your topic by exploring other relevant questions.