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.
// C++ program to check if given array is has
// any even number
#include <iostream>
using namespace std;
bool checkIfAnyEven(int arr[], int n)
{
bool flag = false;
for (int i=0; i<n; i++)
{
if (arr[i] % 2 == 0)
{
flag = true;
break;
}
}
return flag;
}
int main()
{
int arr[] = {1, 3, 2, 5, 6, 7};
int n = sizeof(arr)/sizeof(arr[0]);
if (checkIfAnyEven(arr, n))
cout << "Yes";
else
cout << "No";
}
//Java program to check if given array is has
// any even number
public class GFG {
boolean checkIfAnyEven(int arr[], int n)
{
boolean flag = false;
for (int i = 0; i < n; i++) {
if (arr[i] % 2 == 0) {
flag = true;
break;
}
}
return flag;
}
public static void main(String args[])
{
GFG obj = new GFG();
int arr[] = { 1, 3, 2, 5, 6, 7 };
int n = arr.length;
if (obj.checkIfAnyEven(arr, n)) {
System.out.println("YES");
}
else {
System.out.println("NO");
}
}
}
# Python program to check if given array has
# any even number
my_list=[1,3,5,2,7,9]
def IsListEven(my_list):
flag = False
for i in range(len(my_list)):
if my_list[i] % 2 == 0: # As pointed in comment, use != for isEven
flag = True
print("Yes given list has even numbers.")
break
print(flag)
IsListEven(my_list)
using System;
public class GFG {
public static bool checkIfAnyEven(int[] numberArray)
{
foreach(var n in numberArray)
{
if (n % 2 != 0)
return true;
}
return false;
}
public static void Main()
{
int[] numberArray = { 2, 4, 7, 8, 6 };
if (checkIfAnyEven(numberArray)) {
Console.WriteLine("YES");
}
else {
Console.WriteLine("NO");
}
}
}
// This code is contributed by Rahul Chauhan
// Javascript program to check if given array is has
// any even number
let arr = [1,3,2,5,6,7];
let flag = false;
{ 1, 3, 2, 5, 6, 7 };
for (let elem of arr) {
if (elem % 2 == 0) {
flag = true;
break; // get out of the loop
}
}
if (flag)
console.log("YES")
else
console.log("NO")
//This code is Contributed By Rahul Chauhan
Output
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.
// C++ implementation to show the use of flag variable
#include <iostream>
using namespace std;
// Function to return true if n is prime
bool isPrime(int n)
{
bool flag = true;
// Corner case
if (n <= 1)
return false;
// Check from 2 to n-1
for (int i = 2; i < n; i++) {
// Set flag to false and break out of the loop
// if the condition is not satisfied
if (n % i == 0) {
flag = false;
break;
}
}
// flag variable here can tell whether the previous loop
// broke without completion or it completed the execution
// satisfying all the conditions
return flag;
}
// Driver code
int main()
{
if(isPrime(13))
cout << "PRIME";
else
cout << "NOT A PRIME";
return 0;
}
/*package whatever //do not write package name here */
// Java implementation to show the use of flag variable
import java.io.*;
class GFG {
boolean isPrime(int n)
{
boolean flag = true;
// Corner case
if (n <= 1)
return false;
// Check from 2 to n-1
for (int i = 2; i < n; i++) {
// Set flag to false and break out of the loop
// if the condition is not satisfied
if (n % i == 0) {
flag = false;
break;
}
}
// flag variable here can tell whether the previous loop
// broke without completion or it completed the execution
// satisfying all the conditions
return flag;
}
public static void main(String[] args) {
GFG obj=new GFG();
int n=13;
if(obj.isPrime(n))
{
System.out.println("PRIME");
}else
{
System.out.println("NOT A PRIME");
}
}
}
# Python3 Program to check if a number is prime or not
num =13
# To take input from the user
#num = int(input("Enter Any Number: "))
# define a flag variable
flag = False
if num == 1:
print(num, "NOT A PRIME")
elif num > 1:
for i in range(2, num):
if (num % i) == 0:
flag = True
break
# check if flag is True
if flag:
print("NOT A PRIME")
else:
print("PRIME")
#This code is contributed By Rahul Chauhan
// C# implementation to show the use of flag variable
using System;
public class PrimeNumberExample {
public static void Main(string[] args)
{
int n, i, m = 0, flag = 0;
n = 13;
m = n / 2;
// Check from 2 to n-1
for (i = 2; i <= m; i++) {
// Set flag to false and break out of the loop
// if the condition is not satisfied
if (n % i == 0) {
Console.Write("NOT A PRIME");
flag = 1;
break;
}
}
if (flag == 0)
Console.Write("PRIME");
}
}
//JAVASCRIPT PROGRAM to check if a number is prime or not
let number=13;
let flag = true;
// check if number is equal to 1
if (number === 1) {
console.log("1 is neither prime nor composite number.");
}
// check if number is greater than 1
else if (number > 1) {
for (let i = 2; i < number; i++) {
if (number % i == 0) {
flag = false;
break;
}
}
if (flag) {
console.log(`PRIME`);
} else {
console.log(`NOT A PRIME NUMBER`);
}
}
// check if number is less than 1
else {
console.log("NOT A PRIME");
}
// This code is contributed By Rahul Chauhan
Output
PRIME