Find sum of elements in a given integer array in c

sum using for loop:

sizeof(arr)/sizeof(arr[0]) give you the array length of arr.

#include <stdio.h>

int main()
{
    int arr[] = {1,2,3,4};
    int sum = 0;
    for(int i=0;i<sizeof(arr)/sizeof(arr[0]);i++){
        sum += arr[i];
    }
    printf("%d",sum);
}

sum using Recursion:

#include<stdio.h>


int sum_array( int arr[], int n ) {
   if (n < 0) {
     return 0;
   } 
   else {
     return arr[n] + sum_array(arr, n-1);
    }
}
    
    
int main() {
   int arr[] = {1,2,3,4,5,6,7};
   int sum = 0;
   sum = sum_array(arr,sizeof(arr)/sizeof(arr[0]));
   printf("%d",sum);
   return 0;
}