Repeat commands to restrict a valid value

3

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

I would like help 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);
}
    
asked by anonymous 06.09.2015 / 05:05

1 answer

2

I solved other problems, I formatted better for easier reading. See that even seeming silly, the spaces, the punctuation, everything helps.

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

And if you must stay within the loop while the condition is invalid, you must invert the operator than it is valid. 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 may think. Obviously the opposite of < is >= . If the opposite of > was < , then the equal value would be what? Would I be lost in space? The equal has to enter on one side.

#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 running on ideone .

That's not the way the cousin is, but I'll let you fix it because it's not the focus of the question.

    
06.09.2015 / 05:25