In: Computer Science
Q3) Write a C program that counts the number of odd numbers with using function count() within the set. The set has only one negative number which determines the end of set.
Program:
#include <stdio.h>
int count(int num[], int size); // Function prototype
int main()
{
int num[100],i=0; // variable declaraiton
printf("Enter numbers in the set(Negative number indicate end of the set:\n");
scanf("%d",&num[i]); // Accept the number
while(num[i]>0)
{
i++; // increment i value
scanf("%d",&num[i]); // Accept the number
}
printf("The number of odd numbers in the set is %d\n",count(num,i)); // calling funvtion and print the return value
return 0;
}
int count(int num[], int size) // Function prototype
{
int odd; // variable declaraiton
for(int i=0;i<size;i++)
{
if(num[i]%2==1) // check number is odd
odd++; // calculate number of odd numbers
}
return odd; // return count of odd numbers
}
Output: