Results 1 to 4 of 4
 11252014, 07:30 PM #1Member
 Join Date
 Nov 2014
 Posts
 1
 Rep Power
 0
How do I compute all the possible arithmetic operations of 4 integers?
Hey guys, I'm new to Java and I want to write a program that compute all the possible arithmetic operations of 4 integers.
For example, if the user enter 1, 2, 3, and 4 respectively, then I want the computer to print out the following results:
1 + 2 + 3 + 4
1 + 2 + 3  4
1 + 2 + 3 * 4
1 + 2 + 3 / 4
...
1 / 2 / 3 / 4
(parenthesis can be ignored for this program)
This is what I have so far, but I don't know how to continue:
import java.util.Scanner;
public class ArithmeticComputation{
public static void main(String[] args) {
Scanner stdin = new Scanner(System.in);
int num1 = stdin.nextInt();
int num2 = stdin.nextInt();
int num3 = stdin.nextInt();
int num4 = stdin.nextInt();
char[] operation = { '+', '', '*', '/' };
for (int i = 0; i < operation.length; i++) {
for (int j = 0; j < operation.length; j++) {
for (int k = 0; k < operation.length; k++) {
I'm looking for a method that allows me to assign char into actual operators, so that I can loop through all the computation.
I'd really appreciate it if anyone can help me out!
 11252014, 07:41 PM #2
Re: How do I compute all the possible arithmetic operations of 4 integers?
For the first pass on finding a solution, just generate Strings for all the expressions and print them out.
When that works, look at creating an expression evaluator that will parse the String and evaluate it.Last edited by Norm; 11252014 at 11:14 PM.
If you don't understand my response, don't ignore it, ask a question.
 11252014, 09:33 PM #3Senior Member
 Join Date
 Jan 2013
 Location
 Northern Virginia, United States
 Posts
 6,226
 Rep Power
 13
Re: How do I compute all the possible arithmetic operations of 4 integers?
And in some cases, you will need to change the order of the numbers (e.g. 2/3 != 3/2). So generating permutations would be helpful.
Regards,
JimThe Java^{TM} Tutorials  SSCCE  Java Naming Conventions
Poor planning on your part does not constitute an emergency on my part
 11262014, 11:35 AM #4
 Join Date
 Sep 2008
 Location
 Voorschoten, the Netherlands
 Posts
 14,422
 Blog Entries
 7
 Rep Power
 28
Re: How do I compute all the possible arithmetic operations of 4 integers?
Do you want to allow parentheses as well? e.g. (1+2)*(3+4)
kind regards,
Jos
edit: duh, I missed your remark about those parentheses; an easy solution would be to transform your expressions to postfix form (they don't need parentheses and you can evaluate a b op1 c op2 d op3)Last edited by JosAH; 11262014 at 11:38 AM.
Build a wall around Donald Trump; I'll pay for it.
Similar Threads

Using method to compute the sum of n numbers, not getting the return value
By abi in forum New To JavaReplies: 16Last Post: 02282013, 08:59 PM 
Write a program to compute tan (a+b) tan (ab)
By bestlita in forum New To JavaReplies: 10Last Post: 07092011, 05:25 AM 
How to compute or know the correct value for Xmx and xms?
By icesnake in forum New To JavaReplies: 5Last Post: 06082010, 05:22 PM 
Illegal Arithmetic Operations?
By Cruor in forum New To JavaReplies: 13Last Post: 09192008, 05:46 PM 
Using Scanner to compute an average of the values
By Java Tip in forum java.langReplies: 0Last Post: 04172008, 08:36 PM
Bookmarks