How to add array Elements in a special region | YourSite

How to add array Elements in a special region

Data Structure 1234 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

Data Science Roadmap: From Complete Beginner to Job-Ready Practitioner
Data-Science
Data Science Roadmap: From Complete Beginner to Job-Ready Practitioner

Data Science Roadmap: From Complete Beginner to Job-Ready Practitioner...

👁 10 2026-07-26
Read More →
How AI Is Changing the Way Software Is Made
Data-Science
How AI Is Changing the Way Software Is Made

How AI Is Changing the Way Software Is Made...

👁 7 2026-07-26
Read More →
8 Mistakes That Quietly Slow Down Talented Developers
Personal-Development
8 Mistakes That Quietly Slow Down Talented Developers

8 Mistakes That Quietly Slow Down Talented Developers...

👁 29 2026-07-12
Read More →
Does religion teach hatred?
islam
Does religion teach hatred?

It is easy to spread hatred in the name of religion, but understanding the true message of religion is much de...

👁 320 2026-06-30
Read More →