Find elements

Cpp program to calculate sum of odd and even numbers

Cpp program to calculate the sum of odd and even numbers

In this tutorial, we will discuss The Cpp program to calculate sum of odd and even numbers

In this article, we are going to learn the concept of how to  calculate the sum of odd and even numbers in the C++ program

 

What is even or odd numbers?

When any integer value which ends in 0,2,4,6,8 is divided by two, it is called as an even number

Example for even numbers : 34,-62,58,890

 

When any integer value which ends in 0,1,3,5,7,9 is not divided by two, it is called as an odd number

Example for  odd numbers : 21,567,-57,67

 

We can use a modular operator to find odd or even number in the given range.

if n%2==0,  n is an even number

if n%2==1,  n is an odd number

 

Calculate the sum of odd and even numbers using for loop

Program 1

This program allows the user to enter a maximum number of digits and then, the program will sum up to odd and even numbers from the from 1 to entered digits using a for loop.

#include <iostream>
#include <conio.h>
using namespace std;

int main()
{
    int i, num;  //declare variables i, num
    int oddSum=0,evenSum=0;
    //declare and initialize variables oddSum,evenSum
    cout<<"Enter the value of num \n";
    cin>>num;
    for(i=1; i<=num; i++){// for loop use  to iterate 1 to num
        if(i%2==0)  //Check even number for sum
            evenSum=evenSum+i;
        else
            oddSum=oddSum+i;
    }
    cout<<"Sum of all odd numbers are:"<< oddSum;
    cout<<"\nSum of all even numbers are:"<<evenSum;
    getch();
    return 0;
}

When the above code is executed, it produces the following results

Case 1

Enter the value of num
10
Sum of all odd numbers are:25
Sum of all even numbers are:30

 

Case 2

Enter the value of num
300
Sum of all odd numbers are:22500
Sum of all even numbers are:22650

 

Calculate the sum of odd and even numbers using while loop

Program 2

This program allows the user to enter a maximum number of digits and then, the program will sum up to odd and even numbers from the from 1 to entered digits using a while loop.

#include <iostream>
#include <conio.h>
using namespace std;

int main()
{
    int i, num;  //declare variables i, num
    int oddSum=0,evenSum=0;
    //declare and initialize variables oddSum,evenSum
    cout<<"Enter the value of num \n";
    cin>>num;

    i=1;
    while(i<=num){// for loop use  to iterate 1 to num
        if(i%2==0)  //Check even number for sum
            evenSum=evenSum+i;
        else
            oddSum=oddSum+i;
             i++;
    }
    cout<<"Sum of all odd numbers are:"<<oddSum;
    cout<<"\nSum of all even numbers are:"<<evenSum;
    getch();
    return 0;
}

When the above code is executed, it produces the following results

case 1

Enter the value of num
15
Sum of all odd numbers are: 64
Sum of all even numbers are: 56

case 2

Enter the value of num
400
Sum of all odd numbers are: 40000
Sum of all even numbers are: 40200

 

Calculate the sum of odd and even numbers using do-while loop

Program 3

This program allows the user to enter a maximum number of digits and then, the program will sum up to odd and even numbers from the from 1 to entered digits using a do-while loop.

#include <iostream>
#include <conio.h>
using namespace std;

#include <stdio.h>
#include <stdlib.h>

int main()
{
    int i,num; //declare variable i, num
    int oddSum=0,evenSum=0;
//declare and initialize variables oddSum,evenSum
    cout<<"Enter the number for num\n";
    scanf("%d",&num);//read the number from user

    i=1;
do{
if(i%2==0)
    evenSum=evenSum+i;
else
    oddSum=oddSum+i;
 i++;
}while(i<=num);
cout<<"Sum of all odd numbers are: "<<oddSum;
cout<<"\nSum of all even numbers are: "<<evenSum;
getch();
    return 0;
}

When the above code is executed, it produces the following results

case 1

Enter the value of num
75
Sum of all odd numbers are: 1444
Sum of all even numbers are: 1406

 

 

Suggested for you

for loop in C++ language

while loop in C++ language

if statements in C++ language

 

Similar post

C program to calculate sum of odd and even numbers

Java program to calculate sum of odd and even numbers

Python  program to calculate sum of odd and even numbers

Java program to calculate sum of odd and even numbers
C program to calculate sum of odd and even numbers
Karmehavannan

I am Mr S.Karmehavannan. Founder and CEO of this website. This website specially designed for the programming learners and very especially programming beginners, this website will gradually lead the learners to develop their programming skill.

View Comments

  • write a program in c++ to print even numbers up to 100 and their sum using three different loop control statements-do,while,and for

Recent Posts

PHP Star Triangle pattern program

PHP Star Triangle pattern program In this tutorial, we will discuss about PHP Star Triangle…

2 months ago

PHP Full Pyramid pattern program

PHP Full Pyramid pattern program In this tutorial, we will discuss about PHP Full Pyramid…

2 months ago

5 methods to add two numbers in Java

5 methods to add two numbers in Java In this tutorial, we will discuss the…

2 months ago

Python Full Pyramid star pattern program

Python full Pyramid star pattern program In this tutorial, we will discuss  the concept of…

5 months ago

Write a function or method to convert C into F -Entered by user

Write a function or method to convert C into F -Entered by the user In…

10 months ago

How to write a function or method to convert Celsius into Fahrenheit

How to write a function or method to convert Celsius into Fahrenheit In this tutorial,…

10 months ago