mercredi 23 décembre 2020

Is there an efficient way to build positive-negative number pattern in C?

hey im new in C programming here, currently i'm doing a self learning and have found this quest from some university modul that hasn't been answered yet it asked for write a program for this number pattern

i already tried to write but i couldn't find the best condition that suit this pattern. here's my code

#include <stdio.h>
int even(int input)
{
    int num;
    for(int i=2; i<=input*2; i+=2) {
        if (i == 2)
            printf("%d", i * -1);
        else {
            if (i % 2 <= 3 || i % 2 != 0)
                printf("%d", i * -1);
            else
                printf("d",i);
        }
    }
}
int odd(int input)
{} /* i haven't code for the odd one yet because its almost similar with the even*/

int main ()
{
    int N;
    puts("provide N: ");
    scanf("%d",&N);
    even(N);
    odd(N);
    return 0;
}

and the result are shown here Result

which line should i working on?

Aucun commentaire:

Enregistrer un commentaire