Welcome to the Java Programming Forums

The professional, friendly Java community. 21,500 members and growing!

The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.

>> REGISTER NOW TO START POSTING

# Thread: Need some help with this code - Asap

1. ## Need some help with this code - Asap

```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?  Reply With Quote

3. ## Re: Need some help with this code - Asap

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?  Reply With Quote

4. ## 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);"  Reply With Quote

5. ## Re: Need some help with this code - Asap Originally Posted by Th3T3chGuy ```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.  Reply With Quote