Question:4
C Program to take input for “N” elements using an array, pass the entire array to a function, display the array elements and check and print total +ve elements in the array.
Sol:
#include <stdio.h> void show(int *p,int n) { int i,pos=0; for(i=0;i<n;i++) { printf("%d\n",*p); if(*p > 0) pos=pos+1; p++; } printf("Total +ve elements = %d\n",pos); } int main() { int a[20],i,n; printf("Enter total elements "); scanf("%d",&n); /* input */ for(i=0;i<n;i++) { printf("Enter %d element ",i); scanf("%d",&a[i]); } /* function calling */ show(a,n); /* or */ /* show(&a[0],n); */ return(0); }
/* Output */ Enter total elements 5 Enter 0 element 12 Enter 1 element -67 Enter 2 element 63 Enter 3 element -56 Enter 4 element 95 12 -67 63 -56 95 Total +ve elements = 3
Question:5
C Program to take input for “N” elements using an array, pass the entire array to a function, display the array elements and check and print total -ve elements in the array.
Sol:
#include <stdio.h> void show(int *p,int n) { int i,neg=0; for(i=0;i<n;i++) { printf("%d\n",*p); if(*p < 0) neg=neg+1; p++; } printf("Total -ve elements = %d\n",neg); } int main() { int a[20],i,n; printf("Enter total elements "); scanf("%d",&n); /* input */ for(i=0;i<n;i++) { printf("Enter %d element ",i); scanf("%d",&a[i]); } /* function calling */ show(a,n); /* or */ /* show(&a[0],n); */ return(0); }
/* Output */ Enter total elements 5 Enter 0 element 12 Enter 1 element -98 Enter 2 element -67 Enter 3 element 62 Enter 4 element -34 12 -98 -67 62 -34 Total -ve elements = 3
Question:6
C Program to take input for “N” elements using an array, pass the entire array to a function, display the array elements, and check and print total even elements in the array.
Sol:
#include <stdio.h> void show(int *p,int n) { int i,ev=0; for(i=0;i<n;i++) { printf("%d\n",*p); if(*p % 2==0) ev=ev+1; p++; } printf("Total even elements = %d\n",ev); } int main() { int a[20],i,n; printf("Enter total elements "); scanf("%d",&n); /* input */ for(i=0;i<n;i++) { printf("Enter %d element ",i); scanf("%d",&a[i]); } /* function calling */ show(a,n); /* or */ /* show(&a[0],n); */ return(0); }
/* Output */ Enter total elements 5 Enter 0 element 1 Enter 1 element 2 Enter 2 element 3 Enter 3 element 4 Enter 4 element 5 1 2 3 4 5 Total even elements = 2