C Language | Arrays 6

Question:10
C program to take input for 10 elements using an array, check and print the following:
* total -ve elements
* sum of all -ve elements
* average of all -ve elements
Sol:

#include<stdio.h>

int main()
{
    int a[5],i,s=0,n=0;
    float av;
    /* input */
    for(i=0;i<5;i++)
    {
        printf("Enter %d element ",i);
        scanf("%d",&a[i]);
    }
    /* display */
    for(i=0;i<5;i++)
    {
        printf("%d\n",a[i]);
        if(a[i]<0)
        {
            n++;
            s=s+a[i];
        }

    }
    printf("Total -ve elements =  %d\n",n);
    printf("Sum of all -ve elements = %d\n",s);
    av=(float)s/n;
    printf("Average of all -ve elements %f\n",av);

    return(0);
}
/* Output */

Enter 0 element -2
Enter 1 element -3
Enter 2 element -6
Enter 3 element 9
Enter 4 element 5
-2
-3
-6
9
5
Total -ve elements =  3
Sum of all -ve elements = -11
Average of all -ve elements -3.666667

Question:11
C program to take input for 10 elements using an array, check and print the following:
* total even elements
* sum of all even elements
* average of all even elements
Sol:

#include<stdio.h>

int main()
{
    int a[5],i,s=0,n=0;
    float av;
    /* input */
    for(i=0;i<5;i++)
    {
        printf("Enter %d element ",i);
        scanf("%d",&a[i]);
    }
    /* display */
    for(i=0;i<5;i++)
    {
        printf("%d\n",a[i]);
        if(a[i]%2==0)
        {
            n++;
            s=s+a[i];
        }

    }
    printf("Total even elements =  %d\n",n);
    printf("Sum of all even elements = %d\n",s);
    av=(float)s/n;
    printf("Average of all even elements %f\n",av);

    return(0);
}
/* Output */

Enter 0 element 2
Enter 1 element 3
Enter 2 element 4
Enter 3 element 5
Enter 4 element 6
2
3
4
5
6
Total even elements =  3
Sum of all even elements = 12
Average of all even elements 4.000000

Question:12
C program to take input for 10 elements using an array, check and print the following:
* total odd elements
* sum of all odd elements
* average of all odd elements
Sol:

#include<stdio.h>

int main()
{
    int a[5],i,s=0,n=0;
    float av;
    /* input */
    for(i=0;i<5;i++)
    {
        printf("Enter %d element ",i);
        scanf("%d",&a[i]);
    }
    /* display */
    for(i=0;i<5;i++)
    {
        printf("%d\n",a[i]);
        if(a[i]%2==1) // if(a[i]%2!=0)
        {
            n++;
            s=s+a[i];
        }

    }
    printf("Total odd elements =  %d\n",n);
    printf("Sum of all odd elements = %d\n",s);
    av=(float)s/n;
    printf("Average of all odd elements %f\n",av);

    return(0);
}
/* Output */

Enter 0 element 3
Enter 1 element 1
Enter 2 element 5
Enter 3 element 1
Enter 4 element 56
3
1
5
1
56
Total odd elements =  4
Sum of all odd elements = 10
Average of all odd elements 2.500000

C Language Programming Tutorial

C Language Tutorial Home     Introduction to C Language     Tokens     If Condition      goto statement and Labelname     Switch Statements     For loop     While Loop     Do while loop     break and continue     Functions     Recursion     Inbuild Functions     Storage Classes     Preprocessor     Arrays     Pointers     Structures and Unions     File Handling     Projects