[et_pb_section fb_built=”1″ _builder_version=”4.0.9″][et_pb_row _builder_version=”4.0.9″][et_pb_column type=”4_4″ _builder_version=”4.0.9″][et_pb_text _builder_version=”4.4.2″ text_font=”Verdana||||||||” text_text_color=”#000000″ text_font_size=”16px” header_font=”Georgia|600|||||||” header_text_color=”#000000″]

C program to add even number of an array element

[/et_pb_text][et_pb_text _builder_version=”4.4.2″ text_font=”Verdana||||||||” text_text_color=”#000000″ text_font_size=”16px” header_font=”Georgia|600|||||||” header_text_color=”#000000″]

C Program to add even number of an array element-in this tutorial, I will show you- . the logic of adding even number of an array .Here I have implementing following three-loop

  • For
  • While
  • Do..While

To understand the logic properly i have written down algorithm along with source code. This source code allows the user to enter the array size. Obviously I have applied the One-dimensional array.

[/et_pb_text][et_pb_text _builder_version=”4.4.2″ text_font=”Verdana||||||||” text_text_color=”#000000″ text_font_size=”16px” header_font=”Georgia|600|||||||” header_text_color=”#000000″]

Algorithm

Step 1: Declare four variable size,  i, sum and an array named arr[size].

step 2: initialize sum=0, and size of array  an array will be given by the user

step 3:  Input Element  of an array will be given by the user as long as i<size .

step 4:  check as long as i<size

if (arr[i]%2 ==0) then

sum=sum+arr[i];

step 5: increament i=i+1

step 6: Get the Sum of all even number of an array

step 7: end

[/et_pb_text][et_pb_text _builder_version=”4.4.2″]

Source code

[/et_pb_text][et_pb_text _builder_version=”4.4.2″ text_font=”Verdana|600|||||||” text_text_color=”#000000″ border_radii=”on|6px|6px|6px|6px” border_width_all=”3px” border_style_all=”double”]

Using For Loop


int main() { int size; int arr[size],i,sum=0; printf("Enter array element\\n"); for(i=0; i<size; i++) { scanf("%d",&arr[i]); } for(i=0; i<size; i++) { if(arr[i]%2==0){ sum=sum+arr[i]; } printf("\\nAdd even no only from array element=%d\\n",sum); } return 0; }

[/et_pb_text][/et_pb_column][/et_pb_row][et_pb_row _builder_version=”4.0.9″][et_pb_column type=”4_4″ _builder_version=”4.0.9″][et_pb_text _builder_version=”4.0.9″ text_font=”Georgia|700|||||||” text_text_color=”#000000″ header_font=”Georgia|600|||||||” header_text_color=”#000000″]

OUTPUT

[/et_pb_text][et_pb_text _builder_version=”4.4.2″ text_font=”Georgia|700|||||||” text_text_color=”#000000″ header_font=”Georgia|600|||||||” header_text_color=”#000000″ border_radii=”on|6px|6px|6px|6px” border_width_all=”3px” border_style_all=”double”]

Using while loop

int main()
{
  int size;
  int arr[size],i,sum=0;
  printf("Enter array element\\n");
  for(i=0; i<size; i++)
  {
     scanf("%d",&arr[i]);
  } 
  
  for(i=0; i<size; i++)
  {
   if(arr[i]%2==0){
       sum=sum+arr[i];
    }
    printf("\\nAdd even no only from array element=%d\\n",sum); 
  }
   return 0;
}  

[/et_pb_text][et_pb_text _builder_version=”4.4.2″ text_font=”Georgia|700|||||||” text_text_color=”#000000″ header_font=”Georgia|600|||||||” header_text_color=”#000000″ border_radii=”on|6px|6px|6px|6px” border_width_all=”3px” border_style_all=”double”]

using do..while loop

int main()
{
  int size;
  int arr[size],i,sum=0;
  printf("Enter array element\\n");
  for(i=0; i<size; i++)
  {
     scanf("%d",&arr[i]);
  } 
  
  for(i=0; i<size; i++)
  {
   if(arr[i]%2==0){
       sum=sum+arr[i];
    }
    printf("\\nAdd even no only from array element=%d\\n",sum); 
  }
   return 0;
}  

[/et_pb_text][et_pb_text _builder_version=”4.4.2″ text_font=”Georgia|700|||||||” text_text_color=”#000000″ header_font=”Georgia|600|||||||” header_text_color=”#000000″]

Recommended post

[/et_pb_text][et_pb_text _builder_version=”4.4.2″ text_font=”Georgia|700|||||||” text_text_color=”#000000″ header_font=”Georgia|600|||||||” header_text_color=”#000000″]

Write a program in C to insert an element into an array

Write a program in C to delete an element from an array.

Write a C program to add an odd number of Array elements.

You can visit my facebook oage

[/et_pb_text][et_pb_image src=”https://digitalmind.online/wp-content/uploads/2020/01/array_even_no.png” _builder_version=”4.0.9″][/et_pb_image][/et_pb_column][/et_pb_row][/et_pb_section]