C programming Q/A

Q8) Write a C function to find the kth occurrence of an odd integer in a sequence of non- negative integers, and then call your function from main. Your function should be according to the following declaration: int find_odd(int k); Input -------- You are given the input in two lines: The first line contains a positive integer k. In the second line, you will be given a sequence of numbers terminated with a -1. You have to find the kth occurrence of an odd integer in the sequence. Note: The -1 is not part of the sequence. Output ---------- If there are k odd numbers in the sequence, then output the kth occurrence of an odd number in the sequence, if present. If there are less than k odd numbers in the sequence, output -1. Sample Input ------------------ 2 1 4 3 6 5 2 3 4 1 -1 Sample Output -------------------- 3

    
    #include 

int find_odd(int k, int arr[])
{
int count = 0;
int i = 0;
while (arr[i] != -1)
{
if (arr[i] % 2 == 1)
{
count++;
if (count == k)
return arr[i];
}
i++;
}
return -1;
}

int main()
{
int k;
  scanf("%d", &k);
int arr[100];
int i = 0;
while (1)
{
scanf("%d", &arr[i]);
if (arr[i] == -1)
break;
i++;
}
int result = find_odd(k, arr);
printf("%d", result);
return 0;
}
     
Previous Post Next Post