**Write a C program to the addition of even elements and subtraction of odd elements in the array, **In this C program example, we will see how to the addition of even elements and subtraction of odd elements in an array by using for loop and if-else condition.

## Problem Statement:

Let’s assume an array with n elements that contain numbers in more than 2 digits. Now you have to find the sum of numbers that comes at even index and subtraction of the numbers that comes at even index.

#### Sample input

105

258

485

256

852

#### Sample output

6

-15

17

-9

15

# Algorithm

**Declare five variables and one array, and then initialize two variables.****Take two inputs first for the size of the array and another for element in the array.****Then, take four for loop, first for the input of element and in the second loop take 2d array and if-else condition.****Print with an appropriate message in statements.**

# C program to **the addition of even elements and subtraction of odd elements**

#include<stdio.h> void main (){ int n,j,i,m,sub=0,sum=0,r; int a[100]; printf("enter size"); scanf("%d",&r); printf("enter element\n"); for(i=0;i<r;i++){ scanf("%d",&a[i]); } for(i=0;i<r;i++){ if (i==0||i%2==0){ n=a[i]; sum=0; for(j=0;n>0;j++){ m=n%10; sum=sum+m; n=n/10; } printf("%d\n",sum);} else{ n=n/10; for(j=0;n>0;j++){ m=n%10; sub=sub-m; n=n/10; } printf("%d\n",sub);} } }

#### Sample input

105

258

485

256

852

#### Sample output

6

-15

17

-9

15

# Explanation of this C program

**Step 1:** Start.

**Step 2:** Create a header file and include a library on file.

**Step 3:** Declare five variables (i,n,m,j,r) and one array (a[100]), and initialize two variable (sum=0,sub=0).

**Step 4:** Create two inputs first for the size of the array and the second one for elements both things are taking from the user and create an input with the help of scanf.

**Step 5:** Create four for loop, one for the input of elements, and in the second one take 2d for loop and in 2d for loop create the if-else condition.

**Step 6:** Give all the conditions carefully, for if conditions are as follows:

– (i=0;i<r;i++)=for for loop,

– (i==0||i%2==0)=if condition because taking even number only if any number is odd while checking then condition can’t match and that number is placed in else condition and then check new number,

– n=a[i]=array equal too n,

– sum=0=again take sum zero because the loop start from zero,

– (j=0;j<r;j++)=for 2d for loop,

– m=n%10=taking remainder of n in m for creating a logic sum,

– sum=sum+m= logic for sum,

– n=n/10= for taking quotient.

**Step 7:** Print the appropriate message of the sum.

**Step 8:** Else condition remains same as if condition only chang two things sum replace by sub, and + replace by -.

**Step 9:** Print the appropriate message of the sub.

**Step 10:** End.

# Output

**In this way, we will see how to write a C program to the addition of even elements and subtraction of odd elements in array.**