I am a beginner of c++.I have a homework about "sumodd". It's like that:
Someone input two values,valueA and valueB. It should be add the odd numbers from valueA to valueB by using for loop.How can I get it?
For example,valueA=7 valueB=11. Therefore, the sum = 7+9+11, it's equal to 27.
int main()
{
int a, b;
int max;
int min;
int sum = 0;
cin >> a;
cin >> b;
if ( a > b)
{
max = a;
min = b;
}
else
{
min = a ;
max = b;
}
for(int i = min; i <= max ; i++)
if( i % 2 == 1) sum += i;
cout << "sum is :" << sum;
return 0;
}
you might want it to check for bad input and modify the code a little bit
If we wrote your homework for you, how would you transition our of being a beginner in C++? MikeyBoy gave you a good starting point.
How about you try writing the program, and come back here with specific questions. There are a lot of people on this forum who will answer questions and give suggestions, but you have to put in some effort to get thing rolling.
I will add to MikeyBoy's pseudo-code:
1.5) Determine which number is is greater than the other.
First consider if one or both values are even, not odd, and positive.
some quasi code fragments:
int x
read x
if x/2*2==x the x is even, else it is odd.
if x is even, then x+1 and x-1 are odd (for x>0)
int sum = 0
then for int i =LoOddValue to HighOddValue sum = sum + i
print sum
This is not c or c++ code but you get the idea.
I am not going to write the code for you, since your question is not really one of coding.
It is one of algorithms:
1. How to add integers regularly spaced
2. How to check if an int x is even or odd.
I assume you know how to code input and output and syntax etc.
If not, do some more reading of looping and control structures on this site.
Learning takes effort......
go to it! and Good luck
Ex. When it is finished, test for these inputs
7, 13
7, 14
6 13
6 14
-3, 5
Then consider what it would take to make the program add the EVEN numbers between two inputs like those given
When that is sorted, can you conceive of a SINGLE algorithm (ie. not one with the above two algorithms in it) that accepts two integers and a 1 (odd) or a zero (even)
if 0 add the evens
if 1 add the odds
Sorry everybody.Since I had not the c++ programme in my computer, so I can't write the code here, but I wrote the code before this post.And here you are :)
#include <iostream>
using namespace std;
int main()
{
int valueA, valueB, smaller, large, sum=0;
cout << "Please input value A: ";
cin >> valueA;
cout << "Please input value B: ";
cin >> valueB;
if(valueA > valueB) //Here I can distinguish which num is large or smaller, am I right???
{large = valueA; smaller = valueB;}
else
{smaller = valueA; large = valueB;}
for (
cout << "Sum of odd integers from " << smaller << " to " << large << " is " << sum << endl;
return 0;
}
About the "for loop" part, how can i write the code???
Thx~
#include <iostream>
usingnamespace std;
int main()
{
int valueA, valueB, smaller, large, sum = 0;
cout << "Please input value A: ";
cin >> valueA;
cout << "Please input value B: ";
cin >> valueB;
if (valueA > valueB) //Here I can distinguish which num is large or smaller, am I right???
{
large = valueA; smaller = valueB;
}
else
{
smaller = valueA; large = valueB;
}
for (
cout << "Sum of odd integers from " << smaller << " to " << large << " is " << sum << endl;
return 0;
}