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

This is a discussion on Argh! Need help with program to get sum of integers between m and n inclusively within the C Programming forums, part of the General Programming Boards category; The program will print the sum of the integers between m and n inclusive, where m and n are user ...

  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,825
    int total = 0;

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

  3. #3
    B26354 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,825
    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

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21