# 7.10 – flag, exit and return statements

by on October 11, 2017

### Using flag in programming:

It is the most important logic used in many places while developing real time applications.

• A variable (flag) is initialized with a value say 0 (false).
• The value of variable (flag) is changed if any criterion is satisfied 1 (true).
• Finally we take a decision according to the value of variable (flag)

Note: flag is not a keyword; a variable with a valid name can be used as a flag.

Specification: Accept any number and print whether the number is a prime number or not using break and the flag concept.

Logic:
Here we don’t run the loop for n number of times to count total number of factors.
The main logic here is, a number is not a prime number if it has at least a single factor other than 1 and itself.
flag is initially stored with true (1).

```flag=1;
```

Checking for factors from 2 to n-1, if any factor appears in between then flag is stored with false (0).

```flag=1;
for(i=2;i<n;i++)
if(n%i==0)
{
flag=0;
break;
}
```

After the completion of loop
If flag is 1, then “n” has no factors other than 1 and itself so it is a prime.
If flag is 0, then “n” has at least one factor other than 1 and itself so it is not a prime.
Here we must also see that a negative, 0 and 1 are not prime numbers.

```if(flag&&n>1)
printf("Prime number");
else
printf("Not a prime number");
```

Program:

```#include<stdio.h>
#include<math.h>
int main()
{
int flag,n,i;
printf("Enter any integer:");
scanf("%d",&n);
flag=1;
for(i=2;i<n;i++)
if(n%i==0)
{
flag=0;
break;  /* coming out of loop */
}
if(flag&&n>1)   /* has no factors, not negative, not zero and not one*/
printf("Prime number");
else
printf("Not a prime number");
return 0;
}
```

Execution 1:
Enter any integer:1
Not a prime number
Execution 2:
Enter any integer:-17
Not a prime number
Execution 3:
Enter any integer:17
Prime number

Specification: Accept any integer and find whether a particular digit is existed or not?

Logic:
We will take a flag variable x and initialized with 0

```x=0;
```

We extract individual digits from the number using the logic

```for(x=0;n!=0;n=n/10)
n%10
```

We check the extracted digit with the required digit for every iteration, if both are equal then search is successful, 1 is assigned to flag variable and will come out of the loop.

```for(x=0;n!=0;n=n/10)
if(n%10==s)
{
x=1;
break;
}
```

After the completion of loop, if x is unchanged (0) then we consider that a particular digit is not existed. If x is changed (1) then we consider that the digit is existed

```if(x)
printf("Existed");
else
printf("Not existed");
```

Program:

```#include<stdio.h>
int main()
{
int n,f,x;
printf("Enter any integer:");
scanf("%d",&n);
printf("Find what?");
scanf("%d",&f);
for(x=0;n!=0;n=n/10)
if(n%10==f)     /* search success full */
{
x=1;
break;
}
if(x)
printf("Existed");
else
printf("Not existed");
return 0;
}
```

Execution:
Enter any integer:19876
Find what?8
Existed
Execution:
Enter any integer:19875
Find what?4
Not existed

### Halting the execution:

It is the most common control flow statement used in programming. In C language exit() is a function used to halt the program execution and quit from the program immediately. It accepts an integer as an argument and returns to the Operating system. If we send 0 as an argument then O.S thinks that, nothing wrong in program execution. If we send a non-zero as argument then O.S thinks that, there is a problem in program execution.
We need to add #include at the top of program to use exit() function.

Example:

```#include<stdio.h>
#include<stdlib.h>
int main()
{
printf("Hello");
exit(0);           * terminates from here */
printf("World");
return 0;
}
```

Output:
Hello

Specification: Accept any number and print whether the number is a prime number or not using exit().

Program:

```#include<stdio.h>
#include<stdlib.h>
int main()
{
int n,i;
printf("Enter any integer:");
scanf("%d",&n);
if(n<2)            /* negative, 0 and 1 are not prime numbers */
{
printf("Not a prime number..");
exit(0);                /* terminates */
}
for(i=2;i<n;i++)
if(n%i==0)      /* Any factors existed from 2 to n-1?*/
{
printf("Not a prime number..");
exit(0);
}
/* reaches to this point only when no factors are existed from 2 to n-1 */
printf("Prime number");
return 0;
}
```

Execution 1:
Enter any integer:-7
Not a prime number..
Execution 2:
Enter any integer:17
Prime number
Execution 3:
Enter any integer:80
Not a prime number..

### return statement:

Though the purpose of return statement is different, it can be used in place of exit(). An integer can be returned to the operating system. If we send 0 with return statement then O.S thinks that, nothing wrong in the program execution. If we send a non-zero with return statement then O.S thinks that, there is a problem in program execution.

Example:

```#include<stdio.h>
#include<stdlib.h>
int main()
{
printf("Hello");
return 0;
printf("World");
return 0;
}
```

Output:
Hello

Previous post:

Next post: