'How to solve this problem in DEV C+ percentage calculation?
I've got problem about percentage calculation in DEV C+.
I want to show the program to show the difference in percentage.
Instead of giving the calculated answer, the programs always give me zero.
Here's my code.
#include "stdio.h"
#include <string>
using namespace std;
int main()
{
int book1, baht1, book2, baht2, book3, baht3, book4, baht4, ova1, book5, baht5, book6, baht6, book7, baht7, book8, baht8, ova2, Delta, Zeta;
printf("\nPlease enter monthly plan order.");
printf("\n\n\n\nPlease enter the number of 'The Devotion of Suspect X' = ");
scanf("%d", &book1);
baht1 = book1 * 185;
printf("\n\nPlease enter the number of 'Norwegian Wood' = ");
scanf("%d", &book2);
baht2 = book2 * 200;
printf("\n\nPlease enter the number of 'Beautiful World, Where Are You' = ");
scanf("%d", &book3);
baht3 = book3 * 250;
printf("\n\nPlease enter the number of 'Fascism of Love and Fantasy' = ");
scanf("%d", &book4);
baht4 = book4 * 450;
printf("\n\n_____________________________\n");
printf("\n\nPlease enter this month's total sales.");
printf("\n\n\n\nPlease enter the number of 'The Devotion of Suspect X' = ");
scanf("%d", &book5);
baht5 = book5 * 225;
printf("\n\nPlease enter the number of 'Norwegian Wood' = ");
scanf("%d", &book6);
baht6 = book6 * 325;
printf("\n\nPlease enter the number of 'Beautiful World, Where Are You' = ");
scanf("%d", &book7);
baht7 = book7 * 385;
printf("\n\nPlease enter the number of 'Fascism of Love and Fantasy' = ");
scanf("%d", &book8);
baht8 = book8 * 600;
printf("\n\n_____________________________\n");
printf("\n_____________________________\n");
printf("\n\nAlright.");
printf("\n\nLet's see your work.");
ova1 = (baht1 + baht2 + baht3 + baht4);
printf("\n\n\nBudget Price = %d Bahts\n", ova1);
ova2 = (baht5 + baht6 + baht7 + baht8);
printf("\n\nMonthly Sales Price = %d Bahts\n", ova2);
Delta = (ova2 - ova1);
if (Delta >0)
printf("\n\n\nYour profit : %d Bahts\n", Delta);
else
printf("\n\n\nYour loss : %d Bahts\n", Delta);
Zeta = ((Delta)/(ova1) * 100;
if (Zeta >0)
printf("\n\nProfit were = %f percents\n", Zeta);
else
printf("\n\nLoss were = %f percents\n", Zeta);
return 0;
}
Is there's anything wrong about my code?
I'm not good at both English and programming.
Sorry for my broken English and any grammartical incorrect.
Thanks for your help (if there's any).
Solution 1:[1]
The primary problem is the type of numbers. The int number type is for whole numbers, like 3 and 99. The int type does not handle numbers like 3.5 or 0.99. If you try to put a number with a decimal point into an int, you will lose the details after the decimal point. That is why you are getting zero. For example, if Delta=20 and ova1=50, then your Zeta evaluation goes through these steps:
- 20/50 * 100;
- 0 * 100; because you lose the ".4"
- 0;
One way to improve it is to change the order of the evaluation by doing Zeta = 100*Delta/ova1; which does:
- 100*20/50;
- 2000/50;
- 40;
But that is not sufficient, because you will still be losing fractions. The other part of the problem is that Zeta should not be an int; it should be a double, and you need to convert (or "cast") the int calculation.
Near the top of your code, remove Zeta from the int line and add this line below:
double Zeta;
Then the line to calculate Zeta should become:
Zeta = (double)100*Delta/ova1;
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | Marty Pauley |
