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 7 of 7

Thread: calculate sum and product

  1. #1
    Junior Member
    Join Date
    Sep 2011
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default calculate sum and product

    The program is supposed to calculate the sum of number 1-5 and calculate the product of number 1-5. This is my code:

    package homeworktest;
     
    /**
     *
     * @author user
     */
    public class HomeworkTest {
     
        /**
         * @param args the command line arguments
         */
        public static void main(String[] args) {
            int count = 0;
            int sum = 0;
            int product = 0;
            do
            {
                count++;
                sum += count;
                product *= count;
                if (count == 5)
                    System.out.println("Sum = " + sum);
                    System.out.println("Product = " + product);
            } while (count < 5);
                    } //end main
     
     
            }
    This is the output I get:
    run:
    Product = 0
    Product = 0
    Product = 0
    Product = 0
    Sum = 15
    Product = 0
    Last edited by helloworld922; September 26th, 2011 at 06:06 PM.


  2. #2
    Administrator copeg's Avatar
    Join Date
    Oct 2009
    Location
    US
    Posts
    5,318
    Thanks
    181
    Thanked 833 Times in 772 Posts
    Blog Entries
    5

    Default Re: calculate sum and product

    For future reference, please wrap your code in the [highlight][/highlight] tags.

    I suggest writing down the math on a piece of paper, going through the algorithm the code executes a step at a time. As hint, 0 time anything will still be zero.

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

    Default Re: calculate sum and product

    Ugh, sorry, I still don't get it.

  4. #4
    Super Moderator helloworld922's Avatar
    Join Date
    Jun 2009
    Posts
    2,895
    Thanks
    23
    Thanked 619 Times in 561 Posts
    Blog Entries
    18

    Default Re: calculate sum and product

    In your code:
    int product = 0;
    And later on:
    product *= count;

    0*1 = ?
    0*2 = ?
    0*3 = ?
    ...

  5. #5
    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: calculate sum and product

    I hope you already have the answer now. No need to explain here though any better than helloworld922 and copeg.

  6. #6
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,145
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: calculate sum and product

    One other thing about your coding style, indenting a statement does NOT make it be inside of the if statement's block. If you look at the printout from your code you will see a repeated printout. Did you expect that?
    You should ALWAYS use {}s to delimit the statements following if, else, while etc.
    90% of the time that you don't, it causes an error when you try to add another statement.

  7. #7

    Default Re: calculate sum and product

    A tip:
    Whenever initializing a variable to keep track of a product, initialize it to the first entry or factor.

    //do not do this
    int product = 0;
    product *= intArray[0];
    product *= intArray[1];
    product *= intArray[2];
    ...
     
    //do this
    int product = intArray[0];
    //or
    int product = 1;

    Also:
    if (count == 5)
                    System.out.println("Sum = " + sum);
                    System.out.println("Product = " + product);
    Java allows syntax for using the if statement implicitly with no braces, but only one statement following the if clause is counted in the implied "then" clause. If you use braces, you are being explicit and explicit is better.
    Kenneth Walter
    Software Developer
    http://kennywalter.com

Similar Threads

  1. Cannot calculate. Please help me....
    By safarina02 in forum What's Wrong With My Code?
    Replies: 2
    Last Post: September 15th, 2011, 01:11 PM
  2. The sum and product of a positive number between 1000 and 9999.
    By metaleddie13 in forum Member Introductions
    Replies: 1
    Last Post: September 15th, 2011, 04:39 AM
  3. Calculate Button Not Working
    By czerenborg in forum AWT / Java Swing
    Replies: 8
    Last Post: September 8th, 2011, 09:25 PM
  4. GUI - calculate BMI (need help asap)
    By jahead in forum What's Wrong With My Code?
    Replies: 1
    Last Post: April 18th, 2011, 04:10 AM
  5. Calculate primes help
    By TommyFiz in forum What's Wrong With My Code?
    Replies: 3
    Last Post: October 27th, 2009, 11:41 PM