Thread: Argh! Need help with program to get sum of integers between m and n inclusively

  1. #1
    Registered User
    Join Date
    Mar 2002
    Posts
    11

    Argh! Need help with program to get sum of integers between m and n inclusively

    The program will print the sum of the integers between m and n inclusive, where m and n are user input values.

    After writing the pseudocode, I went to work and have this as part of my source code:

    while (m!=n) {
    if (m>n) {
    ++n;
    }
    else {
    ++m;
    }
    }

    I can't figure out how to do the rest of this problem and it's been driving me crazy! Any help would be greatly appreciated.

    Colin

  2. #2
    Mayor of Awesometown Govtcheez's Avatar
    Join Date
    Aug 2001
    Location
    MI
    Posts
    8,823
    int total = 0;

    while (m<=n) {
    if (m>n) {
    total += n;
    n++;
    }
    else {
    total += m;
    m++;
    }
    }

  3. #3
    The Artful Lurker Deckard's Avatar
    Join Date
    Jan 2002
    Posts
    633
    Where a is the starting value, b is the ending value, and total is the sum of the count:
    Code:
    for ( total = 0; a <= b; a++ )
      total += a;
    Jason Deckard

  4. #4
    Registered User
    Join Date
    Mar 2002
    Posts
    11

    Thanks!

    I've got it working, but I have one question. What is the += mean? I haven't seen that one yet.

    Colin

  5. #5
    Mayor of Awesometown Govtcheez's Avatar
    Join Date
    Aug 2001
    Location
    MI
    Posts
    8,823
    i += 4

    is the same thing as

    i = i + 4

  6. #6
    Registered User
    Join Date
    Mar 2002
    Posts
    11
    Thanks again.

Popular pages Recent additions subscribe to a feed