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: Adding odd numbers from 1 to 15

  1. #1
    Junior Member
    Join Date
    Jan 2013
    Posts
    4
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Adding odd numbers from 1 to 15

    I have an exercise that wants me to write an application that calculates the product of the odd integers from 1 to 15, and displays the result.
    Hereunder is the code that i have written, however I am not managing to count the odd numbers. Can someone help me please.
    public static void main(String[] args) {
            int prod = 0;
            int result = 0;
            int div = 0;
     
            for ( int i = 0; i != 16; i++ ) {
     
                if (( i % 2 ) != 0)  {
     
                 prod = ( i * i );
                 result = (prod + prod);
     
                System.out.println(prod);
     
                }  
                                            }
        }   
    }
    Last edited by helloworld922; January 8th, 2013 at 01:52 PM. Reason: please use [code] tags


  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: Adding odd numbers from 1 to 15

    however I am not managing to count the odd numbers.
    Can you show what the program prints out and add some comments explaining what is wrong with what is printed and show what you want to be printed?

    What is the variable: result used for?

    Can you write an equation that shows how to get the product of the first 4 odd numbers?
    If you don't understand my answer, don't ignore it, ask a question.

  3. #3
    Junior Member
    Join Date
    Jan 2013
    Posts
    5
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Adding odd numbers from 1 to 15

    did the exercise myself for practice
    how do i wrap this in java code?


    ok, so I read your code, and I think you should think it out on paper before even writing the code. Figure out how you wanna do it step by step then try to translate that into code, cause your code seems really off target.

    so your code begins when i iterates to 1. Once i = 1, since 1%2 != 0, you do product = 1*1, after that you say result = 1 + 1 which is 2, then you print out product which is 1. So if you're not going to use result, what's the point of having it there in the first place?


    >>>>>> Code removed
    Last edited by Norm; January 9th, 2013 at 07:22 AM. Reason: removed spoonfed code

  4. #4
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: Adding odd numbers from 1 to 15

    http://www.javaprogrammingforums.com...n-feeding.html
    If you don't understand my answer, don't ignore it, ask a question.

Similar Threads

  1. Odd and Even Numbers
    By tyb97 in forum Algorithms & Recursion
    Replies: 4
    Last Post: September 30th, 2012, 04:19 PM
  2. Counting the amount of zeroes, odd and even numbers in an integer
    By 54stickers in forum What's Wrong With My Code?
    Replies: 2
    Last Post: December 4th, 2011, 07:17 PM
  3. sum of arrays and printing even and odd numbers in the array
    By senecawolf in forum Collections and Generics
    Replies: 3
    Last Post: November 8th, 2011, 03:07 PM
  4. adding up odd and even numbers
    By darlinho in forum What's Wrong With My Code?
    Replies: 10
    Last Post: September 30th, 2010, 03:28 PM
  5. Odd and Even Numbers Logics in FOR and IF Loop methods
    By mparthiban in forum Loops & Control Statements
    Replies: 2
    Last Post: May 12th, 2010, 05:19 AM

Tags for this Thread