Finding the sum of all intergers between x & y
-
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
-
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
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!
-
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
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.
-
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!
-
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
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