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


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 4 of 4

Thread: Need some help with this code - Asap

  1. #1
    Junior Member
    Join Date
    Nov 2011
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default 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?


  2. #2
    Banned
    Join Date
    May 2010
    Location
    North Central Illinois
    Posts
    1,631
    My Mood
    Sleepy
    Thanks
    390
    Thanked 112 Times in 110 Posts

    Default 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?

  3. #3
    Junior Member
    Join Date
    Nov 2011
    Posts
    5
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default 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);"

  4. #4
    Think of me.... Mr.777's Avatar
    Join Date
    Mar 2011
    Location
    Pakistan
    Posts
    1,136
    My Mood
    Grumpy
    Thanks
    20
    Thanked 82 Times in 78 Posts
    Blog Entries
    1

    Default Re: Need some help with this code - Asap

    Quote Originally Posted by Th3T3chGuy View Post
    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.

Similar Threads

  1. help needed ASAP
    By minaa07 in forum AWT / Java Swing
    Replies: 1
    Last Post: October 4th, 2011, 09:08 AM
  2. Need help ASAP!!!!
    By Swiper in forum What's Wrong With My Code?
    Replies: 9
    Last Post: August 12th, 2011, 06:41 PM
  3. NEED HELP ASAP!!
    By JavaStudent_09 in forum What's Wrong With My Code?
    Replies: 7
    Last Post: August 18th, 2010, 07:33 PM
  4. NEED HELP ASAP PLEASE!!!
    By mbm4ever in forum What's Wrong With My Code?
    Replies: 8
    Last Post: August 12th, 2010, 07:01 PM
  5. Need a calandar app done asap
    By gixmo in forum Paid Java Projects
    Replies: 6
    Last Post: July 10th, 2010, 08:58 PM