C Program to count the number of occurrences of an element in array using recursion

Simple C program to count the number of occurrences of an element in array using recursion.
/* Count number of occurrences of an element in array */
#include<stdio.h>
int TotalCount(int arr[],int n,int num)
{
 int temp=0;
 if(n<0)
 return 0;
 if(arr[n]==num)
 temp=1;
 return temp+TotalCount(arr,n-1,num);
}
int main()
{
 int num,n,arr[]={2,4,7,4,9,2,4,0};
 n=sizeof(arr)/sizeof(arr[0]);
 printf("Enter number to count: ");
 scanf("%d",&num);
 printf("Total = %d",TotalCount(arr,n-1,num));
 return 0;
}
/*End of the program */



Popular posts from this blog