Click here to Skip to main content
15,884,388 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
#include<iostream>

using namespace std;


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

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

    if(percentage>=75){
        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.
Posted
Updated 22-May-22 9:08am
v2
Comments
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
C
percentage = (float)attended/(float)held * 100.0;
 
Share this answer
 
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

C#
percentage = (attended *100) / held;
 
Share this answer
 
v3
Comments
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✌️
data type of percentage must be float

#include<iostream>

using namespace std;


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


cout<<"Number of classes attended\n";
cin>>attended;

cout<<"Number of classes held\n";
cin>>held;

percentage = (attended / held)*100;

cout<<"your attendace is\n"<<percentage<<"%"<<endl;

if(percentage="">=75){
cout<<"you are allowed to sit in exams\n";
}else {
cout<<"you are not alowed to sit in exams";
}
}

try this code this will work 100%

>
 
Share this answer
 
v2
Comments
Tony Hill 22-May-22 15:29pm    
Just out of curiosity how do you suggest you solve the original problem if you cannot use floats.

For example can the person attending a class attend 3.5 classes or even more puzzling can you hold 2.5 classes.
Richard Deeming 25-May-22 5:36am    
As solution 2, posted eight months before yours, already explained.

Stick to answering new questions unless you have something new to add to the discussion.

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