sleeping fizzbuzz in C


#1

I have read the “C development on Linux” articles. I was then able to successfully able to create the FizzBuzz. Now how do I get it to sleep for a second each time it prints a line? Here is what I have so far:

#include <stdio.h>
 
int main() {
 
    int n = 1;
    char fz[] = "Fizz";
    char bz[] = "Buzz";
    char fb[] = "FizzBuzz";
 
    for ( n = 1; n < 101 ; ++n ) 
        if ( n % 15 == 0 ) 
            printf("%s
", fb);
        else if ( n % 5 == 0 ) 
            printf("%s
", fz);
        else if ( n % 3 == 0 ) 
            printf("%s
", bz);
        else 
            printf("%d
", n); 
 
    return 0;
 
}



#2

Hi,

you can try to include:

#include <time.h> 

For example this program will wait 1 second after each printf:

#include <stdio.h>
#include <time.h>
 
int main() {
 
    int n = 1;
    char fz[] = "Fizz";
    char bz[] = "Buzz";
    char fb[] = "FizzBuzz";
 
    for ( n = 1; n < 101 ; ++n ) 
        if ( n % 15 == 0 ) {
            printf("%s
", fb);
        sleep(1);
    } else if ( n % 5 == 0 ) {
            printf("%s
", fz);
        sleep(1);
    } else if ( n % 3 == 0 ) {
            printf("%s
", bz);
        sleep(1);
    } else {
            printf("%d
", n);
        sleep(1);
    }
    return 0;
 
}

Is this what you need?


#3

Thanks that is just what I needed. Being that I am just beginning to learn C, I did not know about <time.h>.


#4