So in my program i need it to add all the even numbers in between and its just adding 2 numbers together. For example if I enter 2 and 14 the total is 16. But it is suppose to be 40 because 4+6+8+10+12. Here is what I have so far
#include <iostream>
#include <string>
using namespace std;
int main()
{
// lets declare some variable first.
int positiveSum =0; //this will hold sum of positive nums
int totalSum =0; // this will hold sum of all the nums
int number=0; // user input for number
for (int i = 1; i <=2; i++) // loop from 1 to 10 times
{
cout << " Enter a number: ";
cin >> number;
// now check if number is positive or negative
if (number >=0)
{
positiveSum += number; // adds this number to positiveSum
}
else if (number < 0)
{
}
}
// So finally add the positiveSum and negativeSum to get the totalSum
totalSum = positiveSum;
cout << endl;
cout << " Total of Positive numbers is: " << positiveSum << endl;
cout << " Total of all numbers is: " << totalSum << endl;
ok, assuming you took a crack at it and are very new and very confused, try something like:
cin >> start;
cin >> end;
if(start%2) start++;
for(result = 0, i = start; i <= end; i+=2) result +=i;
that should serve as the core of what you need to do, you can add couts and other logic as needed.
I am pretty sure you can also just compute it directly. Someone bored may come along... but using the old 1-n sum formula should be able to craft the whole thing in one cout statement.
krimsoh, to clear up your confusion, try doing the task in smaller steps.
1. Since you need to add the even numbers between a lower and upper limit, write a program that prompts for the lower and upper limits. Store the limits in two variables and then print them out.
2. Once you have that working add code (located after you print out the two limits) to add up the even numbers between the two. Print the sum.
Not today. Same task was solved some days or few weeks ago. Alas, the Search on top of the window goes through the internet instead of this list only. Hard cheese.
/*
** So in my program i need it to add all the even numbers in betweenand its just
** adding 2 numbers together.For example if I enter 2 and 14 the total is 16. But
** it is suppose to be 40 because 4 + 6 + 8 + 10 + 12.
*/
#include <iostream>
#include <limits>
int main ()
{
int number1{};
int number2{};
//
// A method of prompting the user and validationg the input
// don't worry to much about what it is doing yet, it will
// save you a headache if you enter a letter
while ((std::cout << "Please enter the first number : ") &&
(!(std::cin >> number1) || (number1 < 0)))
{
std::cout << "Error: invalid input.\n";
std::cin.clear ();
std::cin.ignore (std::numeric_limits<std::streamsize>::max (), '\n');
}
//
while ((std::cout << "Please enter the second number : ") &&
(!(std::cin >> number2) || (number2 < number1)))
{
std::cout << "Error: invalid input.\n";
std::cin.clear ();
std::cin.ignore (std::numeric_limits<std::streamsize>::max (), '\n');
}
int index{ number1 };
int sum{};
if (index % 2 == 0)
index += 2;
else
index++;
while (index < number2)
{
sum += index;
index += 2;
}
std::cout << "The sum of the even numbers between " << number1
<< " and " << number2 << " is " << sum << "\n\n";
return 0;
}
#include <iostream>
usingnamespace std;
int main()
{
int a, b;
cout << "Input a and b, with a < b: ";
cin >> a >> b;
b -= 2 - b % 2;
a += 2 - a % 2;
if ( b < a )
{
cout << 0 << '\n';
}
else
{
int n = 1 + ( b - a ) / 2;
cout << n * ( b + a ) / 2;
}
}
#include <iostream>
usingnamespace std;
int main()
{
int a, b, c;
cout << "Input a and b: ";
cin >> a >> b;
if ( b < a ) swap( a, b );
/*
** {
** c = a;
** a = b;
** b = c;
** }
*/
a += 2 - a % 2;
b -= 2 - b % 2;
c = 1 + ( b - a ) / 2;
cout << c * ( b + a ) / 2;
}
;)
Edit: call swap instead of "halfway circular shift exerting an auxiliary variable"