Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C / C++ / MFC
  4. Finding the sum of all intergers between x & y

Finding the sum of all intergers between x & y

Scheduled Pinned Locked Moved C / C++ / MFC
c++tutoriallearning
5 Posts 4 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • P Offline
    P Offline
    pam1288
    wrote on last edited by
    #1

    I am just learning how to use C++. I'm trying to create a simple program to do the following: A) allow the user to enter 2 integer values, B) determine the sum of all values between x & y, C) to show the product of x and y. My code for steps A & C is working. My code for step B is not, I think I'm making it much more complicated then is required. Here is the sample code: /* Write a program to find the sum of the integers between x & y. You are then to find the product*/ #include using namespace std; int main () { int x, y, z, s, p, h, sum; s = 0; sum = 0; cout << "Please enter two integers" << endl; cin >> x >> y; p = x; //p is a place holder for the value x z = x*y; // calculate the product while (s <= y) { s = ++ x; // increment x by 1 h = p+s; // calucate the value of p + h sum +=h; // compile the sum cout <<"p = " << p << ". s = " << s << ". h = "<< h << endl; } cout <<"The product of " << p << " * " << y << " is "<< z << endl; return 0; } Pam

    T T H 3 Replies Last reply
    0
    • P pam1288

      I am just learning how to use C++. I'm trying to create a simple program to do the following: A) allow the user to enter 2 integer values, B) determine the sum of all values between x & y, C) to show the product of x and y. My code for steps A & C is working. My code for step B is not, I think I'm making it much more complicated then is required. Here is the sample code: /* Write a program to find the sum of the integers between x & y. You are then to find the product*/ #include using namespace std; int main () { int x, y, z, s, p, h, sum; s = 0; sum = 0; cout << "Please enter two integers" << endl; cin >> x >> y; p = x; //p is a place holder for the value x z = x*y; // calculate the product while (s <= y) { s = ++ x; // increment x by 1 h = p+s; // calucate the value of p + h sum +=h; // compile the sum cout <<"p = " << p << ". s = " << s << ". h = "<< h << endl; } cout <<"The product of " << p << " * " << y << " is "<< z << endl; return 0; } Pam

      T Offline
      T Offline
      Tim Deveaux
      wrote on last edited by
      #2

      There's a story that Gauss was asked along with his class to sum the numbers from 1 to 100. He blurted out 5050, explaining that there were 50 pairs of numbers that added to 101 (1 + 100, 99 + 2, etc). Not so easy if the number of integers is not even, but should help. If I've just given away the answer I'm sorry - but hey, you can still get fancy with the details!

      T 1 Reply Last reply
      0
      • P pam1288

        I am just learning how to use C++. I'm trying to create a simple program to do the following: A) allow the user to enter 2 integer values, B) determine the sum of all values between x & y, C) to show the product of x and y. My code for steps A & C is working. My code for step B is not, I think I'm making it much more complicated then is required. Here is the sample code: /* Write a program to find the sum of the integers between x & y. You are then to find the product*/ #include using namespace std; int main () { int x, y, z, s, p, h, sum; s = 0; sum = 0; cout << "Please enter two integers" << endl; cin >> x >> y; p = x; //p is a place holder for the value x z = x*y; // calculate the product while (s <= y) { s = ++ x; // increment x by 1 h = p+s; // calucate the value of p + h sum +=h; // compile the sum cout <<"p = " << p << ". s = " << s << ". h = "<< h << endl; } cout <<"The product of " << p << " * " << y << " is "<< z << endl; return 0; } Pam

        T Offline
        T Offline
        Tim Smith
        wrote on last edited by
        #3

        For values greater than 0, the sum of all integers between 1 and n can be computed as n(n+1)/2. Now for your specific problem, we want to compute the sum of all integers between x and y. This can also be expressed as the sum of all integers between 1 and y minus the sum of all integers between 1 and (x-1). So the final answer would be... result = y(y+1)/2 - (x-1)x/2 Tim Smith I'm going to patent thought. I have yet to see any prior art.

        1 Reply Last reply
        0
        • T Tim Deveaux

          There's a story that Gauss was asked along with his class to sum the numbers from 1 to 100. He blurted out 5050, explaining that there were 50 pairs of numbers that added to 101 (1 + 100, 99 + 2, etc). Not so easy if the number of integers is not even, but should help. If I've just given away the answer I'm sorry - but hey, you can still get fancy with the details!

          T Offline
          T Offline
          Tim Smith
          wrote on last edited by
          #4

          The rule works for even and odd values of x. Tim Smith I'm going to patent thought. I have yet to see any prior art.

          1 Reply Last reply
          0
          • P pam1288

            I am just learning how to use C++. I'm trying to create a simple program to do the following: A) allow the user to enter 2 integer values, B) determine the sum of all values between x & y, C) to show the product of x and y. My code for steps A & C is working. My code for step B is not, I think I'm making it much more complicated then is required. Here is the sample code: /* Write a program to find the sum of the integers between x & y. You are then to find the product*/ #include using namespace std; int main () { int x, y, z, s, p, h, sum; s = 0; sum = 0; cout << "Please enter two integers" << endl; cin >> x >> y; p = x; //p is a place holder for the value x z = x*y; // calculate the product while (s <= y) { s = ++ x; // increment x by 1 h = p+s; // calucate the value of p + h sum +=h; // compile the sum cout <<"p = " << p << ". s = " << s << ". h = "<< h << endl; } cout <<"The product of " << p << " * " << y << " is "<< z << endl; return 0; } Pam

            H Offline
            H Offline
            Harco
            wrote on last edited by
            #5

            Finding the sum of the numbers between two integers can be done as follows: int i = x + 1; // we start counting 1 higher then x // or make this i = x if you want to include x int sum = 0; while (i < y) { // repeat while we haven't reached y // or make this i<=y if you want to include y sum = sum + i; i++; } voila. or shorter: int sum = 0; for (int i=x+1;i

            1 Reply Last reply
            0
            Reply
            • Reply as topic
            Log in to reply
            • Oldest to Newest
            • Newest to Oldest
            • Most Votes


            • Login

            • Don't have an account? Register

            • Login or register to search.
            • First post
              Last post
            0
            • Categories
            • Recent
            • Tags
            • Popular
            • World
            • Users
            • Groups