Results 1 to 4 of 4
  1. #1
    perfectjfl is offline Member
    Join Date
    Nov 2014
    Posts
    1
    Rep Power
    0

    Default 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!

  2. #2
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    20,003
    Rep Power
    33

    Default 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; 11-25-2014 at 11:14 PM.
    If you don't understand my response, don't ignore it, ask a question.

  3. #3
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    6,226
    Rep Power
    13

    Default 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,
    Jim
    The JavaTM Tutorials | SSCCE | Java Naming Conventions
    Poor planning on your part does not constitute an emergency on my part

  4. #4
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    14,422
    Blog Entries
    7
    Rep Power
    28

    Default 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; 11-26-2014 at 11:38 AM.
    Build a wall around Donald Trump; I'll pay for it.

Similar Threads

  1. Replies: 16
    Last Post: 02-28-2013, 08:59 PM
  2. Write a program to compute tan (a+b) tan (a-b)
    By bestlita in forum New To Java
    Replies: 10
    Last Post: 07-09-2011, 05:25 AM
  3. Replies: 5
    Last Post: 06-08-2010, 05:22 PM
  4. Illegal Arithmetic Operations?
    By Cruor in forum New To Java
    Replies: 13
    Last Post: 09-19-2008, 05:46 PM
  5. Using Scanner to compute an average of the values
    By Java Tip in forum java.lang
    Replies: 0
    Last Post: 04-17-2008, 08:36 PM

Tags for this Thread

Posting Permissions

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