C++ Program to find Sum of Even and Odd Numbers

Write a C++ Program to find the sum of even and odd Numbers from 0 to n. This C++ program allows you to enter the maximum number and the for loop iterate numbers from 1 to maximum. Within the loop, we used the If Else condition. if ( number % 2 == 0 ) checks whether the number % 2 equals to 0 . If true, add that number to evenSum. Otherwise, add that number to oddSum. Next, we used cout to print the sum of even numbers and odd numbers. 

#include<iostream>
using namespace std;

int main()
{
	int number, maximum, evenSum = 0, oddSum = 0;
	
	cout << "\nPlease Enter the Maximum Limit for Even & Odd Numbers  =  ";
	cin >> maximum;	
	
	for(number = 1; number <= maximum; number++)
	{
  		if ( number % 2 == 0 ) 
		{
			evenSum = evenSum + number;
		}
		else
		{
			oddSum = oddSum + number;
		}
	}
	
	cout << "\nThe Sum of All Even Numbers upto " << maximum << " = " << evenSum;
	cout << "\nThe Sum of All Odd Numbers upto " << maximum << "  = " << oddSum;
	
 	return 0;
}
Please Enter the Maximum Limit for Even & Odd Numbers  =  20

The Sum of All Even Numbers upto 20 = 110
The Sum of All Odd Numbers upto 20  = 100

C++ Program to find Sum of Even and Odd Numbers using the While loop

#include<iostream>
using namespace std;

int main()
{
	int number, maximum, evenSum = 0, oddSum = 0;
	
	cout << "\nPlease Enter the Maximum Limit for Even & Odd Numbers  =  ";
	cin >> maximum;	
	
	cout <<"\nEven and Odd Numbers between 0 and " << maximum << " = ";
	number = 1;
	while(number <= maximum)
	{
  		if ( number % 2 == 0 ) 
		{
  			//cout << number << " ";  
			cout << "\nEven Number = " << number;
			evenSum = evenSum + number;
		}
		else
		{
			//cout << number << " ";
			cout << "\nOdd Number  = " << number;
			oddSum = oddSum + number;
		}
		number++;
	}
	
	cout << "\nThe Sum of All Even Numbers upto " << maximum << " = " << evenSum;
	cout << "\nThe Sum of All Odd Numbers upto " << maximum << "  = " << oddSum;
	
 	return 0;
}
Please Enter the Maximum Limit for Even & Odd Numbers  =  10

Even and Odd Numbers between 0 and 10 = 
Odd Number  = 1
Even Number = 2
Odd Number  = 3
Even Number = 4
Odd Number  = 5
Even Number = 6
Odd Number  = 7
Even Number = 8
Odd Number  = 9
Even Number = 10
The Sum of All Even Numbers upto 10 = 30
The Sum of All Odd Numbers upto 10  = 25

This C++ program to calculate sum of odd and even numbers allows the user to enter minimum and maximum value. Next, it will calculate the sum of even and odd numbers from minimum to maximum.

#include<iostream>
using namespace std;

int main()
{
	int number, minimum, maximum, evenSum = 0, oddSum = 0;
	
	cout << "\nPlease Enter the Minimum Limit for Even & Odd Numbers  =  ";
	cin >> minimum;	
	
	cout << "\nPlease Enter the Maximum Limit for Even & Odd Numbers  =  ";
	cin >> maximum;	
	
	for(number = minimum; number <= maximum; number++)
	{
  		if ( number % 2 == 0 ) 
		{
			evenSum = evenSum + number;
		}
		else
		{
			oddSum = oddSum + number;
		}
	}
	
	cout << "\nThe Sum of All Even Numbers from " << minimum << " to "<< maximum << " = " << evenSum;
	cout << "\nThe Sum of All Odd Numbers from " << minimum << " to "<< maximum << "  = " << oddSum;
	
 	return 0;
}
C++ Program to find Sum of Even and Odd Numbers