I am trying to practice C programming after a long time and stuck in the very basic. Trying to search in an array that works fine using while loop but when tried to use For loop, I am not getting the actual output. Here is the code that I've tried and works almost fine:
#include<stdio.h>
int main()
{
int arr[100], element, num, i;
int cnt = 1;
printf("Enter no of elements: ");
scanf("%d", &num);
printf("\nEnter the values: ");
for(i = 0; i < num; i++)
{
scanf("%d", &arr[i]);
}
printf("\nEnter the element to be searched: ");
scanf("%d", &element);
for(i = 0; i < num; i++)
{
if(arr[i] == element)
{
printf("\nNumber found at the index = %d", i + 1);
}
else
{
/**To prevent repeated output but trying to remove this section - Starts**/
if(cnt == 1)
{
printf("\nNumber not found");
}
cnt += 2;
/**To prevent repeated output but trying to remove this section - Ends**/
}
}
return 0;
}
The problem is when the searched value is matched, it gets the index of the matched value as well it prints the message 'Number not found'. But if no value matches, it only returns 'Number not found'. Here is the output I am getting for the searched values:

cntwithin the loop.