+ 2

Add all Even and Odd Numbers from the Entered Number

How to add even and odd number from entered number.

25th Oct 2017, 1:22 PM
GODOF7
GODOF7 - avatar
13 Respostas
+ 6
#include <iostream> using namespace std; int main() { int i = 1, n, odd = 0, even = 0; cin>>n; while(i <= n){ if (i%2!=0) odd += i; else even += i; i++; } cout<<"Even sum:"<<even<<"\n"; cout<<"Odd sum:"<<odd; return 0; }
25th Oct 2017, 1:39 PM
Kartikey Sahu
Kartikey Sahu - avatar
25th Oct 2017, 1:47 PM
Kartikey Sahu
Kartikey Sahu - avatar
+ 2
it just adds 1 to the operand e.g. i += 1 is same as i++
25th Oct 2017, 1:46 PM
Kartikey Sahu
Kartikey Sahu - avatar
+ 2
Ask if you need any help. Welcome :)
25th Oct 2017, 1:52 PM
Kartikey Sahu
Kartikey Sahu - avatar
+ 1
Are you talking about range of numbers?
25th Oct 2017, 1:32 PM
Kartikey Sahu
Kartikey Sahu - avatar
+ 1
like if I entered 5 1, 2, 3, 4, 5 odd-sum = 9 even-sum = 6 Is this what you want?
25th Oct 2017, 1:35 PM
Kartikey Sahu
Kartikey Sahu - avatar
+ 1
I really dont know what while means even the sign ++,+=,+-, my prof focuses on smart students, he choose who to guide.
25th Oct 2017, 1:39 PM
GODOF7
GODOF7 - avatar
+ 1
a += b is equivalent to a = a + b a -= b is equivalent to a = a - b a++ or ++a increments the value of 'a' by 1
25th Oct 2017, 1:41 PM
Kartikey Sahu
Kartikey Sahu - avatar
+ 1
What's the use of incrementing?
25th Oct 2017, 1:43 PM
GODOF7
GODOF7 - avatar
+ 1
Thanks for the Help!
25th Oct 2017, 1:51 PM
GODOF7
GODOF7 - avatar
0
my prof told us to use "while"
25th Oct 2017, 1:34 PM
GODOF7
GODOF7 - avatar
0
Yes
25th Oct 2017, 1:37 PM
GODOF7
GODOF7 - avatar
0
Thanks!
25th Oct 2017, 1:40 PM
GODOF7
GODOF7 - avatar