Home > database >  C Programming: Print "Hello", "Red", "Green", "Blue" for dif
C Programming: Print "Hello", "Red", "Green", "Blue" for dif

Time:09-14

My brain has just hit a wall and I'm not sure how to do this without going into an infinite loop and I mean both my brain and the code! No idea if I have over complicated it to the max or if I am on the right track. Any help would be much appreciated!

The problem:

I want to print a list of numbers similar to a Fizz Buzz sequence but instead they say "Hello", "Red", "Green" and "Blue" from 0 to 24. I want to use a for loop but I am stuck on how to add the logic for every occurrence.

For even numbers I want it to print "Hello" and I have already worked that out using modulo.

Now, I want Red, Green and Blue to start at different places but obey the same equation of: "x 6" at their respective starting points 1, 3 and 5.

I'm not sure how I can assign the values within the array(s) to the "i" variable of the for loop

Objectives:

Be able to print "Hello" for every even number.

Be able to print "Red" at = {1, 7, 13, 19}.

Be able to print "Green" at = {3, 9, 15, 21}.

Be able to print "Blue" at = {5,11,17,23}.

My code in C:

int main ()
{
    int i,j,k,m;

    int R[] = {1,7,13,19}; 
    int G[] = {3,9,15,21}; 
    int B[] = {5,11,17,23};

    for (i = 0; i < 24; i  )
    {
        /* Even Numbers */
        if (i % 2)
        {
            printf("Hello\n", i);
        }
        /* Odd Numbers */
        if (i % 2 == 1)
        {
            for (j = 0; j < 4; j  )
            {
                /* Red */
                if (i = R[j]) 
                    printf("Red\n", i);
            }
            for (k = 0; k < 4; k  )
            {
                /* Green */
                if (i = G[k]) 
                    printf("Green\n", i);
            }
            for (m = 0; m < 4; m  )
            {
                /* Blue */
                if (i = B[m]) 
                    printf("Blue\n", i);
            }
        } 
    }
    return 0;
}

Sample Output:

0 Hello

1 Red

2 Hello

3 Green

4 Hello

5 Blue

6 Hello

CodePudding user response:

If I have understood correctly you need something like the following

#include <stdio.h>

int main( void ) 
{
    enum { Red = 1, Green = 3, Blue = 5 };

    for ( unsigned int i = 0; i < 24; i   )
    {
        if ( i % 2 == 0 )
        {
            printf( "%u %s\n", i, "Hello" );
        }
        else
        {
            switch( i % 6 )
            {
            case Red:
                printf( "%u %s\n", i, "Red" );
                break;

            case Green:
                printf( "%u %s\n", i, "Green" );
                break; 

            case Blue:
                printf( "%u %s\n", i, "Blue" );
                break;
            }
        }
    }
}

The program output is

0 Hello
1 Red
2 Hello
3 Green
4 Hello
5 Blue
6 Hello
7 Red
8 Hello
9 Green
10 Hello
11 Blue
12 Hello
13 Red
14 Hello
15 Green
16 Hello
17 Blue
18 Hello
19 Red
20 Hello
21 Green
22 Hello
23 Blue

Or if you want to insert a blank line between two sequential outputs then just add one more new line character '\n' in the format string of the calls of printf like for example

printf( "%u %s\n\n", i, "Hello" );
              ^^^^^ 

Another approach is the following

#include <stdio.h>

int main( void ) 
{
    const char * color[] = { "Hello", "Red", "Green", "Blue" };

    for ( unsigned int i = 0; i < 24; i   )
    {
        if ( i % 2 == 0 )
        {
            printf( "%u %s\n", i, color[0] );
        }
        else
        {
            printf( "%u %s\n", i, color[i % 6 / 2   1] );
        }
    }
}

The program output is the same as shown above.

CodePudding user response:

I hope it is what you wanted

#include <stdio.h>
            
int main() {
    int i=1;
    int j=3;
    int k=5;

    for(int m=0;m<24;m  ){
        if(m%2==0){
            printf("%d hello\n",m);
        }
        if(m==i){
            printf("%dred\n",m);
            i=i 6;
        }

        if(m==j){
            printf("%dgreen\n",m);
            j=j 6;
        }
            
        if(m==k){
            printf("           
  • Related