How to decrease, except a number of the other value?

Asked

Viewed 53 times

0

In the final value was to have the total of notes greater than 7, another condition would be that the user has to enter a number between 0 and 10, then any number outside of it would give "invalid message", and would have the decrease of it, so for example if the user typed: 11, 9 and 8; instead of appearing in total 3 notes > 7, only 2 would appear... But I’m not able to do it, so if someone knows how to solve it, or another way to do it, say it in the comments... Thanks in advance.

#include <stdio.h> 

int main () { 
    int aluno, i, alunosComNotaMaiorQueSete = 0;
    float nota;

    printf("Quantos alunos fizeram a prova? ");
    scanf("%d", &aluno);

    for(i = 0; i < aluno; i++) { 
   
        printf("Digite a nota do aluno %d:\n", i);
        scanf("%f", &nota);

    if (nota<0 || nota>10)
   { 
        printf("\n Nota Invalida\n");
        alunosComNotaMaiorQueSete--;
   }else if(nota>7 && nota<=10) 
   {
        alunosComNotaMaiorQueSete++;
   }
    }     
    printf("Alunos com nota > 7: %d\n", alunosComNotaMaiorQueSete);

    return 0;
}

  • Stackoverflow code emulator is for Javascript, HTML or CSS, not C

1 answer

2


You don’t have to decrement it only adds up if the grade is higher. And you also don’t need to say that the note < 10 on Else if will only pass to Else if the condition of the first if is true.

#include <stdio.h> 

int main () { 
int aluno, i, alunosComNotaMaiorQueSete = 0;
float nota;

printf("Quantos alunos fizeram a prova? ");
scanf("%d", &aluno);

for(i = 0; i < aluno; i++) { 

    printf("Digite a nota do aluno %d:\n", i);
    scanf("%f", &nota);

    if (nota<0 || nota>10)
    { 
        printf("\n Nota Invalida\n");
        //codigo antigo que nao é preciso: 
        //alunosComNotaMaiorQueSete--;
    }
    else if(nota>7 /*&& nota<=10*/) 
    {
        alunosComNotaMaiorQueSete++;
    }
}        
    printf("Alunos com nota > 7: %d\n", alunosComNotaMaiorQueSete);
    return 0;
}
  • thanks, thanks a lot...

Browser other questions tagged

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