Rohan Meharchandani
Rohan Meharchandani

Reputation: 3

Arithmetic operation doesn't work with variable

I created this variable float avg_perc and performed some arithmetic operation to find average percentage. The output is unexpected i.e. 0 for any value of marks1, mark2, marks3.

#include <stdio.h>

int main(){
    int marks1, marks2, marks3;
    printf("Enter your marks in all 3 subjects : ");
    scanf("%d %d %d", &marks1, &marks2, &marks3);
    float avg_perc = ((marks1+marks2+marks3)/300)*100;    //problem part
    printf("%f \n", avg_perc);
    if(avg_perc>=40 &&marks1>=33 && marks2>=33 && marks3>=33){
        printf("Pass.");
    }
    else{
        printf("Fail.");
    }
     return 0;

}

Upvotes: 0

Views: 214

Answers (3)

Rohan Bari
Rohan Bari

Reputation: 7726

You need to make the divisor a type of double by appending .0 into it to avoid the integer division and this was exactly the reason you were having this strange issue.

Change:

(marks1 + marks2 + marks3) / 300

into:

(marks1 + marks2 + marks3) / 300.0

Note that if you put a floating point prefix, for instance 300.0f then you'll see a miscalculation. For example, you get 30.000002 after inputting 30 30 30 in the program.

Upvotes: 0

Lundin
Lundin

Reputation: 214300

It doesn't really matter that you have float avg_perc = ... for storing the result, because that type does not affect the type used for the calculations.

In C, every operand and integer constant in an expression has a type. And depending on the types of the operands, each operation is carried out on a specific type which then also becomes the resulting type of the (sub) expression.

In this case the types are as indicated by the comment below:

    float avg_perc = ((marks1 + marks2 + marks3) / 300) * 100;
//        float        int    + int    + int     / int  * int

Operator precedence determines which operands that belong to which operator. In this case the () parenthesis has the highest precedence, then * and /, then + and finally =.

The subexpression (marks1 + marks2 + marks3) will get all calculations carried out on int type since all involved operands of + are int. Then the result of that will form a new expression "result / 300" where 300 is int. Again, calculation is carried out on int. And then finally the same thing with * 100.

When all the above calculations have been carried out on int, then the assignment happens last of all, since = has the lowest precedence. During assignment, there is a special conversion rule stating that the value of the right operand is is converted to the type of the left operand. So this conversion of the result to float happens last, when all of the other calculations have already been carried out on int.

Upvotes: 2

Sourav Ghosh
Sourav Ghosh

Reputation: 134356

In your code

  ((marks1+marks2+marks3)/300)*100; 

is integer arithmetic. You need to ensure at least one of the participating arguments are floating point (or casted to a float or double) in order to ensure floating point arithmetic. Something like

(((float)marks1+marks2+marks3)/300)*100;

Upvotes: 0

Related Questions