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 upto 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.
create a C program that generates Fibonacci series upto a specified number N.
Create a program that generates and prints the Fibonacci series up to a specified number 'N'. The Fibonacci series is a sequence of numbers in which each number is the sum of the two preceding numbers, starting with 0 and 1. Your program should take an integer input 'N' and display the Fibonacci series up to the Nth term using a while loop.Fibonacci series: 0, 1, 1, 2, 3, 5, 8,... Note: This question is one of the most asked questions in placements.Input format :The input consists of a positive integer N.Output format :The output displays the Fibonacci series up to the Nth term separated by space.
Write a shell script to print the n terms in a Fibonacci Series
WAP a program to generate a list of elements of Fibonacci Series.
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.