Thread: Need help with fibonacci sequence in c

  1. #1
    Registered User
    Join Date
    Apr 2017
    Posts
    17

    Need help with fibonacci sequence in c

    Below is my code. My problem is that when i run it, it asks me how many times i want to run, i input my number and hit enter.... and nothing else happens.

    Code:
    #include <stdio.h>
    
    
    int main() {
    
    
    int num1 = 0;
    int num2 = 1;
    
    
    int num_t = 0;
    printf("Enter the number of times you want the Fibonacci sequence to be run: \n");
    scanf("%d", &num_t);
    
    
    
    
    int num_r = 0;
    
    
    while(num_r <= num_t); {
        printf("Here is the next number: %d ", num1);
        printf("Here is the next number: %d ", num2);
        num1 = num1 + num2;
        num2 = num1 + num2;
        num_r += 1;
    
    
    
    
    
    
    }
    return 0;
    
    
    }

  2. #2
    Registered User
    Join Date
    Apr 2017
    Posts
    17
    please PM me if you've replied with a possible fix

  3. #3
    Programming Wraith GReaper's Avatar
    Join Date
    Apr 2009
    Location
    Greece
    Posts
    2,739
    On line #21, that semicolon says to the compiler that your while statement ends there, without any instructions. That results in an infinite loop if you input a number >= 0. Yes, C is weird like that sometimes. Remove it.
    Devoted my life to programming...

  4. #4
    Registered User
    Join Date
    Apr 2017
    Posts
    17
    thanks so much!

  5. #5
    Registered User
    Join Date
    Apr 2017
    Posts
    17
    IT WORKED Thanks so much, I have been cracking my head open for weeks with this lol thanks so much

  6. #6
    Registered User
    Join Date
    Apr 2013
    Posts
    1,658
    Here is an alternate version. For 32 bit signed integer, max input is 46.

    Code:
    #include <stdio.h>
    
    int main() {
    int num1 =  1;    /* fib(-1) */
    int num2 = -1;    /* fib(-2) */
    int num_r;
    int num_t;
    int temp;
        printf("Enter the number of times you want the Fibonacci sequence to be run: \n");
        scanf("%d", &num_t);
        for(num_r = 0; num_r <= num_t; num_r += 1){
            temp = num1;
            num1 = num1 + num2;
            num2 = temp;
            printf("%d\n", num1);
        }
        return 0;
    }
    or

    Code:
    #include <stdio.h>
    
    int main() {
    int num1 =  1;    /* fib(-1) */
    int num2 = -1;    /* fib(-2) */
    int num_r;
    int num_t;
        printf("Enter the number of times you want the Fibonacci sequence to be run: \n");
        scanf("%d", &num_t);
        for(num_r = 0; num_r <= num_t; num_r += 1){
            num1 = num1 + num2;
            num2 = num1 - num2;
            printf("%d\n", num1);
        }
        return 0;
    }
    Last edited by rcgldr; 04-03-2017 at 03:48 PM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. 5 - Fibonacci Sequence
    By cmp in forum C Programming
    Replies: 7
    Last Post: 03-08-2014, 06:18 PM
  2. fibonacci sequence
    By cph in forum C Programming
    Replies: 57
    Last Post: 04-30-2009, 07:17 AM
  3. Fibonacci Sequence
    By Dogmasur in forum C Programming
    Replies: 15
    Last Post: 08-10-2008, 07:55 AM
  4. Fibonacci sequence
    By MuffanMan123 in forum C++ Programming
    Replies: 6
    Last Post: 02-26-2008, 09:15 AM
  5. Fibonacci Sequence
    By Unregistered in forum C++ Programming
    Replies: 6
    Last Post: 09-08-2001, 11:29 AM

Tags for this Thread