bsc

Comprehensive codebase and cou...
Log | Files | Refs | Activity | README | LICENSE

root / semester_1 / tuition-c / P061.c

P061.c (849B)


      1 /* Write a C program to find the sum of even elements in an array using a user defined function.
      2 Define a functions int sumEven(int arr[], int n) that returns the sum of all even elements in the array. */
      3 
      4 #include <stdio.h>
      5 
      6 void inputarr(int[], int);
      7 int sumEven(int[], int);
      8 
      9 int main()
     10 {
     11     int arr[20], n;
     12     printf("Enter the n (Max: 20): ");
     13     scanf("%d", &n);
     14     inputarr(arr, n);
     15     printf("\nSum of the even number in the array: %d", sumEven(arr, n));
     16     return 0;
     17 }
     18 
     19 void inputarr(int arr[], int n)
     20 {
     21     int i;
     22     for (i = 0; i < n; i++)
     23     {
     24         printf("Enter element %d: ", i + 1);
     25         scanf("%d", &arr[i]);
     26     }
     27 }
     28 
     29 int sumEven(int arr[], int n)
     30 {
     31     int i, sum_even = 0;
     32     for (i = 0; i < n; i++)
     33     {
     34         if (arr[i] % 2 == 0)
     35         {
     36             sum_even += arr[i];
     37         }
     38     }
     39     return sum_even;
     40 }
© notamitgamer • Site Built: 2026-09-05 01:53:16 UTC • git-mirror commit: c170d72 [view raw info]
Originally created with stagit • modified by notamitgamer
Forked from github.com/notamitgamer/git-mirror