[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” hover_enabled=”0″]

 C Program to add odd 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” hover_enabled=”0″]

 C Program to add odd number of an array element- In this topic we will see that the logic behind of addding odd number of an array element. Here i have implemented the following three loops.

  • For
  • While
  • Do..While

To understand the logic properly i have written down algorithm along with source code. This soruce code allow user to enter the array size. Obviously i have applied One dimentional array.

[/et_pb_text][et_pb_text _builder_version=”4.4.2″ header_4_font=”Verdana|600|||||||” header_4_text_color=”#000000″]

Algorithm

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

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 odd number of an array

step 7: end

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

Using For Loop

[/et_pb_text][et_pb_text _builder_version=”4.4.2″ text_font=”|600|||||||” text_text_color=”#000000″ border_radii=”on|6px|6px|6px|6px” border_width_all=”3px” border_style_all=”double”]
 int main()
{
 int size;
 int arr[size],i,sum=0;
 printf("\nEnter the size array element:");
 scanf("%d",&size);
 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",sum);
 }
   return 0;
}
[/et_pb_text][et_pb_code _builder_version=”4.4.1″][/et_pb_code][et_pb_text _builder_version=”4.0.9″ text_font=”Georgia|700|||||||” text_text_color=”#000000″]

OUTPUT

[/et_pb_text][et_pb_image src=”https://digitalmind.online/wp-content/uploads/2020/03/odd_no.png” _builder_version=”4.4.1″][/et_pb_image][et_pb_text _builder_version=”4.4.1″]

Using While Loop

[/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”]
int main()
{
 int size;
 int arr[size],i,sum=0;
 printf("\nEnter the size array element:");
 scanf("%d",&size);
 i=0;
 while(i<size)
  {
    scanf("%d",&arr[i]);
    i++; 
  }
  i=0; 
   while(i<size)
  {
     if(arr[i]%2!=0)
      {
        sum=sum+arr[i];
       }
     i++;
    printf("\nAdd even no only from array element=%d",sum);
   }
  return 0;
}
[/et_pb_text][et_pb_text _builder_version=”4.4.1″]

Using do..while loop

[/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”]
 int main()
{
 int size;
 int arr[size],i,sum=0;
 printf("\nEnter the size array element:");
 scanf("%d",&size);
 i=0;
 do{
  scanf("%d",&arr[i]);
  i++;
 }while(i<size);
   i=0;
   do{
    if(arr[i]%2!=0)
    {
      sum=sum+arr[i];
      printf("\nAdd odd no only from array element=%d",sum);
     }  
    i++;
   }while(i<size);
 
  return 0;
}
[/et_pb_text][et_pb_text _builder_version=”4.4.2″]

Recommended Post

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

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 even number of Array elements.

[/et_pb_text][/et_pb_column][/et_pb_row][/et_pb_section]