Repeating commands to constrain a valid value

Question:

I need a code that reads a number greater than 1, and if a number less than or equal to zero is typed, another number is requested, and therefore can be tested if true.

I would like assistance in completing my code.

#include <stdio.h>

int main()


{
int num,primo;
num=1;
do
{
    printf("Informe um numero primo maior que 1:");
    scanf("%d",&primo);

    if((primo%num)==0)
    printf("Numero primo",primo);

    else
    printf("Numero não primo");
}
while(primo>1);
}

Answer:

Solved other problems, formatted better for readability. See that even if it seems silly, spaces, punctuation, everything helps.

If the problem is to keep repeating the order until an invalid value is typed in then you should only put inside the repetition what needs to be done again and the only thing that has to be done again is the typing order. The rest stay out of the loop.

And if you must stay inside the loop while the condition is invalid, you must reverse the valid operator. That is, if you need the value to be > 1 , it should be stuck as invalid if the value is <= 1 .

And this is another important learning experience. The opposite of > is <= and not < as many might think. Obviously the opposite of < is >= . If the opposite of > were < , then what would the equal value be? Would it be lost in space? The equal has to enter one of the sides.

#include <stdio.h>

int main() {
    int num = 1, primo;
    do {
        printf("Informe um numero primo maior que 1:");
        scanf("%d", &primo);
    } while (primo <= 1);
    if (primo % num == 0) {
        printf("Numero primo %d", primo);
    } else {
        printf("Numero não primo");
    }
}

See working on ideone . And on repl.it. Also posted on GitHub for future reference .

That's not how you think of cousin, but I'll let you fix it because that's not the focus of the question.

Scroll to Top