Find elements

C program to calculate sum of odd and even numbers

C program to calculate sum of odd and even numbers

In this tutorial, we will discuss The C 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

C program to calculate sum of odd and even numbers

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 1 to entered digits using a for loop.

 

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

int main()
{
    int i, num;  //declare variables i, num
    int oddSum=0,evenSum=0;
    //declare and initialize variables oddSum,evenSum
    printf("Enter the value of num \n");
    scanf("%d",&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;
    }
    printf("Sum of all odd numbers are: %d",oddSum);
    printf("\nSum of all even numbers are: %d",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:
100
Sum of all odd numbers are: 2500
Sum of all even numbers are: 2550

 

Calculate 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 1 to entered digits using a while loop.

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

int main()
{
    int i, num;  //declare variables i, num
    int oddSum=0,evenSum=0;
    //declare and initialize variables oddSum,evenSum
    printf("Enter the value of num \n");
    scanf("%d",&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++;
    }
    printf("Sum of all odd numbers are: %d",oddSum);
    printf("\nSum of all even numbers are: %d",evenSum);
    getch();
    return 0;
}

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

case 1

Enter the value of num
12
Sum of all odd numbers are: 36
Sum of all even numbers are: 42

 

case 2

Enter the value of num
200
Sum of all odd numbers are: 10000
Sum of all even numbers are: 10100

 

Calculate 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 1 to entered digits using a do-while loop.

#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
    printf("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);
printf("Sum of all odd numbers are: %d ",oddSum);
printf("\nSum of all even numbers are:%d ",evenSum);
getch();
    return 0;
}

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

case 1

Enter the value of num
150
Sum of all odd numbers are: 5625
Sum of all even numbers are: 5700

 

 

Suggested for you

for loop in C language

while loop in C language

if statements in C language

 

Similar post

C program to find a number is even or odd using the function

C program to separate Odd and Even numbers from an array

C program to Electricity bill calculation using the function

C program to display all even and odd numbers from 1 to n

C program display odd and even numbers without if statements

C program to calculate the sum of odd and even numbers

C program to find whether a number is even or odd

 

C++ program to find a number is even or odd using the function

C++ program to separate Odd and Even numbers from an array

C++ program to display all even and odd numbers from 1 to n

C++ program calculate Average of odd and even numbers in an array 

C++ program to calculate the sum of odd and even numbers

C++ program to find whether a number is even or odd

 

Java program to find a number is even or odd using the method

Java program to separate Odd and Even numbers from an array

Java program to display all even and odd numbers from 1 to n

Java program display odd and even numbers without if statements

Java program to calculate the sum of odd and even numbers

Java program to find whether a number is even or odd

 

Python program check whether a number is odd or even

Python program to check a number is even or odd using the function

Python program to display even and odd numbers without if

Python program to display even and odd number in the given range

Separate odd and even numbers in a list to different two list

Python Program to find odd and even numbers from a list

 

Java program to calculate sum of odd and even numbers

C++ program to calculate sum of odd and even numbers

Python program to calculate sum of odd and even numbers

Cpp program to calculate sum of odd and even numbers
Python 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

Recent Posts

C# inverted full pyramid star pattern

C# inverted full pyramid star pattern In this article, we will discuss the concept of…

3 weeks ago

C# Full Pyramid star pattern program

C# Full Pyramid star pattern program In this article, we will discuss the concept of…

1 month ago

Program to count vowels,consonants,words, characters and space in Java

Program to count vowels, consonants, words, characters and space in Java In this article, we…

1 month ago

How to print multiplication table using Array in C++ language

How to print multiplication table using Array in C++ language In this post, we will…

1 month ago

C Program to multiplication table using Array

C Program to multiplication table using Array In this tutorial , we will discuss about…

2 months ago

Java program to check odd or even using recursion

Java program to check odd or even using recursion In this tutorial, we discuss a…

2 months ago