← The C Language
Lesson 69 of 77 · 5 XP
Warm-up: where is the number? warm-up
Theory and problem statement
Machine translation, not yet reviewed by a native speaker.
The simplest search checks the elements one by one and stops at the first match:
int pos = -1;
for (int i = 0; i < n; i++) {
if (a[i] == target) {
pos = i;
break; // found it — stop looking
}
}
Task. Read n (up to 100), n numbers and the number to find. Print the index of its first occurrence (from zero), or -1 if it isn't there.
Sign up for free to solve this task right in your browser: code editor, running, test checks and hints.