Home > Software engineering >  How to increase a value by 50%? [duplicate]
How to increase a value by 50%? [duplicate]

Time:09-29

Question

I have example of salary = 2000 and I want to increase the salary by a bonus of 50%, resulting in a salary of 3000.

So like:

salary = salary   50% of the salary

Attempt

I tried implementing it like this:

int salary = 2000;
salary = salary   50 % salary;

System.out.println(salary);

but I got 2050 as result, not 3000. What am I doing wrong?

CodePudding user response:

You'd multiply

int salary = 2000;  
salary = (int) (1.5 * salary);

Note that this is only correct for even integers.

You shouldn't use ints, floats, or doubles for currency types

CodePudding user response:

Try the following:

int salary = 2000;
double finalSalary = salary   0.5*salary;
System.out.println(finalSalary);

or

int salary = 2000;
double finalSalary = salary * 1.5;
System.out.println(finalSalary);

CodePudding user response:

50 % salary

means "the remainder of dividing 50 by salary", because % is the remainder operator. Since 50 < salary, the value of that expression is just 50.

50% of salary is given by salary / 2. So you can write:

salary = salary   (salary / 2);
// or
salary  = salary / 2;
// or
salary = salary * 3 / 2;
// etc.
  • Related