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

C Program to Display Fibonacci Sequence

The document provides an overview of the Fibonacci sequence, including its definition and a C programming example to display the sequence. It includes code snippets for generating Fibonacci numbers using both for and while loops, along with explanations of how the loops function. Additionally, it encourages readers to explore more C programming tutorials and examples on the website.

Uploaded by

Anirban Sarkar
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
18 views

C Program to Display Fibonacci Sequence

The document provides an overview of the Fibonacci sequence, including its definition and a C programming example to display the sequence. It includes code snippets for generating Fibonacci numbers using both for and while loops, along with explanations of how the loops function. Additionally, it encourages readers to explore more C programming tutorials and examples on the website.

Uploaded by

Anirban Sarkar
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 6

Thank you for printing our content at www.domain-name.com. Please check back soon for new contents.

Try hands-on C Programming (https://programiz.pro/learn/master-c-programming?


36%
off
with Programiz PRO! utm_source=sticky-
Claim Discount Now banner&utm_campaign=programiz&utm_medium=referral)
(https://programiz.pro/learn/master-c-programming?
Programiz
PRO
utm_source=nav-
Search tutorials & examples
(/)
floating&utm_campaign=programiz&utm_medium=referral)
www.domain-name.com

C Program to Display
Fibonacci Sequence
To understand this example, you should have the
knowledge of the following C programming (/c-
programming) topics:

C Programming Operators (/c-programming/c-


operators)

C while and do...while Loop (/c-programming/c-do-


while-loops)

C for Loop (/c-programming/c-for-loop)

C break and continue (/c-programming/c-break-


continue-statement)

The Fibonacci sequence is a sequence where the next


term is the sum of the previous two terms. The first two
terms of the Fibonacci sequence are 0 followed by 1.

The Fibonacci sequence: 0, 1, 1, 2, 3, 5, 8, 13, 21


Visitour
Thank you for printing thiscontent
pageattowww.domain-name.com.
learn about the Fibonacci
Please sequence
check back soon for new contents.
(https://www.mathsisfun.com/numbers/fibonacci-
Try hands-on C Programming (https://programiz.pro/learn/master-c-programming?
36%
off
sequence.html).
with Programiz PRO! utm_source=sticky-
Claim Discount Now banner&utm_campaign=programiz&utm_medium=referral)
(https://programiz.pro/learn/master-c-programming?
Programiz
PRO
utm_source=nav-
Search tutorials & examples
(/)
floating&utm_campaign=programiz&utm_medium=referral)
www.domain-name.com
Fibonacci Series up to n terms

#include <stdio.h>
int main() {

int i, n;

// initialize first and second terms


int t1 = 0, t2 = 1;

// initialize the next term (3rd term)


int nextTerm = t1 + t2;

// get no. of terms from user


printf("Enter the number of terms: ");
scanf("%d", &n);

// print the first two terms t1 and t2


printf("Fibonacci Series: %d, %d, ", t1, t2);

// print 3rd to nth terms


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

return 0;
}

Run Code (/c-programming/online-compiler)

Output

Enter the number of terms: 10


Fibonacci Series: 0, 1, 1, 2, 3, 5, 8, 13, 21, 34,
Let us
Thank you for printing oursuppose = 10 . First, we havePlease
content at nwww.domain-name.com. printed the back
check first soon
two for new contents.
terms of the Fibonacci sequence before using a for loop
Try hands-on C Programming (https://programiz.pro/learn/master-c-programming?
36%
off
to print
with Programiz the next n
PRO! terms.
utm_source=sticky-
Claim Discount Now banner&utm_campaign=programiz&utm_medium=referral)
(https://programiz.pro/learn/master-c-programming?
Let us see how the for loop works:
Programiz
PRO
utm_source=nav-
Search tutorials & examples
(/)
floating&utm_campaign=programiz&utm_medium=referral)
i t1 www.domain-name.com
t2 nextTerm

3 0 1 1

4 1 1 2

5 1 2 3

6 2 3 5

7 3 5 8

8 5 8 13

9 8 13 21

10 13 21 34
Fibonacci
Thank you for printing our content Sequence Up to aPlease
at www.domain-name.com. Certain
check back soon for new contents.

36%
Number
Try hands-on C Programming (https://programiz.pro/learn/master-c-programming?
off
with Programiz PRO! utm_source=sticky-
Claim Discount Now banner&utm_campaign=programiz&utm_medium=referral)
(https://programiz.pro/learn/master-c-programming?
Programiz
#include <stdio.h>
PRO
utm_source=nav-
Search tutorials & examples
(/)
intfloating&utm_campaign=programiz&utm_medium=referral)
main() {
int t1 = 0, t2 www.domain-name.com
= 1, nextTerm = 0, n;
printf("Enter a positive number: ");
scanf("%d", &n);

// displays the first two terms which is always 0 and 1


printf("Fibonacci Series: %d, %d, ", t1, t2);
nextTerm = t1 + t2;

while (nextTerm <= n) {


printf("%d, ", nextTerm);
t1 = t2;
t2 = nextTerm;
nextTerm = t1 + t2;
}

return 0;
}

Run Code (/c-programming/online-compiler)

Output

Enter a positive integer: 100


Fibonacci Series: 0, 1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89

In this program, we have used a while loop to print all


the Fibonacci numbers up to n .

If n is not part of the Fibonacci sequence, we print the


sequence up to the number that is closest to (and lesser
than) n .

Suppose n = 100 . First, we print the first two terms


t1 = 0 and t2 = 1 .
Thenour
Thank you for printing the
content
while loop prints the rest ofPlease
at www.domain-name.com. the sequence using
check back soon for new contents.
the nextTerm variable:
Try hands-on C Programming (https://programiz.pro/learn/master-c-programming?
36%
off
with Programiz PRO! utm_source=sticky-
Claim Discount
t1 Now t2 banner&utm_campaign=programiz&utm_medium=referral)
nextTerm nextTerm <= n
(https://programiz.pro/learn/master-c-programming?
Programiz
PRO
utm_source=nav-
Search tutorials & examples
(/) true . Print nextTerm
floating&utm_campaign=programiz&utm_medium=referral)
0 1 1
www.domain-name.com
.

true . Print nextTerm


1 1 2
.

true . Print nextTerm


1 2 3
.

... ... ... ...

true . Print nextTerm


34 55 89
.

false . Terminate
55 89 144
Loop.

Share on:

(https://www.facebook.com/sharer/sharer.php? (https://twitter.com/int
u=https://www.programiz.com/c- text=Check%20this%2
programming/examples/fibonacci-series) programming/example

Did you find this article helpful?


Thank you for printing our content at www.domain-name.com. Please check back soon for new contents.

Try hands-on C Programming (https://programiz.pro/learn/master-c-programming?


36%
off
with Programiz PRO! utm_source=sticky-
Claim Discount Now banner&utm_campaign=programiz&utm_medium=referral)
(https://programiz.pro/learn/master-c-programming?
Programiz
PRO
utm_source=nav-
Search tutorials & examples
(/)
floating&utm_campaign=programiz&utm_medium=referral)
Related Examples www.domain-name.com

C Example

Print an Integer (Entered by the User)

(/c-programming/examples/print-integer)

C Example

Print Pyramids and Patterns

(/c-programming/examples/pyramid-pattern)

C Example

Display Factors of a Number

(/c-programming/examples/factors-number)

C Example

Check Whether a Number is Positive or Negative

(/c-programming/examples/negative-positive-zero)

You might also like