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.


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

Results 1 to 3 of 3

Thread: Sum of series program logical error

  1. #1
    Junior Member
    Join Date
    Aug 2011
    Thanked 0 Times in 0 Posts

    Question Sum of series program logical error

    This program is supposed to calculate:
    S= 2! -4! +6! -8! ..n terms

    public class Series7
    int sum, i, term, j, prod;
    public void main(int n)
    sum = 0;
    prod = 1;
    for (i=1;i<=n;i++)
    prod *= j;

    term += 2;
    sum -= prod;
    sum += prod;

    System.out.println("The sum is "+sum);

  2. #2
    Crazy Cat Lady KevinWorkman's Avatar
    Join Date
    Oct 2010
    Washington, DC
    My Mood
    Thanked 632 Times in 540 Posts

    Default Re: Sum of series program logical error

    What does it do instead? Do you have a question?
    Useful links: How to Ask Questions the Smart Way | Use Code Tags | Java Tutorials
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  3. #3
    Join Date
    Aug 2009
    Thanked 3 Times in 2 Posts

    Default Re: Sum of series program logical error

    You have a ton of problems with syntax but that aside your logic problems stems from messy seperation of what each loop does.

    Outer loop: adds 2 to the term, adds the fac result to the output sum.
    Inner loop: Fac'ing the term.

    Im not sure you are fac'ing right. Try using recursion as it brings beautiful solution to fac'ing.

    	public void myFunc(int n){
    		int sum = 0;
    		int term = 2;
    		for (int i = 1; i <= n ; i++){
    			if(i % 2 == 0)
    				sum -= myFac(term);
    			} else {
    				sum += myFac(term);
    			term += 2;

    Remember to use highlight tags, or you code is unreadable
    Last edited by Johannes; August 12th, 2011 at 11:36 AM.

Similar Threads

  1. Logical Operators
    By truebluecougarman in forum Java Theory & Questions
    Replies: 3
    Last Post: January 22nd, 2011, 05:26 PM
  2. While (logical confusing output)
    By chronoz13 in forum Loops & Control Statements
    Replies: 4
    Last Post: December 20th, 2009, 12:17 AM
  3. Replies: 1
    Last Post: October 19th, 2009, 11:53 PM
  4. [SOLVED] displaying sum of harmonic series
    By sriraj.kundan in forum Algorithms & Recursion
    Replies: 2
    Last Post: June 15th, 2009, 11:42 PM
  5. Proper output for Non preemptive scheduling problem
    By haygaurav in forum What's Wrong With My Code?
    Replies: 0
    Last Post: March 4th, 2009, 06:58 AM

Tags for this Thread