Click here to Skip to main content
15,885,244 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
Hi. in program below percentage is showing 0.
I tried all datatypes int float and double but still same problem
#include <iostream>
using namespace std;

int marks=4;
int tmarks=10;
void result()
{
	double per;

	per=(marks/tmarks)*100;
	cout <<"percentage is "<<per<<endl;
	if (per>50)
	{	
	cout <<"pass"<<endl;
	}	
	else
	cout <<"fail"<<endl;
}

int main()
{
    result();
    
}


What I have tried:

#include <iostream>
using namespace std;

int marks=4;
int tmarks=10;
void result()
{
	double per;

	per=(marks/tmarks)*100;
	cout <<"percentage is "<<per<<endl;
	if (per>50)
	{	
	cout <<"pass"<<endl;
	}	
	else
	cout <<"fail"<<endl;
}

int main()
{
    result();
    
}
Posted
Updated 5-Jan-22 7:31am

Think about ...
per=(marks/tmarks)*100;

... and here especally about
(marks/tmarks)

Both marks and tmarks defined as int
int marks=4;
int tmarks=10;

with plain int 4/10 will always end with result 0.

Think about that:
double marks=4.0;
double tmarks=10.0;


I hope it helps.
 
Share this answer
 
Comments
Hamza Jameel 5-Jan-22 13:15pm    
Hi. Thanks for replying. It works perfectly individually. But putting it along with other functions in my project it still shows 0. double marks has incremenet as user tells right answers.
0x01AA 5-Jan-22 13:17pm    
Pls. give an example with 'that other functions' where it fails.
Hamza Jameel 5-Jan-22 13:22pm    
http://cpp.sh/2npgr
you can see code here
name and sapid can be anything.
Press 1 to start quiz(only this one works as programe is remaining are yet to complete.
0x01AA 5-Jan-22 13:25pm    
int marks=0;
Even it would be double marks=0.0;
0.0 devided by whatever results in 0.0, right?
Hamza Jameel 5-Jan-22 13:28pm    
0.0(marks) divided by double total marks
0.0(whatever result) / 10.0
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.
C++
per = (marks * 100.0)/tmarks; // This evaluates to (double)400.0/(int)15
                             // so the whole expression gets promoted to double

Perhaps it might be better to do the whole calculation in floating point by changing the type of marks and tmarks to double.
 
Share this answer
 
Comments
0x01AA 5-Jan-22 14:16pm    
Yep, 5
try to replace:
C++
per=(marks/tmarks)*100;

with
C++
per=marks*100/tmarks;

In C++, a division of integers id an integer division, and the result have no decimal.
 
Share this answer
 
Comments
0x01AA 5-Jan-22 13:30pm    
To the downvoter: Please explain the downvote. Basically this answer is leading to the right direction.
Patrice T 5-Jan-22 13:37pm    
Can someone explain me what is so wrong in my answer that it merit a downvote ?
0x01AA 5-Jan-22 14:17pm    
5
Patrice T 5-Jan-22 14:19pm    
Thank you.
What happened to your old account ?
0x01AA 5-Jan-22 14:28pm    
I deleted it in a fit of annoyance ...
... and now I start again, no big thing ;)

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