![]() |
VOOZH | about |
Flag variable is used as a signal in programming to let the program know that a certain condition has met. It usually acts as a boolean variable indicating a condition to be either true or false.
Example 1: Check if an array has any even number.
Input : arr[] = {1, 3, 7, 5}
Output : No All numbers are odd.Input : arr[] = {1, 2, 7, 5}
Output : Yes There is one even number in the array.
We initialize a flag variable as false, then traverse the array. As soon as we find an even element, we set flag as true and break the loop. Finally we return flag.
Yes
To learn more about flags and how to effectively use them in your programs, our C programming course offers detailed lessons on using flags, control flow, and improving your program logic.
Example 2 : Check if given number is prime or not.
Input : n = 5
Output : YesInput : n = 18
Output : No
We initialize a flag variable as true. Then we traverse through all numbers from 2 to n-1. As soon as we find a number that divides n, we set flag as false. Finally we return flag.
PRIME