WiloWisk
WiloWisk

Reputation: 137

How to get a formula of add > subtract > add > vice versa in this output?

enter image description here

We get a total of 47 because of [5 + 23 - 3 + 14 - (-8)]. My question is what formula should I use to get a total of 47 from the example?

This is my code but I always get an answer of -8 for some reason. Any help is much appreciated!

        Scanner input = new Scanner(System.in);

        System.out.print("Enter value of n: ");
        nValue = input.nextInt(); 

        for (int x = 1; x <= nValue; x++){
            System.out.print("Enter number " + x + ": ");
            num1 = input.nextInt();

            num2 = num1 + num1;
            total = num2 - num1;
        }
        System.out.println("Answer: " + total);

Upvotes: 0

Views: 105

Answers (4)

Sia
Sia

Reputation: 86

It appears you are adding an extra step that is resetting instead of adding values. Here is a way to keep adding to the total value and only keeping track of the immediate input.

Updated for any future passer-bys

    Scanner input = new Scanner(System.in);

    System.out.print("Enter value of n: ");
    nValue = input.nextInt(); 
    //variable for total value before loop
    int total = 0
    for (int x = 1; x <= nValue; x++){
        System.out.print("Enter number " + x + ": ");
        num = input.nextInt();
        //add or subtract to total with each iteration (updated logic)
        if (x%2==0){
           total += num;
        }
        else {
           total -= num;
        }
    }
    System.out.println("Answer: " + total);

Upvotes: 0

Dipanshu Khandait
Dipanshu Khandait

Reputation: 26

package com.company;

import java.util.Scanner; public class Main {

public static void main(String[] args) {
    Scanner obj=new Scanner(System.in);
    System.out.println("Enter the value of n");
    int n=obj.nextInt();
    System.out.println("Enter"+1+"Number : ");
    int num=obj.nextInt();
    int sum=num;
    for(int i=2;i<=n;i++){
        System.out.println("Enter"+i+"Number : ");
        num=obj.nextInt();
        if(i%2==0){
            sum=sum+num;
        }
        else{
            sum=sum-num;
        }
    }
    System.out.println("Answer: "+sum);
}

}

Upvotes: 1

Sweeper
Sweeper

Reputation: 274835

To determine whether you should add to, or subtract from the total, you just need to check the value of x.

If x is even or equal to 1, you add to the total. Otherwise you subtract:

    for (int x = 1; x <= nValue; x++){
        System.out.print("Enter number " + x + ": ");
        num1 = input.nextInt();
        if (x % 2 == 0 || x == 1) {
            total += num1;
        } else {
            total -= num1;
        }
    }
    System.out.println("Answer: " + total);

Upvotes: 0

Yvonne
Yvonne

Reputation: 41

Not using loops but this does give the output you're asking for

Scanner input = new Scanner(System.in);
        
        System.out.print("Enter value of n: ");
        int n =input.nextInt();
        System.out.print("Enter " + n + " integers: \n");
        
        Scanner input = new Scanner(System.in);
        
        System.out.print("Enter value of n: ");
        int n =input.nextInt();
        System.out.print("Enter " + n + " integers: \n");
        
        System.out.print("Enter number 1:");
        int num1 = input.nextInt();
        System.out.print("Enter number 2:");
        int num2 = input.nextInt();
        System.out.print("Enter number 3:");
        int num3 = input.nextInt();
        System.out.print("Enter number 4:");
        int num4 = input.nextInt();
        System.out.print("Enter number 5:");
        int num5 = input.nextInt();
        
        int a = num1 + num2;
        int b = a - num3;
        int c = b + num4;
        int d = c - num5;
        
        System.out.print("Answer: " + d);

Upvotes: 0

Related Questions