How to add array Elements in a special region | YourSite

How to add array Elements in a special region

Data Structure • 1212 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

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...

👁 7 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...

👁 8 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...

👁 9 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 →