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

Problem with C# ... total n00b question!

Options
  • 23-03-2006 9:06pm
    #1
    Registered Users Posts: 10,210 ✭✭✭✭


    Evening gents,
    I'm doing an assignment, and have pretty much 75% of it done! Basicly, I have to input 2 numbers, and do various things with those 2 numbers.

    I have to 1) Multiply the numbers 2) list the numbers between the numbers 3) display all the odd number 4) display the SUM of the numbers between the numbers

    Now I managed 1,2,3 through trial and error and managed to get it eventually (go me!) but I am totally stuck on 4... Has anyone got any idea how id even go about doing this? I'm not asking for someone to do it, but rather a helping hand, or a hint. I'm sure im just missing something simple :(


    An example of the 3rd part is below which I managed to get ok... Just so you guys know how im getting on within the new world of programming!

    static void DisplayOddNumbers ()

    {
    while (firstNumber <= secondNumber)
    {

    int rem = (firstNumber % 2);
    if ((rem>0)&&(rem<2))
    Console.WriteLine("{0}",firstNumber);
    firstNumber ++;

    }


Comments

  • Closed Accounts Posts: 2,028 ✭✭✭oq4v3ht0u76kf2


    Surely if you can do part 2 then a bit of modifying that code will add the numbers as well as list them?


  • Registered Users Posts: 10,210 ✭✭✭✭JohnCleary


    Your probably right, like I said, im just missing something simple, but I cant get my head round it :(

    I've been trying all day lol


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


    You're already able to find all the numbers in between the start and the end. The sum is the total of all those numbers.

    Can you see where you're going wrong now?


  • Closed Accounts Posts: 4,943 ✭✭✭Mutant_Fruit


    This
    static void DisplayOddNumbers ()
    
    {
    while (firstNumber <= secondNumber)
    {
    
    int rem = (firstNumber % 2);
    [B]if (rem==1)[/B]
    Console.WriteLine("{0}",firstNumber);
    firstNumber ++;
    
    }
    

    or alternatively this:
    while (firstNumber <= secondNumber)
    {
    
    int rem = ();
    [B]if ((firstNumber % 2) == 1)[/B]
    Console.WriteLine("{0}",firstNumber);
    firstNumber ++;
    
    }
    
    is a bit shorter and more concise.

    As for "summing" up the numbers, you have to hard work done. You are already looping through all the numbers between the initial start and end number... so you just have to add them up as you loop through the while loop...


  • Registered Users Posts: 10,210 ✭✭✭✭JohnCleary




    As for "summing" up the numbers, you have to hard work done. You are already looping through all the numbers between the initial start and end number... so you just have to add them up as you loop through the while loop...

    The problem is I dont know how to ask it to add them up :(


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


    Add them while looping through the set.


Advertisement