12

How can I calculate several percent of int?
for example I want to get 30% from number, if I will use this example of code, I will get wrong answer:

int number = 250;
int result = (number / 100) * 30;  

result will be 60, and the real answer is 75, is there any way to calculate it?

banarun
  • 2,305
  • 2
  • 23
  • 40
user1544067
  • 1,676
  • 2
  • 19
  • 30
  • 4
    You are doing an integer division 250/100 = 2.5 but as an integer it is 2... The easiest way to fix is to make it a floating point divide by making the 100 a floating point value of 100.0 instead. "int result = (number / 100.0) * 30;" – jcoder Jul 01 '13 at 13:42

11 Answers11

18

Multiply before dividing:

int result = number * 30 / 100;

The reason you get the result you get is that division with integer types produces an integer result: 250 / 100 is 2. If you multiply before dividing you still get an integer result, but at least you haven't lost data in intermediate steps. If you have to deal with really huge numbers there is a danger of overflowing the range permitted by int though.

Alternatively you can use floating point arithmetic, where division can produce fractions of integers:

int result = number * 0.30;

This may produce unexpected results though, so you're better off using integers only as above. Or write 3.0/10 instead of 0.30.

Community
  • 1
  • 1
Joni
  • 108,737
  • 14
  • 143
  • 193
  • Integer casting is redundant as the result will be casted implicitly. – Sceptical Jule Jul 01 '13 at 13:47
  • Thanks, explicit cast removed. I thought the compiler would at least warn about the loss of precision... – Joni Jul 01 '13 at 13:50
  • 3
    Most compilers will spit out warnings about the implicit cast. The explicit cast is valuable in that it makes your intent clear to other developers and the compiler. The reason for the warnings is that with the implicit cast there's no way to know if the cast was intended or a mistake. – Sean Middleditch Jul 01 '13 at 18:05
  • What if the result of the first multiplication overflows `int`? For example, consider this code: `int number = 1000000000`; int result = number * 30 / 100;`. The `result` wouldn't be 300000000 because of the overflow. – LRDPRDX Dec 11 '19 at 08:08
3

Assuming the numbers are small(ish), you can just turn it around:

 int result = (number * 30) / 100;

(Parenthesis not required, but helps clarify).

This won't work if either of the numbers are several million, but should be fine for numbers smaller than that.

Mats Petersson
  • 126,704
  • 14
  • 140
  • 227
3

Switching the operands (as others suggested) would work too, but just in case you do not want to, there's another solution:

int number = 250;
int result = static_cast<double>(number) / 100 * 30; 
Sceptical Jule
  • 889
  • 1
  • 8
  • 26
2

Try this,

int number = 250;
float result = number  * (float)(30/100.0);
CodeCrusader
  • 559
  • 2
  • 7
  • 13
1

Another possibility, good in Windows when working with 32 bit numbers.

int num = 750;

int result;

result = MulDiv( num, 30, 100 );

https://learn.microsoft.com/en-us/windows/win32/api/winbase/nf-winbase-muldiv

Tim
  • 11
  • 1
0

You need to perform floating point math, otherwise the compiler does all the arithmetic here as integers.

You might try

int result = (number / 100.0) * 30;
Emil Sit
  • 22,894
  • 7
  • 53
  • 75
0

Use float:

float number = 250;
float result = (number / 100.0) * 30

Also just putting there the 100.0 with deciaml point might be enough.

Because if you do it your way 250 / 100 in integer equals 20( you can put 100 in 200 only twice and integer doesn't care about the rest 50) and times 30 = 60.

Ms. Nobody
  • 1,219
  • 3
  • 14
  • 34
0

Now I think about some solution:

int number = 250;
int result = number * 100;
result = (result / 100) * 30;
result /= 100;
user1544067
  • 1,676
  • 2
  • 19
  • 30
0

The whole program is like:

double number , result;

cout << "Enter the number:";
cin >> number;

result = number * 30.0 / 100;
cout << "Result = " << result << endl;

and you will get your answer....

-1

score/items*30 ex;

15%

int score, items;
cout<<"input your score"<< score<<endl;
cin>>score;
cout<<"input number of items"<<items<<endl;
cin>>items;
float total=score/items*15
cout<<"you got "<<total<<""<<endl;
Garf365
  • 3,619
  • 5
  • 29
  • 41
-1

This is a First-degree equation, simpler than it looks!

float annualGrossSalary;
cout << "Please enter your annual salary: ";
cin >> annualGrossSalary;
float annualNetSalary = (annualGrossSalary - (annualGrossSalary / 100) * 40);
float monthyGrossSalary = annualGrossSalary / 12;
float monthyNetSalary = annualNetSalary / 12;
cout << "Your monthly gross salary is " << monthyGrossSalary << "\n";
cout << "Your monthly net salary is " << monthyNetSalary << "\n";
cout << "In 10 years you will earn " << annualNetSalary * 10 << "\n";