Results 1 to 3 of 3
Thread: I need help finding a remainder
- 09-24-2012, 05:38 AM #1
Member
- Join Date
- Sep 2012
- Posts
- 68
- Rep Power
- 0
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
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.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); } }Last edited by MrPosotive; 09-24-2012 at 05:49 AM.
- 09-24-2012, 05:53 AM #2
Member
- Join Date
- Jul 2012
- Posts
- 9
- Rep Power
- 0
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
- 09-24-2012, 06:01 AM #3
Member
- Join Date
- Sep 2012
- Posts
- 68
- Rep Power
- 0
Similar Threads
-
How to calculate remainder in JAVA (%)
By batista11b5 in forum New To JavaReplies: 25Last Post: 10-25-2010, 07:39 AM -
finding max value
By rocky86 in forum New To JavaReplies: 6Last Post: 10-10-2010, 01:37 AM -
how to discard remainder on division?
By RobertF in forum New To JavaReplies: 9Last Post: 03-13-2009, 12:20 PM -
[SOLVED] Modulus/Remainder results
By antgaudi in forum New To JavaReplies: 9Last Post: 10-13-2008, 03:49 AM -
BigInteger remainder results in zero
By perito in forum New To JavaReplies: 1Last Post: 03-21-2008, 04:07 PM


1Likes
LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks