Please post what you'vre done so far

Solved

Posted on 2007-08-11

user input 2 numbers, the program need print out the remainder of these two numbers. can not use mod functions.

my method is use a simple loop that persorms the subtraction (big number - small number), but i found if user input negative number, this program can not print out correct answer

anyone can help me?

my method is use a simple loop that persorms the subtraction (big number - small number), but i found if user input negative number, this program can not print out correct answer

anyone can help me?

6 Comments

so you simply have to check before your loop if the 2 values are positive numbers.

So, first determine the sign of the first argument (boolean isNeg = first<0;).

Then, subtract the absolute value of the second argument from the absolute value of the first argument until you get a value less than the first number.

Finally, if the first argument was negative, subtract the return value from zero.

That is, first assign your return value to the absolute value of the first argument, then, WHILE the difference between the return value and the second argument is greater than the absolute value of the first argument, subtract the absolute value of the second argument from the return value.

It would be much simpler to post the code, but it would be wrong for me to just do your homework for you.

Title | # Comments | Views | Activity |
---|---|---|---|

upcasting down casting object remains same but refernece changes | 8 | 47 | |

Books that can get me started on JAVA | 2 | 35 | |

strCopies challenge | 17 | 60 | |

json format text only | 4 | 50 |

Join the community of 500,000 technology professionals and ask your questions.

Connect with top rated Experts

**22** Experts available now in Live!