How to add array Elements in odd and even places | YourSite

How to add array Elements in odd and even places

Data Structure • 1333 views

To add array elements within specific region we can pass the start and finish index as shown in the c code in our previous blog. Click here to see the previous blog We can make this function even more intellegent. This intellegent function can be used to sum those integers in the even place of the array or the odd place elements, or can be made to return the sum of all the elements in the specified region or range.

A flag will be passed as the third arguments. If the argument is zero(0) then the function will return sum of all the elements in the specified region including the start and finish index. If the argument is one(1), then it will return the sum of only the even place numbers. If the argument is two (2), then his function would return the sum of all integers in the odd place within the specified range by start and finish. Here is the code in C.

Program

	 
// How to add array Elements in a special region

#include<stdio.h>
void main()
{
int value; 
int array[] ={0,1,2,3,4,5,6,7,8,9,10,11};  
value = arrayadd(array,0,11,0);
printf("All the elements from start to finish SUM=: %d \n",value);

value = arrayadd(array,0,11,1);
printf("Only the even place number SUM=: %d \n",value);
 
value = arrayadd(array,0,11,2);
printf("Only the odd place number SUM=: %d \n",value);
}




int arrayadd(int array[], int start, int finish, int flag)
{
int i;
int  sum;

 i = start ;
 sum = 0;

if( flag== 0 ) // Add all the elements from start to finish
 for(; i<=finish; i++)
 {
 sum+= array[i];
 }
 
 if(flag == 1) // Add only the even place number
  {
     if(start%2==0) // where to really start from?
        i = start;
    else
       i = start+1;
      
  
  for(; i<=finish; i+=2)
   sum+=array[i];
  }
  
  
  if( flag==2) // adding only the odd place number
  {
     if(start%2!=0) // where to really start from?
       i = start;
     else
	    i = start+1;
		    
     for(; i<=finish; i+=2)
     sum+=array[i];  
  
  }
  
  return sum;
  
}
	 
 

output

	
All the elements from start to finish SUM=: 66
Only the even place number SUM=: 30
Only the odd place number SUM=: 36
Press any key to continue . . .
	 
 

See all the elements and their additions according to odd and even position

	 
 
// How to add array Elements in a special region

#include<stdio.h>
void main()
{
int value; 
int array[] ={0,1,2,3,4,5,6,7,8,9,10,11};  
value = arrayadd(array,0,11,0);
printf("All the elements from start to finish SUM=: %d \n",value);

value = arrayadd(array,0,11,1);
printf("Only the even place number SUM=: %d \n",value);
 
value = arrayadd(array,0,11,2);
printf("Only the odd place number SUM=: %d \n",value);
}




int arrayadd(int array[], int start, int finish, int flag)
{
int i;
int  sum;

 i = start ;
 sum = 0;

if( flag== 0 ){ // Add all the elements from start to finish
 for(; i<=finish; i++)
 {
 printf("All Element array[%d] = %d \n",i, array[i]);
 sum+= array[i];
 }
 
}
 
 if(flag == 1) // Add only the even place number
  {
     if(start%2==0) // where to really start from?
        i = start;
    else
       i = start+1;
      
  
  for(; i<=finish; i+=2){
    printf("Edd Element array[%d] = %d \n",i, array[i]);
   sum+=array[i];
   }
  
  }
  
  
  if( flag==2) // adding only the odd place number
  {
     if(start%2!=0) // where to really start from?
       i = start;
     else
	    i = start+1;
		    
     for(; i<=finish; i+=2){
     printf("Odd Element array[%d] = %d \n",i, array[i]);
     sum+=array[i];  
     } 
  }
  
  return sum;
  
}
	 
 

output

	
 All Element array[0] = 0
All Element array[1] = 1
All Element array[2] = 2
All Element array[3] = 3
All Element array[4] = 4
All Element array[5] = 5
All Element array[6] = 6
All Element array[7] = 7
All Element array[8] = 8
All Element array[9] = 9
All Element array[10] = 10
All Element array[11] = 11
All the elements from start to finish SUM=: 66
Edd Element array[0] = 0
Edd Element array[2] = 2
Edd Element array[4] = 4
Edd Element array[6] = 6
Edd Element array[8] = 8
Edd Element array[10] = 10
Only the even place number SUM=: 30
Odd Element array[1] = 1
Odd Element array[3] = 3
Odd Element array[5] = 5
Odd Element array[7] = 7
Odd Element array[9] = 9
Odd Element array[11] = 11
Only the odd place number SUM=: 36
Press any key to continue . . .
	 
 

🚀 More Blogs You Might Like

Explore more articles and keep learning

Heat Stroke in Summer: Causes, Symptoms, Prevention and What To Do
health
Heat Stroke in Summer: Causes, Symptoms, Prevention and What To Do

Heat Stroke in Summer: Causes, Symptoms, Prevention and What To Do...

👁 8 2026-04-26
Read More →
Alcoholic Fatty Liver Disease: Causes, Symptoms, Risks and How to Improve It
health
Alcoholic Fatty Liver Disease: Causes, Symptoms, Risks and How to Improve It

Alcoholic Fatty Liver Disease: Causes, Symptoms, Risks and How to Improve It...

👁 9 2026-04-26
Read More →
Non-Alcoholic Fatty Liver: Meaning, Causes, Symptoms, and How to Improve It
health
Non-Alcoholic Fatty Liver: Meaning, Causes, Symptoms, and How to Improve It

Non-Alcoholic Fatty Liver: Meaning, Causes, Symptoms, and How to Improve It...

👁 10 2026-04-26
Read More →
The Ultimate Blueprint to Score 70/70 in ISC Class 12 Computer Science
class-1-12-resources
The Ultimate Blueprint to Score 70/70 in ISC Class 12 Computer Science

The Ultimate Blueprint to Score 70/70 in ISC Class 12 Computer Science...

👁 47 2026-04-11
Read More →