0% found this document useful (0 votes)
236 views

C Program Fibonacci Series

This document contains 4 examples of C program code to generate Fibonacci series using different approaches: 1. A while loop is used to iteratively calculate and print terms until the number reaches 100. 2. A for loop calculates and prints the first n terms where n is entered by the user. 3. Recursion is used by calling a fib function that calculates individual terms. 4. Variables are used to store the previous two terms and calculate the next term, printing up to the Nth number entered by the user.
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
236 views

C Program Fibonacci Series

This document contains 4 examples of C program code to generate Fibonacci series using different approaches: 1. A while loop is used to iteratively calculate and print terms until the number reaches 100. 2. A for loop calculates and prints the first n terms where n is entered by the user. 3. Recursion is used by calling a fib function that calculates individual terms. 4. Variables are used to store the previous two terms and calculate the next term, printing up to the Nth number entered by the user.
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 5

C Program: Fibonacci Series

#include <stdio.h>

int old_number;

int current_number;

int next_number;

int main()

old_number = 1;

current_number = 1;

clrscr();

printf("Fibonacci Series are : \n");

printf("1 ");

while (current_number < 100)

printf(" %d ", current_number);

next_number = current_number +
old_number;

old_number = current_number;

current_number = next_number;

getch();

return (0);

}
Fibonacci series in c using for loop

/* Fibonacci Series c language */

#include<stdio.h>

void main()

int n, first = 0, second = 1, next, c;

clrscr();

printf("Enter the Number of Terms : ");

scanf("%d",&n);

printf("First %d terms of Fibonacci Series are :


",n);

for (c = 0;c < n;c++)

if ( c <= 1 )

next = c;

else

next = first + second;

first = second;

second = next;

printf(" %d ",next);

getch();

}
Fibonacci Series Using Recursion

#include<stdio.h>

int fib(int m);

void main()

int i,n;

clrscr();

printf("\n Enter no: of Terms = ");

scanf("%d",& n);

for(i=1;i<=n;i++)

printf(" %d ",fib(i));

getch();

int fib(int m)

if(m==1||m==2)

return(1);

else
{

return(fib(m-1)+fib(m-2));

Fibonacci Series upto N number

#include<stdio.h>

void main()

int n,n1,n2,n3,i;

clrscr();

printf("Enter the Number : ");

scanf("%d",&n);

n1=0;

n2=1;

printf("%d %d ",n1,n2);

for(i=1;i<=n;i++)

n3=n1+n2;

printf(" %d ",n3);

n1=n2;

n2=n3;

}
getch();

You might also like