3. Fibonacci sequence of first N numbers

3. Fibonacci Sequence of first N numbers

Aim:

To write a C program is to generate the Fibonacci sequence up to a specified number of terms (n) entered by the user.

 

Program :

// Fibonacci Series up to n terms

#include <stdio.h>
void 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 \t %d \t ", t1, t2);

  // print 3rd to nth terms
  for (i = 3; i <= n; ++i) {
    printf("%d \t ", nextTerm);
    t1 = t2;
    t2 = nextTerm;
    nextTerm = t1 + t2;
  }
}


No comments:

Post a Comment

P24CA3P5 - Core Practical V: SMART DEVICES PROGRAMMING LAB

  Core Practical V: SMART DEVICES PROGRAMMING LAB SEMESTER: III                                 CODE: P24CA3P5 CREDITS: 2                  ...