C Language | Arrays 22

Question:4
C Program to take input for “n” elements using an array. Pass the entire array to a function, Further print total number of -ve elements in the array.
Sol:

#include<stdio.h>
void show(int a[],int n)
{
	int i,p=0;
	for(i=0;i<n;i++)
	{
		printf("%d\n",a[i]);
		if(a[i]<0)
		p++;
	}
	printf("Total -ve elements = %d\n",p);
}
int main()
{
	int a[50],i,n;
	printf("Enter total elements ");
	scanf("%d",&n);
	/* input  */
	for(i=0;i<n;i++)
	{
		printf("Enter element ");
		scanf("%d",&a[i]);
	}
	/* to display elements */
	show(a,n);
	return(0);
}
/* Output */

Enter total elements 5
Enter element 25
Enter element -9
Enter element -35
Enter element 65
Enter element -12
25
-9
-35
65
-12
Total -ve elements = 3

Question:5
C Program to take input for “n” elements using an array. Pass the entire array to a function, Further print total number of even elements in the array.
Sol:

#include<stdio.h>
void show(int a[],int n)
{
	int i,e=0;
	for(i=0;i<n;i++)
	{
		printf("%d\n",a[i]);
		if(a[i]%2==0)
		e++;
	}
	printf("Total even elements = %d\n",e);
}
int main()
{
	int a[50],i,n;
	printf("Enter total elements ");
	scanf("%d",&n);
	/* input  */
	for(i=0;i<n;i++)
	{
		printf("Enter element ");
		scanf("%d",&a[i]);
	}
	/* to display elements */
	show(a,n);
	return(0);
}
/* Output */

Enter total elements 5
Enter element 25
Enter element 634
Enter element 45
Enter element 24
Enter element 3
25
634
45
24
3
Total even elements = 2

Question:6
C Program to take input for “n” elements using an array. Pass the entire array to a function, Further print total number of odd elements in the array.
Sol:

#include<stdio.h>
void show(int a[],int n)
{
	int i,e=0;
	for(i=0;i<n;i++)
	{
		printf("%d\n",a[i]);
		if(a[i]%2==1) /* if(a[i]%2!=0) */
		e++;
	}
	printf("Total odd elements = %d\n",e);
}
int main()
{
	int a[50],i,n;
	printf("Enter total elements ");
	scanf("%d",&n);
	/* input  */
	for(i=0;i<n;i++)
	{
		printf("Enter element ");
		scanf("%d",&a[i]);
	}
	/* to display elements */
	show(a,n);
	return(0);
}
/* Output */

Enter total elements 5
Enter element 25
Enter element 63
Enter element 42
Enter element 18
Enter element 69
25
63
42
18
69
Total odd elements = 3

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