Need some help with this code - Asap

Code :

import java.util.Scanner;
public class Text1{
public static void main(String [] args){
Scanner input = new Scanner(System.in);
int answer = 1;
int number = 0;
int x;
System.out.print("Enter number: ");
number = input.nextInt();
System.out.print("Enter value of x: ");
x = input.nextInt();
for (int i=1; i<=number;i++)
if (answer += i * x){
System.out.println(i);
}
}
}

The task is to write a program to find the sum of a number of terms of the infinite series 1 + x + x2 + x3 + x4 + .... xn where the number of terms n to be evaluated and the value of x are input before the summation begins.

Note: x0 = 1 AND x1 = x are known mathematical values

What do you guys think?

Re: Need some help with this code - Asap

answer = answer + i*x.

x = 5;

number = 10;

answer = 1 + (1 *5)

answer = 6 Good

i = 2;

answer = 6 + (2*5) = 16 Not good

answer = 16 + (3*5) = 31 Not right (though it would work for 1 + 5 + 5^2)

1 + 5 + 25 = 31

i = 4;

answer = 31 + (4*5) = 51.

i = 5;

answer = 51 + 25 = 76. Not Good

i = 6

answer = 76 + 30 = 106 Not good

i = 7

answer = 106 + 35 = 141 not good

i = 8

answer = 141 + 40 = 181 not good

i = 9

answer = 181 + 45 = 226 not good

i = 10

answer = 226 + 50 = 276 not good

Also, what does it print out if somebody enters in 0 as the number?

Re: Need some help with this code - Asap

This "if (answer += i * x)" is not correct because the result of the expression is not a boolean.

Instead of this statement you can put "answer += Math.pow(x, i);"

Re: Need some help with this code - Asap

Quote:

Originally Posted by

**Th3T3chGuy**
Code :

import java.util.Scanner;
public class Text1{
public static void main(String [] args){
Scanner input = new Scanner(System.in);
int answer = 1;
int number = 0;
int x;
System.out.print("Enter number: ");
number = input.nextInt();
System.out.print("Enter value of x: ");
x = input.nextInt();
for (int i=1; i<=number;i++)
if (answer += i * x){
System.out.println(i);
}
}
}

The task is to write a program to find the sum of a number of terms of the infinite series 1 + x + x2 + x3 + x4 + .... xn where the number of terms n to be evaluated and the value of x are input before the summation begins.

Note: x0 = 1 AND x1 = x are known mathematical values

What do you guys think?

And...... ?

What is the problem in code?

What are the errors?

What is the problem, you are facing?

Be precise and get precise answers.