Problem 19

author
By Supreme2023-03-15
Problem Statement :
Write a program to generate first n number of Fibonacci series.
Code:

#include <stdio.h>

int main(){

//19. Write a program to generate first n number of Fibonacci series

int t1=0,t2=1,next=t1+t2,n,i;

printf("\tGenerate First N Number Of Fibonacci Series");
printf("\n==========================================================");

printf("\n\nEnter The Value Of N : ");
scanf("%d", &n);

printf("\n__________________________________________________________");

printf("\n\nFibonaci Series : %d %d",t1,t2);

for(i=3;i<=n;i++){
printf(" %d",next);
t1=t2;
t2=next;
next=t1+t2;
}

printf("\n__________________________________________________________\n\n");

return 0;
}

Description :
  • This is a C program that generates the first n numbers of the Fibonacci series. The program prompts the user to input a number 'n', and then uses a for loop to generate the Fibonacci series. The variables t1, t2, and next are initialized to 0, 1, and their sum (i.e., 1), respectively. The program then prints the first two numbers of the series (i.e., t1 and t2). In each iteration of the loop, the program calculates the next number in the series (i.e., next) as the sum of the previous two numbers (i.e., t1 and t2), and then updates the values of t1, t2, and next accordingly. The program then prints the next number in the series to the console. The loop continues until i is less than or equal to n. Finally, the program ends by returning 0 to indicate that it has executed successfully.
Output Image:
Output-image

© Copyright 2022. All right reserved, GTU Coder.