Practice Program







  • Program of Sum of even Element in an array.
  • Solution:
    void main()
    {
        int a[10],n,i;
        int sum_of_even = 0;
    
        printf("Enter the number of elements: ");
        scanf("%d",&n);
    
      //taking array elements as input.
        for(i=0;i<i++)
        {
            printf("\nEnter the Element: ");
            scanf("%d",&a[i]);
        }
    
        //printing the array elements
        printf("Array Elements are: \n");
        for(i=0;i<i++)
        {
            printf("%d\t",a[i]);
    
        }
    
      //CALCULATING THE SUM OF ODD AND EVEN ELEMENTS.
        for(i=0;i<i++)
        {
            if(a[i]%2 == 0)
            {
                sum_of_even = sum_of_even + a[i];
            }
    
        }
    
    
        printf("\nSum of all Even Numbers is %d",sum_of_even);
    
    
    }
    

    Output1
    Enter the number of elements: 5

    Enter the Element:1
    Enter the Element:2
    Enter the Element: 3
    Enter the Element: 4
    Enter the Element:5

    Array Elements are:
    1      2     3     4     5

    Sum of all Even Numbers is 6

    Output2
    Enter the number of elements: 10

    Enter the Element:1
    Enter the Element:2
    Enter the Element: 3
    Enter the Element: 4
    Enter the Element:5
    Enter the Element:6
    Enter the Element:7
    Enter the Element: 8
    Enter the Element: 9
    Enter the Element:10

    Array Elements are:
    1      2     3     4     5     6      7     8     9     10

    Sum of all Even Numbers is 30.

    Output3
    Enter the number of elements: 15

    Enter the Element:1
    Enter the Element:2
    Enter the Element: 3
    Enter the Element: 4
    Enter the Element:5
    Enter the Element:6
    Enter the Element:7
    Enter the Element: 8
    Enter the Element: 9
    Enter the Element:10
    Enter the Element:11
    Enter the Element:12
    Enter the Element: 13
    Enter the Element: 14
    Enter the Element:15

    Array Elements are:
    1      2     3     4     5     6      7     8     9     10     11     12     13     14    15

    Sum of all Even Numbers is 56.








    defining a function 'sum_of_even' which will return sum of all Even numbers.

    int sum_of_even(int a[],int n)
    {
         int sum_of_even = 0;
        int i;
    
         for(i=0;i<i++)
        {
            if(a[i]%2 == 0)
            {
                sum_of_even = sum_of_even + a[i];
            }
    
        }
    
    return sum_of_even;
    
    
    }
    

    Using above define function 'sum_of_even()':
    void main()
    {
        int a[10] = {1,2,3,4,5};
        
        result = sum_of_even(a,5);
        
        printf("Sum of all even numbers are %d",result);
    }
    

    Output1
    Sum of all even numbers are 6.

    Using above define function 'sum_of_even()':
    void main()
    {
        int a[10] = {1,2,3,4,5,6,7,8,9,10};
        int result;
        result = sum_of_even(a,10);
        
        printf("Sum of all even numbers are %d",result);
    }
    

    Output2
    Sum of all even numbers are 30.

    Using above define function 'sum_of_even()':
    void main()
    {
        int a[10] = {1,2,3,4,5,6,7,8,9,10,11,12,13,14,15};
        int result;
        result = sum_of_even(a,15);
        
        printf("Sum of all even numbers are %d",result);
    }
    

    Output3
    Sum of all even numbers are 54.