Home > Software design >  How can i run a command only one time inside of for loop?
How can i run a command only one time inside of for loop?

Time:08-20

How can i run a command only one time inside of for loop? I couldn't find how to do it so i wrote code like that. But the problem on this code you can see when the program goes to else command it doesn't work.

I just wanted to see odd and even numbers with using for loop but i don't want to get result like; Even numbers :12 Even numbers :28 Even numbers :46

I just want to see only one time print Even numbers and then print numbers like; Even numbers: 12 28 46 e.t.c

I hope i could explain clear. Waiting for your help. Thanks.

My alternative but wrong code is;

#include <stdio.h>

int main()
{   
    int num [] = {12, 14, 16, 33, 65, 98, 45, 25, 87, 18, 20};
    
    printf("even numbers:");
    printf("\t\t\t\t Odd numbers:");

    for (int i = 0; i < 11; i  ) {
        if (num[i] % 2 == 0) {
            printf("\n%d", num[i]);
        }
        else {
            printf("\t\t\n%d");
        }   
    }
        
    return 0;
}

CodePudding user response:

You want two loops:

#include <stdio.h>

int main(void)
{
    int num[] = {12,14,16,33,65,98,45,25,87,18,20};
    size_t n = sizeof num / sizeof *num;

    printf ("Even numbers:\t\t\t\tOdd numbers:\n");

    for (size_t i = 0; i < n; i  ) {
        if (num[i] % 2 == 0) {
            printf ("%d ", num[i]);
        }
    }
    printf("\r\t\t\t\t\t");
    for (size_t i = 0; i < n; i  ) {
        if (num[i] % 2 != 0) {
            printf ("%d ", num[i]);
        }
    }
    printf("\n");
    return 0;
}

Output:

Even numbers:               Odd numbers:
12 14 16 98 18 20           33 65 45 25 87 

Notice the \r to go to the beginning of the line

CodePudding user response:

Here a different format:

#include <stdio.h>

int main(void) {
    unsigned num []={12,14,16,33,65,98,45,25,87,18,20};
    printf("even numbers"
        "\todd numbers\n");
    const char *prefix[] = {"", "\t\t"};
    for(unsigned i = 0; i < sizeof(num) / sizeof(*num); i  ) {
        printf("%s%u\n", prefix[num[i] % 2], num[i]);
    }
}

and the output is:

even numbers    odd numbers
12
14
16
                33
                65
98
                45
                25
                87
18
20

CodePudding user response:

You just want evens on one line, and odds on the next, right?

"Factoring out" common processing into a function is always a good idea.

#include <stdio.h>

void show( int num[], int nItems, char *title, int rem ) {
    printf( title );
    for( int i = 0; i < nItems; i   )
        if( num[i]%2 == rem )
            printf( "%d ", num[ i ] );
    printf( "\n" );
}

int main () {
    int num [] = { 12, 14, 16, 33, 65, 98, 45, 25, 87, 18, 20 };

    show( num, sizeof num/sizeof num[0], "even numbers: ", 0 );
    show( num, sizeof num/sizeof num[0], "odd numbers: ", 1 );

    return 0;
}

Output:
even numbers: 12 14 16 98 18 20
odd numbers: 33 65 45 25 87
  •  Tags:  
  • c
  • Related