0

Percentage of a variable

In c++, how do I turn a number that's in a variable into a percentage, as I don't know the number in advance, it depends on the users input. For example: 15 is in the variable, so I want 15% of 50. I've tried 'price = 50 - (perc)%;' & 'price = 50 - (perc%);', but nothing works. Thanks in advance.

9th Feb 2021, 1:12 PM
Brian Curran
Brian Curran - avatar
3 Respostas
0
15% means 15/100*number, so it should be 50-(50*15/100)
9th Feb 2021, 1:18 PM
Abhay
Abhay - avatar
0
in code languages % operator is not the percent (it's almost the modulo, ie: to get the remainder of integer division)... percentages needs to be "manually" computed: price = price - price*perc/100; // or: price*(1-perc/100)
9th Feb 2021, 4:29 PM
visph
visph - avatar
0
Of course, I should have known that. Thanks guys!
10th Feb 2021, 3:34 PM
Brian Curran
Brian Curran - avatar