Click here to Skip to main content
15,068,646 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:

using namespace std;

int main(){
    int attended,held,percentage;

    cout<<"Number of classes attended\n";
    cout<<"Number of classes held\n";
    percentage = (attended / held)*100;
    cout<<"your attendace is\n"<<percentage<<"%"<<endl;

        cout<<"you are allowed to sit in exams\n";
    }else {
        cout<<"you are not alowed to sit in exams";

What I have tried:

i have tried to write percentage formula in different ways,But it din't work.
Updated 20-Aug-21 5:03am
jeron1 20-Aug-21 10:39am
Try something like,
percentage = ((float)attended / (float)held)*100;

Your percentage calculation involves all integer values, so the calculation is done using integer math. So, for example, if attended=7 and held=24, then (int)7/(int)24 = (int)0. Try instead
percentage = (float)attended/(float)held * 100.0;
Your are dividing one integer by another and expecting a float but the division of one integer by another always result in an integer answer so there is no fractional part to multiply by 100.

Try this

percentage = (attended *100) / held;
Sohit Rana 20-Aug-21 10:59am
float was just a experiment it also didn't worked for (int percentage;).
But (percentage = (attended *100) / held;) worked and now i know my mistake.
Thanks bro✌️

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900