How to solve this without using parentheses?

Related searches

This is an exercise from CodeGym.

Exercise is this:

In the main method, place plus and minus signs correctly so that the variable result is equal to 20. Signs must be placed only in the line where the variable result is declared.

Do not change the order of the variables in this line. Each variable must be preceded by either a plus or minus sign.

Requirements:

1. Values ​​of variables: Do not change a, b, c, or d.

2. Each of the variables (a, b, c, and d) in the line where the variable result is declared must be preceded by either a plus or minus sign.

3. The program should display the number 20 on the screen.

4. The plus and minus signs must be placed correctly.

I tried to use Math.abs() to return positive number 20, but it throws an error.

I've Tried to add some variable, still throws an error.

Also, Tried to use parentheses, still same problem.

    package com.codegym.task.task01.task0137;

    /* 
    Only 20 will do

    */

    public class Solution {
        public static int a = 1;
        public static int b = 3;
        public static int c = 9;
        public static int d = 27;


        public static void main(String[] args) 
   {

            int result = + a - b + c - d;

            System.out.println(result);
        }
    }

Thanks for all! I didn't try int result = - a + b - c + d; which several of you suggested :) It was correct))

This is more of a math problem, rather than a Java/programming skill related problem.

[This question could be made a programming problem ( more interesting :D ) by making the four variables and the final output dynamic by passing those as input parameters]

The logical steps I took to solve the problem as below

  1. given numbers are 1,3,9,27 and output should be 20, this means 27 cannot be negative in any case. so assigning + sign to 27.

  2. take next number 9, it can only take '-' sign, otherwise the total sum will be more than 20 no matter what the signs of remaining numbers

  3. now the result of above two steps will give 27-9 = 18. To achieve overall 20, now give + sign to 3 and - sign to 1

public class Solution {
    public static int a = 1;
    public static int b = 3;
    public static int c = 9;
    public static int d = 27;

    public static void main(String[] args) {
        int result = - a + b - c + d;
        System.out.println(result);
    }
}

Order of Operations - Part 1 - No Parenthesis, order of operations without parenthesis (that video is coming soon). Your browser does not Duration: 3:48 Posted: Aug 24, 2015 Continue working inside the parentheses by evaluating the division 36 ÷ 9: = 1 + (3 – 4) · 2 2. Now you can get rid of the parentheses altogether: = 1 + –1 · 2 2. At this point, what’s left is an expression with an exponent. This expression takes three steps, starting with the exponent: = 1 + –1 · 4. = 1 + –4.

This is math, and the result should be 27 - 9 + 3 - 1, and you can use the unary negative to make -1. That is,

int result = -a + b - c + d;

Evaluating expressions with and without parentheses, So maybe parentheses don't seem like a big deal? Check out this problem and the difference in Duration: 2:06 Posted: May 26, 2015 Without the parentheses the multiplication is done first: 3 + 2 × 6 − 4 = 3 + 12 − 4 = 11 (not 10) With more complicated grouping it is good to use different

Try doing this.

package com.codegym.task.task01.task0137;

public class Solution 
{
    public static int a = 1;
    public static int b = 3;
    public static int c = 9;
    public static int d = 27;


    public static void main(String[] args) 
    {

        int result = d - c + b - a;

        System.out.println(result);
    }
}

Order of Operations without Parentheses, If given a problem with no parenthesis to indicate the order of operations you're suppose to do * and / first, then + and -. But how do you solve a� Step 1: First, perform the operations within the parenthesis. Step 2: Then, perform multiplication and division from left to right. Step 3: Next, perform addition and subtraction from left to right. Example: Calculate 9 × (12 – 2) ÷ 5 + 1 = Solution: 9 × (12 – 2) ÷ 5 + 1 (perform within parenthesis) = 9 × 10 ÷ 5 + 1(perform multiplication)

If you are required not to change the order of the variables, then something like this:

package com.codegym.task.task01.task0137;

public class Solution 
{
    public static int a = 1;
    public static int b = 3;
    public static int c = 9;
    public static int d = 27;


    public static void main(String[] args) 
    {

        int result = -a + b - c + d;

        System.out.println(result);
    }
}

How to solve this without using parentheses?, This is more of a math problem, rather than a Java/programming skill related problem. [This question could be made a programming problem� Order of Operations without Parentheses. Date: 8/3/96 at 10:15:1From: Ken DunhamSubject: Order of Operations for Unstructured Problem If given a problem with no parenthesis to indicate the order of operations you're suppose to do * and / first, then + and -. But how do you solve a problem like:21+82-59+29*86*66/50*7/87The first part of the problem is 44 - rewritten as:44+29*86*66/50*7/87But how do you deal with all the multiplication and extra division symbols?

Simplifying with Parentheses: An Introduction, Provides worked examples explaining and illustrating how to multiply through parethetical expressions, including how to deal with "minus" signs. Using Parentheses ( ) Parentheses are used to group numbers or variables, or both. When you see a math problem containing parentheses, you need to use the order of operations to solve it. For example, take the problem: 9 - 5 ÷ (8 - 3) x 2 + 6

Go through the operations in order and check for each. This doesn’t contain parentheses or exponents, so move onto the multiplication and division. First, 6 × 2 = 12, and 6 ÷ 2 = 3, and these can be inserted to leave an easy problem to solve: 4 + 12 − 3 = 13. This example includes more operations: (7 + 3) 2 – 9 × 11.

If you have not taught this step-by step-method of solving order of operations problems, you might be tempted to skip it and let your students use mental math. Most of the problems are so easy that your students may be able to solve them without writing out each step.

Solve a basic linear algebraic equation. A linear algebraic equation is nice and simple, containing only constants and variables to the first degree (no exponents or fancy stuff). To solve it, simply use multiplication, division, addition, and subtraction when necessary to isolate the variable and solve for "x".

Comments
  • (d+b)-(a+c) it will give result as 20
  • @MonisMajeed I believe he said without using paranthesis in the title...