
code help
hi i have got this code and i got it to equal 2000 but i need it to equal 1000 can somebody please help me
many thanks..
/calculate and output half of my savings
public class Savings
{
//your share is 50% of my savings
public static void main (String args[])
{
int mySavings = 2000;
int yourPercentage = 50;
int yourShare = (mySavings + (yourPercentage / 100));
System.out.println("Your share: " + yourShare);
}
}

Re: code help
Multiplication is used to find a percentage of some number, not addition.

Re: code help
i put in * m8 and i got an answer of 0?

Re: code help
Try replacing your code:
int yourShare = (mySavings + (yourPercentage / 100));
with:
int yourShare = (mySavings * (yourPercentage / 100));
your code should produce 2000.5 This code produces 1000.
Golfnut

Re: code help
//calculate and output half of my savings
public class Savings
{
//your share is 50% of my savings
public static void main (String args[])
{
int mySavings = 2000;
int yourPercentage = 50;
int yourShare = (mySavings*(yourPercentage / 100));
System.out.println("Your share: " + yourShare);
}
}
this is the code and i am still getting 0 as my answer

Re: code help
A hint:
what is an Integer from 50 / 100?

Re: code help
Duh. Not sure if this would be the preferred way to go about this, but this works:
double mySavings = 2000;
double yourPercentage = 50;
double yourShare = mySavings * (yourPercentage / 100);
System.out.println("Your share: " + yourShare);
}
}

Re: code help
It has to do with how integer division works. Integer division uses truncation of the remainder. If you divide 9 by 5, you get 1.8, but integer division truncates the decimal part. You would get 1 as an answer. While Golfnut tests solution works (side note to golfnut  don't spoon feed, and use code tags [code] YOUR CODE HERE [/code]), sometimes it's not feasible to have everything be an integer. In this case you can cast one of the numbers to a floating point form (double or float). Integer division only occurs with 2 integers, if there exists one non integer, the compiler will handle everything for you.
is fine in most cases.