How to present only negative values in C?

Asked

Viewed 10,388 times

2

I have three variables :

int numero1,numero2,numero3;

After you have declared them, I am asked the user to fill them in :

        printf("Digite o primeiro numero inteiro: ");
        scanf("%d", &numero1);
        printf("Digite o segundo numero inteiro: ");
        scanf("%d",&numero2);
        printf("Digite o terceiro numero inteiro: ");
        scanf ("%d",&numero3);

And based on this, I need to verify which of the numbers typed by the user are negative.

I have a method that checks whether the numbers typed are positive :

void Positivo(int valor) {
        for(count = 1;count <= valor; count++) {
            if(count > 0) {
                printf("%d \n",count);
            }
        }
    }

And I also have one that checks if the numbers typed are negative :

void Negativo(int valor) {
    for(count = 0;count >= valor; count++) {
        if(count < 0) {
            printf("%d \n", count);
        }
    }
}

The problem is that when typing the numbers, they do not appear anything, because it seems that it is not recognized by the function scanf() the negative numbers.

Screenshot of the program :

So how can I fix this ? Causing the user to enter the negative numbers, can read the variables correctly, and show only the negative numbers.

1 answer

3


You just need to check if the number is higher or lower than 0.

void negativo_print(int a);
void positivo_print(int a);

int main(int argc, char *argv[]) {

    int a, b, c;

    puts("Digite o primeiro digito:");
    scanf("%d", &a);
    puts("Digite o segundo digito:");
    scanf("%d", &b);
    puts("Digite o terceiro digito:");
    scanf("%d", &c);

    negativo_print(a);
    negativo_print(b);
    negativo_print(c);

    return 0;
}

void negativo_print(int a){
    if(a < 0){
        printf("Numero: %d\n", a);
    }
}
void positivo_print(int a){
    if(a > 0){
        printf("Numero: %d\n", a);
    }
}

You can also use matrices:

int main(int argc, char *argv[]) {
    int n[2], i;
    size_t tmh = *(&n + 1) - n;

    puts("Digite o primeiro numero: ");
    scanf("%d", &n[0]);
    puts("\nDigite o segundo numero: ");
    scanf("%d", &n[1]);
    puts("\nDigite o terceiro numero: ");
    scanf("%d", &n[2]);

    for(i=0; i<tmh+1; i++){
        if(negativo(n[i])){
            printf("numero: %i\n", n[i]);
        }
        continue;
    }
    return 0;
}

In the loop for simply check if the number is negative. If it is printed on the screen, if it does not skip to the next iteration if there is.

And create a function that checks whether the number is positive or negative, zero is neutral:

int negativo(int a){
    if(a < 0) 
    {
        //negativo
        return 1;
    }
    // positvo
    return 0;
}
  • I believe that in the case, who posted the problem, does not want to use arrays to solve the problem, since according to what it shows in the problem, it intends to use integers.

  • So, that’s exactly what @Falion said, I need to use whole numbers and not arrays. If possible, I would like to know how to check with integers.

  • @Edilson It was not I who denied, nor do I even have enough points to be able to deny. Thank you for your patience.

  • @Edilson The code worked, thank you very much.

  • @Sure, problem will be if you have to add more numbers.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.