I'm trying to create a program that generates a series and asks the user to enter the last number in it.
I wrote it but the output should be something similar to
The Sum= -2+4-6+8-10+12-14+16-18+20....
And my output is
-20-18-16-14-12-10-8-6-4-2
So, as you can see, I have three mistakes which are
- Their sequence(should start from 2 not vice versa)
- The signs, it should be one time - and another + but I have no clue how
- Writing sum once at the beginning of the series :(
Here's my code
import java.util.*;
public class Even{
public static void main (String [] args){
Scanner myScanner= new Scanner(System.in);
System.out.println("Enter last number");
int lastNumber= myScanner.nextInt();
if (lastNumber<=1)
{
System.out.print("error");
}
while(lastNumber>1){
System.out.print("-"+lastNumber);
lastNumber-=2;
}
}
}
Thanks in advance!
No comments:
Post a Comment