?
Solved

how to get the remainder of two input number?

Posted on 2007-08-11
7
Medium Priority
?
1,214 Views
Last Modified: 2013-11-05
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
Comment
Question by:ericylr
  • 3
  • 2
6 Comments
 
LVL 86

Expert Comment

by:CEHJ
ID: 19676135
Please post what you'vre done so far
0
 
LVL 143

Expert Comment

by:Guy Hengel [angelIII / a3]
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

by:petiex
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
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 11

Accepted Solution

by:
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

by:petiex
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

by:CEHJ
ID: 19677024
petiex: please see the site guidelines about homework
0

Featured Post

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

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

864 members asked questions and received personalized solutions in the past 7 days.

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

Join & Ask a Question