I get the error, "The left-hand side of an assignment must be a variable" for this line, "(addition + n) = answer." I am trying to prompt for n and add 1 to it, then two, then three, and so on until all numbers up to n are added.
Code:
// A program to prompt for n, and then
// add all the numbers from 1 to n.
// The program should also calculate
// n(n+1)/2 and print both values.
package loops;
import java.util.Scanner;
public class Addition
{
static double slow(double n){
double addition = 1; // number to add to n and increases by one each time until above n
double answer; // answer
//loop condition
while (addition <= n) {
return ((addition + n) = answer);
}
}
static double fast(double n){
return (n * (n + 1) / 2);
}
public static void main(String [] args) {
Scanner sc = new Scanner (System.in);
int n;
//user input
System.out.print("n = ");
n = sc.nextInt();
System.out.println("Slow way answer is: " + slow(n));
System.out.println("Fast way answer is: " + fast(n));
}
}