Write a C program that prints the numbers from 1 to 100, but substitutes the word "fizz" if the number is evenly divisble by 3, and "buzz" if the number is divisible by 5, and if divisible by both prints "fizz buzz" like this: 13 1 14 2 fizz 4 buzz fizz 7 fizz buzz 16 17 fizz 8 fizz buzz 19 buzz ... 11 fizz and so on

Answer :

Answer:

// here is code in C.

#include <stdio.h>

int main(void) {

int num;

 // loop run 100 times

for(num=1;num<=100;num++)

{

 //check divisibility by 3 and 5

    if(num%15==0)

    {

    printf("fizz buzz ");

    }

     // check divisibility by 5 only

    else if(num%5==0)

    {

        printf("buzz ");

    }

     // check divisibility by 3 only

    else if(num%3==0)

    {

        printf("fizz ");

    }

    else{

        printf("%d ",num);

    }

}

return 0;

}

Explanation:

Run a for loop from 1 to 100. Check the divisibility of number by 15, if it is divisible print "fizz buzz", if it is not then Check the divisibility by 5 only.if it returns true then print "buzz". if not then check for 3 only,if returns true, print "fizz". else print the number only.

Output:

1 2 fizz 4 buzz fizz ........buzz fizz 97 98 fizz buzz

Other Questions