Feb 7, 2022

foobar problem-Make some Fun ! (C language)

Read the problem below and create your own problem like this.

 /*FooBar Problem: print numbers from 1 to n (user inp), but
print 'foo' if the number is divisible by 3,
 'bar' if the number is divisible by 5, and
 if divisible by both print 'foobar', instead of number
Eg:
    1
    n=5
    2
    foo
    4
    bar */
#include <stdio.h>

void fooBar(int *num)
{
    for (int i = 1; i <= *num; i++)
    {
        int div_3 = i % 3;
        int div_5 = i % 5;
        if (!div_3 && !div_5)
        {
            printf("foobar\n");
        }
        else if (!div_3)
        {
            printf("foo\n");
        }
        else if (!div_5)
        {
            printf("bar\n");
        }
        else
        {

            printf("%d\n", i);
        }
    }
}
int main(int argc, char const *argv[])
{
    int number;
    scanf("%d", &number);
    fooBar(&number);

    return 0;
}

No comments:

Post a Comment

Comparing two files in C language.

Operations on More than one File.    # include < stdio.h > # include < stdlib.h > # include < string.h > int main ( int ...