Solved

# how to get the remainder of two input number?

Posted on 2007-08-11
Medium Priority
1,214 Views
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?
0
Question by:ericylr
• 3
• 2

LVL 86

Expert Comment

ID: 19676135
Please post what you'vre done so far
0

LVL 143

Expert Comment

ID: 19676418
>but i found if user input negative number, this program can not print out correct answer

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

LVL 11

Expert Comment

ID: 19676522
The mod function always returns a value that is the same sign as the first number, regardless of the sign of the second number.

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.
0

LVL 11

Accepted Solution

petiex earned 80 total points
ID: 19676556
Correction, not until, but while:

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.
0

LVL 11

Expert Comment

ID: 19676646
Also, if the absolute value of the first argument is less than the absolute value of the second argument, then just return the first argument.
0

LVL 86

Expert Comment

ID: 19677024
0

## Featured Post

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

INTRODUCTION Working with files is a moderately common task in Java.  For most projects hard coding the file names, using parameters in configuration files, or using command-line arguments is sufficient.   However, when your application has vi…
Introduction Java can be integrated with native programs using an interface called JNI(Java Native Interface). Native programs are programs which can directly run on the processor. JNI is simply a naming and calling convention so that the JVM (Java…
Viewers will learn about the different types of variables in Java and how to declare them. Decide the type of variable desired: Put the keyword corresponding to the type of variable in front of the variable name: Use the equal sign to assign a v…
This video teaches viewers about errors in exception handling.
###### Suggested Courses
Course of the Month16 days, 13 hours left to enroll