Guideness Required

Created a program to get total marks from the user
Calculated his obtained and then display the total obtained marks and percentage
i wonder what is the mistake in the program because the percentage is always displayed 0
 

#include<iostream.h>
#include<conio.h>
#include<math.h>
int main()
{
int tm, sb1, sb2,ob;
float p;
cout<<"total marks = ";
cin>>tm;
cout<<"subject 1 = ";
cin>>sb1;
cout<<"subject 2 = ";
cin>>sb2;
ob=sb1+sb2;
p=ob/tm*100;
cout<<"Total obtained = "<<ob<<endl;
cout<<"percentage"<<p;
getch();
}
It's probably because you're doing integer division.

int tm, sb1, sb2,ob; // Change them to float

Also, you want parenthesis in your calculations otherwise it goes wrong.

p=ob/ (tm*100); // added ( ) around tm*100
Or you can cast one of the variables to a float.
1
2
// C-style cast
p = (float)ob / (tm*100);

http://www.cplusplus.com/doc/tutorial/typecasting/
Topic archived. No new replies allowed.