Generate a fibonacci

    0

    0

    Giovanny Gongora

    Codiga's C Recipes

    Generate a Fibonacci sequence of first n numbers (entered by the user)

    #include <stdio.h>
    
    int fibonacci() {
      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;
    }
    
    Codiga Logo
    Codiga Hub
    • Rulesets
    • Explore
    • Cookbooks
    • Playground
    soc-2 icon

    We are SOC-2 Compliance Certified

    G2 high performer medal

    Codiga – All rights reserved 2022.