#include <iostream>
using namespace std;
int main(){
int marks;
float percentage;
cout<<"Marks of Robert are as follows :-"<<endl;
cout<<"78,45,62";
marks=78+45+62;
percentage(marks/300)*100;
cout<<"marks of Robert : "<<marks<<endl;
cout<<"Percentage scored by Robert : "<<percentage<<endl;
return 0;
}
Asked
Active
Viewed 52 times
-3

Nathan Pierson
- 5,461
- 1
- 12
- 30

Rohit Singh
- 1
- 1
-
1Looks like the same problem as [this stab](https://stackoverflow.com/questions/69057703/i-am-trying-to-build-an-program-which-calculates-your-percentage-and-its-giving) at the question. `marks` is an integer. `marks/300` is still an integer, it doesn't get converted to a `float` until after the division has been computed. – Nathan Pierson Sep 07 '21 at 04:44
-
No it is not. Use a debugger. Int 185/Int 300 is zero. You should use float pointing arithmetic in this case. Try to change marks or 300 to float and you will see your result. – honzakuzel1989 Sep 07 '21 at 04:45
-
`percentage = (marks/300)*100;` – Wang Liang Sep 07 '21 at 04:47
-
yeah it worked i've converted the 'marks' to float. – Rohit Singh Sep 07 '21 at 04:54
-
Thnx everybody.. ^_^ – Rohit Singh Sep 07 '21 at 04:56
-
1What is the max marks? 300? If so, `percentage = marks*100.f/300;` may give a better result. – Ted Lyngmo Sep 07 '21 at 04:56
1 Answers
1
the problem is that int
cannot have decimals.
so this will solve your problem:
percentage = static_cast<float >(marks) / 300 * 100;
explanation: casting one of the integers to float
will cause the compiler to implicitly convert others to float
. so the final result will contain decimals

Ted Lyngmo
- 93,841
- 5
- 60
- 108

Mohammad Mostafa Dastjerdi
- 674
- 1
- 6
- 21
-
2A simpler version: `percentage = marks/300.f*100;` or `percentage = marks*100.f/300;` – Ted Lyngmo Sep 07 '21 at 05:06