As Solution 1 points out, C++ does integer math when given all integer terms. So even the proposed solution of
(marks * 100)/tmarks
, may not give you what you expect. If marks = 4 but tmarks = 15, this evaluates to (int)400/(int)15, withe a final value of (int)26.
If, on the other hand you use a floating point constant, then you get 26.6667, which might be closer to what's expected. E.G.
per = (marks * 100.0)/tmarks;
Perhaps it might be better to do the whole calculation in floating point by changing the type of marks and tmarks to double.