C Language | Arrays 4

Question:4
C program to take input for 5 elements using an array, check and print total +ve elements in the array.
Sol:

#include<stdio.h>

int main()
{
    int a[5],i,p=0;
    /* 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)
            p++;
    }
    printf("Total +ve elements =  %d\n",p);

    return(0);
}
/* Output */
Enter 0 element 10
Enter 1 element -96
Enter 2 element 35
Enter 3 element -85
Enter 4 element 78
10
-96
35
-85
78
Total +ve elements =  3

Question:5
C program to take input for 5 elements using an array, check and print total -ve elements in the array.
Sol:

#include<stdio.h>

int main()
{
    int a[5],i,ne=0;
    /* 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)
            ne++;
    }
    printf("Total -ve elements =  %d\n",ne);

    return(0);
}
/* Output */
Enter 0 element 25
Enter 1 element -6
Enter 2 element 75
Enter 3 element -85
Enter 4 element -74
25
-6
75
-85
-74
Total -ve elements =  3

Question:6
C program to take input for 5 elements using an array, check and print total even elements in the array.
Sol:

#include<stdio.h>

int main()
{
    int a[5],i,e=0;
    /* 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)
            e++;
    }
    printf("Total even elements =  %d\n",e);

    return(0);
}
/* Output */
Enter 0 element 25
Enter 1 element 26
Enter 2 element 35
Enter 3 element 98
Enter 4 element 45
25
26
35
98
45
Total even elements =  2

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