percentage difference of variables

hi can someone help - when i calculate the percentage different of the actual number and the approximate number (calculating farenheit when knowing celsius) with this formula it always returns 0? cant seem to work it out

1
2
3
4
5
6
7
8
9
10
11
x=0;
	for (x>=0; x<=100; x++)
	{
		approx = (x*2)+30;
		actual = (x*9.5)+32;
		diff = actual-approx;
		pdiff = (((actual-approx)/actual)*100);

		
		cout << x << "\t" << approx << "\t" << actual << "\t" << diff << "\t" << "\t" << pdiff << "\n";
	}
How do you have your variables defined? Sounds like you might be running into integer division giving you result of 0. I defined them as double and the output works with the rest of your code.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
#include <iostream>
using namespace std;

int main() {

double approx,actual,diff,pdiff;

for (int x=0; x<=100; x++)
	{
		approx = (x*2)+30;
		actual = (x*9.5)+32;
		diff = actual-approx;
		pdiff = (((actual-approx)/actual)*100);

		
		cout << x << "\t" << approx << "\t" << actual << "\t" << diff << "\t" << "\t" << pdiff << "\n";
	}
	return 0;
}


0	30	32	2		6.25
1	32	41.5	9.5		22.8916
2	34	51	17		33.3333
3	36	60.5	24.5		40.4959
4	38	70	32		45.7143
5	40	79.5	39.5		49.6855
6	42	89	47		52.809
7	44	98.5	54.5		55.3299
8	46	108	62		57.4074
9	48	117.5	69.5		59.1489
10	50	127	77		60.6299

...

97	224	953.5	729.5		76.5076
98	226	963	737		76.5317
99	228	972.5	744.5		76.5553
100	230	982	752		76.5784
thanks!
Topic archived. No new replies allowed.