Question

I'm trying to write a loop in Java that can output the sum of a series which has this form... 1! -3! + 5! – 7! + ... up to n (user gives n as a positive odd number). For example, if the user inputs 5 for n, then the series should calculate the sum of 1! -3! + 5! (hard part) & display it to the user with a basic print statement (easy part). If the user gives 9, then the sum calculated would come from 1! -3! + 5! - 7! + 9!.

For ease, just assume the user always puts in a positive odd number at any time. I'm just concerned about trying to make a sum using a loop for now.

The closest code I've come up with to do this...

int counter = 1;
int prod = 1;   
n = console.nextInt(); 
while (counter <= n) 
{                  
    prod = prod * counter;
    counter++; 
}   
System.out.println(prod);

This does n!, but I'm finding it hard to get it do as specified. Any pointers would be great.

Was it helpful?

Solution 2

First of all, you need to introduce a variable int sum = 0; to store the value of the alternate series.

To only sum every second value, you should skip every second value. You can check that using the modulo operation, e.g. if( counter % 2 == 1 ).

If that is true, you can add/subtract the current value of prod to the sum.

To get the alternating part, you can use a boolean positive = true; like this:

if( positive ) {
    sum += prod;
} else {
    sum -= prod;
}
positive = !positive;

Based on the boolean, the prod is either added or subtracted. The value is altered afterwards.

Because factorials become very large very fast, it would be better to use variables of type long.

OTHER TIPS

As you calculate the factorials, keep a running total of the series so far. Whenever counter % 4 == 1, add the factorial to the running total. Whenever counter % 4 == 3, subtract the factorial from the running total.

You said "any pointers" - I assume that means you don't want me to write the code for you.

Update

This is closely based on your original code, so that it would be as easy as possible for you to understand. I have changed the bare minimum that I needed to change, to get this working.

int counter = 1;
long prod = 1;   
long total = 0;

n = console.nextInt(); 

while (counter <= n) 
{                  
    prod = prod * counter;
    if( counter % 4 == 1 ) {
        total += prod;
    } else if (counter % 4 == 3) {
        total -= prod;
    }

    counter++; 
}   

System.out.println(total);

First up, notice that I have changed prod to a long. That's because factorials get very big very fast. It would be even better to use a BigInteger, but I'm guessing you haven't learnt about these yet.

Now, there are those two conditions in there, for when to add prod to the total, and when to subtract prod from the total. These both work by checking the remainder when counter is divided by 4 - in other words, checking which factorial we're up to, and doing the right operation accordingly.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top