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.
CPP
#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
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
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 :
flag = True
print ( "Yes given list has even numbers." )
break
print (flag)
IsListEven(my_list)
|
C#
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" );
}
}
}
|
Javascript
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 ;
}
}
if (flag)
console.log( "YES" )
else
console.log( "NO" )
|
Example 2 : Check if given number is prime or not.
Input : n = 5
Output : Yes
Input : 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.
CPP
#include <iostream>
using namespace std;
bool isPrime( int n)
{
bool flag = true ;
if (n <= 1)
return false ;
for ( int i = 2; i < n; i++) {
if (n % i == 0) {
flag = false ;
break ;
}
}
return flag;
}
int main()
{
if (isPrime(13))
cout << "PRIME" ;
else
cout << "NOT A PRIME" ;
return 0;
}
|
Java
import java.io.*;
class GFG {
boolean isPrime( int n)
{
boolean flag = true ;
if (n <= 1 )
return false ;
for ( int i = 2 ; i < n; i++) {
if (n % i == 0 ) {
flag = false ;
break ;
}
}
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
num = 13
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
if flag:
print ( "NOT A PRIME" )
else :
print ( "PRIME" )
|
C#
using System;
public class PrimeNumberExample {
public static void Main( string [] args)
{
int n, i, m = 0, flag = 0;
n = 13;
m = n / 2;
for (i = 2; i <= m; i++) {
if (n % i == 0) {
Console.Write( "NOT A PRIME" );
flag = 1;
break ;
}
}
if (flag == 0)
Console.Write( "PRIME" );
}
}
|
Javascript
let number=13;
let flag = true ;
if (number === 1) {
console.log( "1 is neither prime nor composite number." );
}
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`);
}
}
else {
console.log( "NOT A PRIME" );
}
|
Feeling lost in the world of random DSA topics, wasting time without progress? It’s time for a change! Join our DSA course, where we’ll guide you on an exciting journey to master DSA efficiently and on schedule.
Ready to dive in? Explore our Free Demo Content and join our DSA course, trusted by over 100,000 neveropen!