Results 1 to 3 of 3
Like Tree1Likes
  • 1 Post By MrPosotive

Thread: I need help finding a remainder

  1. #1
    MrPosotive is offline Member
    Join Date
    Sep 2012
    Posts
    68
    Rep Power
    0

    Default I need help finding a remainder

    Hey guys im writing a program for my computer science class and its kicking my ass. The think im having a problem with is making it tell me what the remainder is im using the modulous *correctly i think* but it keeps being weird. Ill show you the code ive wrote so far

    import java.util.Scanner;
    public class Prog72a
    Java Code:
    {
        public static void main(String args[])
        {
            int a,b,c,d;
            int e = 26;
            
            Scanner aa = new Scanner(System.in);
            
            System.out.println("imput the first two numbers");
            
            a = aa.nextInt();
            
            Scanner bb = new Scanner(System.in);
            System.out.println("imput the second two numbers");
            b = bb.nextInt();
            
            Scanner cc = new Scanner(System.in);
            System.out.println("imput the third two numbers");
            c = cc.nextInt();
            
            Scanner dd = new Scanner(System.in);
            System.out.println("imput the fourth two numbers");
            d = dd.nextInt();
            
            
            int f = a + b + c + d % e; 
            
            System.out.println("The remainder is" + f);
            
            
            
        }
    }
    So at the point im at now it should tell me what the remainder is of the 4 two digit numbers i put in divided by 26 but when i do for some reason it will just add the 4 two digit numbers up and tell me what their sum is...unless i do "26 26 26 26" then it tells me the sum of the four 26's but it subtracts 26 so the number it shows me is 78. Please help guys, i really have no clue whats wrong, i think im using the modulous wrong.
    Last edited by MrPosotive; 09-24-2012 at 05:49 AM.

  2. #2
    java.java is offline Member
    Join Date
    Jul 2012
    Posts
    9
    Rep Power
    0

    Default Re: I need help finding a remainder

    You have to follow the rules of operator precedence when you are using arithmetic expressions. Here '%' is having a higher precedence than '+' hence the operation that will be first performed here is modulus and the result will be added with the other variables. Use the expression "(a+b+c+d)%d". This should solve your problem. Let me know if it doesn't

  3. #3
    MrPosotive is offline Member
    Join Date
    Sep 2012
    Posts
    68
    Rep Power
    0

    Default Re: I need help finding a remainder

    Thanks a lot, i feel dumb now.
    java.java likes this.

Similar Threads

  1. How to calculate remainder in JAVA (%)
    By batista11b5 in forum New To Java
    Replies: 25
    Last Post: 10-25-2010, 07:39 AM
  2. finding max value
    By rocky86 in forum New To Java
    Replies: 6
    Last Post: 10-10-2010, 01:37 AM
  3. how to discard remainder on division?
    By RobertF in forum New To Java
    Replies: 9
    Last Post: 03-13-2009, 12:20 PM
  4. [SOLVED] Modulus/Remainder results
    By antgaudi in forum New To Java
    Replies: 9
    Last Post: 10-13-2008, 03:49 AM
  5. BigInteger remainder results in zero
    By perito in forum New To Java
    Replies: 1
    Last Post: 03-21-2008, 04:07 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •