LinuxQuestions.org
Share your knowledge at the LQ Wiki.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 03-03-2004, 04:29 PM   #1
craddy
LQ Newbie
 
Registered: Sep 2003
Posts: 20

Rep: Reputation: 0
java + maths problem


Hello,

I have an assignment in which I have to write a java programme to add together the numbers in a positive integer. For example if the number 4567 were entered the output would be:

4 + 5 + 6 + 7 = 22

I have been told to use the modulus function aswell as division, but I have no clue!

Any info would be greatly appreciated!

Craddy
 
Old 03-03-2004, 04:53 PM   #2
maniac01
LQ Newbie
 
Registered: Mar 2004
Posts: 11

Rep: Reputation: 0
I will give you a hint

4567 mod 10 = 7
 
Old 03-04-2004, 02:23 PM   #3
[WebCarnage]
Member
 
Registered: Jun 2003
Location: USA
Distribution: Slackware 9.1 | netBSD 1.6.2
Posts: 35

Rep: Reputation: 15
Quote:
Originally posted by: craddy
I have been told to use the modulus function aswell as division, but I have no clue!
Remember in elementary when you were first taught long division? Think of modulus as something like that.


Code:
Example: modulus1

    ___4_   Remainder 1
3   | 13
      12
       1

That division problem has a remainder of 1, because 4 goes into 13 evenly only 3 times, and it ends up with a decimal (.25) or a remainder of 1.

Ergo, 4567 % 10 = 7, as maniac01 told you. The answer is 7 because:

Code:
Example: modulus2

     ___456___   Remainder 7
10   | 4567
       40
        5
        56
        50
         6
         67
         60
          7
Hope this helped. Moduluses are quite simple to code, ontop of that. For instance.

Code:
Example: modulus3

public class modulusTest {
    public static void main(String[] args) {
        int mod = 4567 % 10;
        System.out.println(mod);

        //System.out.println(4567 % 10);
     }
}

Last edited by [WebCarnage]; 03-04-2004 at 02:31 PM.
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Programming maths in Python DiagonalArg Programming 4 08-05-2005 10:33 AM
childrens learning ie maths master Linux - General 2 07-09-2005 02:43 PM
please help me do maths :) darkRoom General 36 04-11-2005 01:03 PM
problem with maths redduck666 General 7 02-12-2005 02:33 PM
And some maths :-P Mathiasdm General 7 11-11-2004 03:48 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 11:02 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration