c code for integer hunt

  • c
  • write a c code for integer hunt

    Given an array container and integer hunt. WAP to find whether hunt is present in container or not. If present, then triple the value of hunt and search again. Repeat these steps until hunt is not found. Finally return the value of hunt. Input: container = {1, 2, 3} and hunt = 1 then Output: 9 Explanation: Start with hunt = 1. Since it is present in array, it becomes 3. Now 3 is present in array and hence hunt becomes 9. Since 9 is a not present, program return 9

    Code

    #include <stdio.h> #include <stdlib.h> #include <string.h> int search(int *A, int n, int h) { for (int i = 0; i < n; i++) { if (A[i] == h) { return 1; } } return 0; } int integer_hunt(int *A, int n, int h) { while (search(A, n, h) != 0) { h = h * 3; } return h; } int main() { int n; printf("enter the number of elemnts : "); scanf("%d", &n); int arr[n]; for (int i = 0; i < n; i++) { scanf("%d", &arr[i]); } int h; printf("enter the value of Hunt : "); scanf("%d", &h); printf("%d", integer_hunt(arr, n, h)); return 0; }