R12 Find first occurrence of an element (Recursion) Dry Run in C

R12 Find first occurrence of an element (Recursion) is an interactive C dry run visualizer from the Recursion programs section. Study the source code, then use the execution controls to follow each step, variable update, highlighted line, and console output.

This page provides a browser-based dry run with source-code highlighting, auto-scroll, voice narration controls, and execution output for learning the program step by step.

R12 Find first occurrence of an element (Recursion) Program Code

#include <stdio.h>

int firstOccurrence(int arr[], int n, int target, int index) {
    if(index == n) {
        return -1;
    }

    if(arr[index] == target) {
        return index;
    }

    int position = firstOccurrence(arr, n, target, index + 1);
    return position;
}

int main() {
    int arr[] = {5, 3, 8, 3, 9, 8, 2};
    int target = 8;
    int pos = firstOccurrence(arr, 7, target, 0);
    if(pos != -1)
        printf("First %d at index %d\n", target, pos);
    else
        printf("%d not found\n", target);
    return 0;
}

View the complete Recursion programs page.

Program Console R12 Find first occurrence of an element (Recursion) Topic: C Vignaankosh.com
Execution Panel
Step 0/0
Console is empty.