Advertisement
If you have a new account but are having problems posting or verifying your account, please email us on hello@boards.ie for help. Thanks :)
Hello all! Please ensure that you are posting a new thread or question in the appropriate forum. The Feedback forum is overwhelmed with questions that are having to be moved elsewhere. If you need help to verify your account contact hello@boards.ie

Need help before 10pm

Options
  • 22-11-2004 6:54pm
    #1
    Registered Users Posts: 2,601 ✭✭✭


    I'm doing Java in college and i'm stuck on a question that is asking me to Write a program (using a loop) to calculate the product of the even numbers between 1 and 20. Print the result.

    I'm stuck and dont know what to do! Help please!! :)


Comments

  • Registered Users Posts: 17,727 ✭✭✭✭Sherifu


    int result=0;

    for(int i=0;i<21;i+=2){

    result+=i;

    }

    //edit forgot to print result

    javax.swing.JOptionPane.showMessageDialog(null,result);

    RKM.


  • Closed Accounts Posts: 17,208 ✭✭✭✭aidan_walsh


    You need to answer these questions to do it.

    1) What would th ebest looping structure be?
    2) How would I determine if a number is even or not?
    3) How would I determine their product?

    Try to answer these and the program will practically write itself.


  • Registered Users Posts: 396 ✭✭ai ing


    rmulryan wrote:
    int result=0;

    for(int i=0;i<21;i+=2){

    result+=i;

    }

    //edit forgot to print result

    javax.swing.JOptionPane.showMessageDialog(null,result);

    RKM.

    That is just adding the even numbers. you need to get the product as below

    int result=1;

    for(int i=2;i<=20;i+=2){

    result*=i;


  • Registered Users Posts: 17,727 ✭✭✭✭Sherifu


    that is true, but i'd get most of the marks anyway. :cool:


  • Registered Users Posts: 2,601 ✭✭✭MidnightQueen


    Thanks alot guys!! Very much appreciated!! ;) Rep for all tomorrow cos i'm all outa rep 2day. :(


  • Advertisement
  • Registered Users Posts: 2,781 ✭✭✭amen


    I always though the point of this board was to provide help to someone who is stuck on a particular piece of code or requires advice on something not do some ones college/homework assignment when there has been no input/effore made by the poster


  • Closed Accounts Posts: 17,208 ✭✭✭✭aidan_walsh


    It is, hence my response... You learn nothing by having all the answers handed to you, always better to be just pointed in the right direction and figure the rest out yourself.


  • Registered Users Posts: 16,413 ✭✭✭✭Trojan


    Guys, KK in particular, please read the forum charter. It's fine to ask for help here but you should make at least some token effort yourself.


This discussion has been closed.
Advertisement