C program to find sum of even number from array

Write a C program to find the sum of even numbers from an array. In this program, we are going to write a program in which we will create an array and find the sum of even numbers from an array

Algorithm to find the sum of even number in a array

  • Include the library in the program
  • Initialize the main method with void as its return type
  • Initialize variables according to the requirement
  • Performing logic and get the result
  • Print result

C program to find the sum of even number from an array

#include<stdio.h>
void main(){
  int arr[100], a,sum,i;
  printf("enter the size of array\n");
  scanf("%d",&a);
  printf("enter element\n" );
  for(i=0;i<a;i++){
    scanf("%d",&arr[i]);
  }
  for(i=0;i<a;i++){
    printf("%d \t ",arr[i]);
  }
  i=0;
  printf("\nthe even numbers are\t");
  while(i<a){
    if (arr[i]%2==0){
      printf("%d  ",arr[i]);
      sum=sum+arr[i];
    }
  i++;	
  }
  printf("\nsum is \n%d\n",sum);
}

Explanation of the code

Step 1 includes the library in the program

Step 2 declare the main method with void as the return type

Step 3 initialize the variable and array

Step 4 command to the user

Step 5 use scanf to take input according to command

Step 6 again command for the user

Step 7  using the for loop to take input and assign the inputted value into the array

Step 8 take input and assign to their respected position

Step 9 closing the scope of for loop

Step 10 using for loop again to print out the array

Step 11 printing the array with index

Step 12 closing the loop of for loop

Step 13 again setting the value of i to 0

Step 14 printing the array of even number

Step 15 Now comes to the while  loop where we make sum of even numbers

Step 16 checking the condition for even number

Step 17 printing the even number

Step 18 updating sum with even  number

Step 19 closing the condition scope

Step 20 increment in value of i

Step 21 closing while loop scope

Step 22  printing the sum of the even number

Step 23 closing the main method scope

Output

In this blog, we have learned how to create a program to find the sum of the even number from the array