How to add array Elements in a special region | YourSite

How to add array Elements in a special region

Data Structure 1220 views

In this lesson we are trying to manipulate the elements of the array. In the data structure, you might have to take the summation of the elements of the array or do some kind of a mathematical operation on them. In this program we will to add array Elements in a special region.


// How to add array Elements in a special region

#include<stdio.h>
void main()
{
int value; 
int array[] ={1,2,3,4,5,66,7,8,9,10,11,12};
value = add(array,0,6);
printf("%d \n",value);
}

int add(int array[], int start, int finish)
{

int i = start;
int sum = 0;

for(; i<= finish; i++)
  {
  sum+=array[i];  
  }
  
  return sum;

}

Output

88
Press any key to continue . . .

Explanation

add function:

This function will add all the elements of the array and will return the sum to the calling method. This function will take the array name and start and finish indices as argements. Here is the c code.


// How to add array Elements in a special region
 
int add(int array[], int start, int finish)
{

int i = start;
int sum = 0;

for(; i<= finish; i++)
  {
  sum+=array[i];  
  }
  
  return sum;

}

This method will add all the array elements whose locations fall between the start and finish index inclusive of the two, Say, there is an array like

Array Initialization while declaration the array

int array[] ={1,2,3,4,5,66,7,8,9,10,11,12};

And we want to add 1 to 5; then we will call the above method as add(array,0,6)

This function can be used to find out the summation of all the array elements if the start index is 0 and the last index is the number of array elements. That means for the above array , if we want to find the summation of entire array elements then we should write add(array, 0,11) .

Add all array elements

int array[] ={1,2,3,4,5,66,7,8,9,10,11,12};

// How to add array Elements in a special region

#include<stdio.h>
void main()
{
int value; 
int array[] ={1,2,3,4,5,66,7,8,9,10,11,12};
value = add(array,0,11);
printf("%d \n",value);
}

int add(int array[], int start, int finish)
{

int i = start;
int sum = 0;

for(; i<= finish; i++)
  {
  sum+=array[i];  
  }
  
  return sum;

}

Output


138
Press any key to continue . . .

How to add array Elements in a special region (Take region from user)


// How to add array Elements in a special region

#include<stdio.h>
void main()
{
int start,finish,value; 
int array[] ={1,2,3,4,5,66,7,8,9,10,11,12};
printf("Enter start position and Finish position: ");
scanf("%d%d",&start,&finish);

value = add(array,start,finish);
printf("Sum is: %d \n",value);
}

int add(int array[], int start, int finish)
{

int i = start;
int sum = 0;

for(; i<= finish; i++)
  {
  sum+=array[i];  
  }
  
  return sum;

}

Output 1:

Enter start position and Finish position: 0 11
Sum is: 138
Press any key to continue . .  

Output 2:

Enter start position and Finish position: 0 5
Sum is: 81
Press any key to continue . . .

🚀 More Blogs You Might Like

Explore more articles and keep learning

What is Bounce Rate in SEO? Complete Guide for Beginners
search-engine-optimization
What is Bounce Rate in SEO? Complete Guide for Beginners

Learn what bounce rate is in SEO, how it is calculated, why it matters, common causes of high bounce rates, an...

👁 28 2026-05-24
Read More →
Comprehensive Interviewer Guide - Detailed Article
skill
Comprehensive Interviewer Guide - Detailed Article

Learn how to conduct effective interviews with this comprehensive interviewer guide. Explore hiring strategies...

👁 43 2026-05-22
Read More →
Five Industry Shifts Reshaping the AI Ecosystem (2026 Trends)
skill
Five Industry Shifts Reshaping the AI Ecosystem (2026 Trends)

Five Industry Shifts Reshaping the AI Ecosystem (2026 Trends)...

👁 38 2026-05-19
Read More →
How to Grow Your Business Mindset Step by Step
skill
How to Grow Your Business Mindset Step by Step

Learn how to develop and grow a successful business mindset step by step. Discover entrepreneurial thinking, p...

👁 56 2026-05-09
Read More →