Question:7
C program to take input for 10 elements using an array, check and print total odd elements in the array.
Sol:
#include<stdio.h> int main() { int a[5],i,od=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==1) // if(a[i]%2!=0) od++; } printf("Total odd elements = %d\n",od); return(0); }
/* Output */ Enter 0 element 25 Enter 1 element 63 Enter 2 element 78 Enter 3 element 96 Enter 4 element 35 25 63 78 96 35 Total odd elements = 3
Question:8
C program to take input for 10 elements using an array, check and print total elements with zero (0) value in the array.
Sol:
#include<stdio.h> int main() { int a[5],i,z=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) z++; } printf("Total zero elements = %d\n",z); return(0); }
/* Output */ Enter 0 element 25 Enter 1 element 0 Enter 2 element 36 Enter 3 element 45 Enter 4 element 0 25 0 36 45 0 Total zero elements = 2
Question:9
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,p=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) { p++; s=s+a[i]; } } printf("Total +ve elements = %d\n",p); printf("Sum of all +ve elements = %d\n",s); av=(float)s/p; printf("Average of all +ve elements %f\n",av); return(0); }
/* Output */ Enter 0 element 6 Enter 1 element -9 Enter 2 element 2 Enter 3 element -8 Enter 4 element 3 6 -9 2 -8 3 Total +ve elements = 3 Sum of all +ve elements = 11 Average of all +ve elements 3.666667