Why am I receiving a floating point exception when I run the following code?

Viewed 64

Basically it's a code to input the scores and calculating if the student has passed or not, based upon a criterion. In VScode, after taking the input values, the program just terminates without either a return value of 0 or an error. In an online C compiler, Float point exception prints out in the terminal. What's going on?

int main(){
    int enteries; 
    scanf("%d", &enteries); 

    int pass, fail, spass, sfail, avg = 0;
    

    while ( enteries != 0){
        int score;
        printf("Print score"); 
        scanf("%d", &score); 
        if (score<60){
            fail = fail + 1; 
            sfail = score + sfail;
            enteries -= 1;
        }
        else if(score >= 60 && score <=100){
            pass = pass + 1; 
            spass = spass + score;
            enteries -= 1; 
        }
         
    } 

    int sum = spass + sfail; 
    avg = sum / enteries; 
    printf("%d" , avg);  

    return 0; 

}
1 Answers

After the while ( enteries != 0) while-loop, enteries will be zero.

Then

avg = sum / enteries; 

divides by zero, causing a divide-by-zero exception.

Related