October 1, 2015

# Some Practical Applications of Control Flow

Let’s do something practical and useful stuff  ! Welcome to the Newbie Programmer Series. Sorry for being late (my bad habit) Actually got stuck on some project of mine, so.. Anyways, with last post about the switch statement, We have successfully completed ‘controlling the flow’ But I think we need to do some more examples to become more easy on these. So in this post, we will do some examples and will learn some tricks and tips about controlling the flow with C. SO if you are new to this series, please go to the index (click here) and read out all the previous parts so that you can easily understand everything what I am talking ( I mean writing ) about.

We are going to make 3 real programs below.

1. A calculator
2. A invitation filter
3. A prime number tester

I am saying this again that if you get stuck at some point below, please go to the index (click here) and read.

## Calculator again…

Let’s do a warm up. Do you remember we have made a calculator ? So let’s make it more beautiful and professional.

``````#include<stdio.h>
main()
{
int choice;
float A, B;
printf("======================= \n");
printf("=     Calculator      = \n");
printf("======================= \n");
calc :
printf("\n"); //blank line
printf("Please enter two numbers : A  B \n");
scanf("%f %f",&A, &B);
printf("\n");
printf("Please tell me what to calculate ? \n");
printf("2\. Subtraction \n");
printf("3\. Multiplication \n");
printf("4\. Division \n");
printf("5\. Quit \n");
printf("\n");
scanf("%d",&choice);
switch(choice)
{
case 1:
printf("%f + %f = %f \n",A,B,A+B);
break;
case 2:
printf("%f - %f = %f \n",A,B,A-B);
break;
case 3:
printf("%f * %f = %f \n",A,B,A*B);
break;
case 4:
if( B == 0)
{
printf("Error : Division by zero not defined ! \n");
break;
}
printf("%f / %f = %f \n",A,B,A/B);
break;
case 5:
printf("Thanks for using me ! \n");
return 0;
default :
printf("You entered a wrong choice. Calculator is Restarting \n");
goto calc;
}
printf("\n");
printf("Thanks for using me ! \n");
return 0;
}
``````

OUTPUT : Check yourself, too many things to do !

What I did here is that now the user can choose what calculation to do as we have used the switch statement. Which then performs the operations based on the case entered. But in the case of division, I have used if to come out of the process if the number gets divided by zero. And also, if the user makes a wrong choice, I have used goto to get back to restart the program again by using the label calc. But i am saying again then using goto is a bad practice and one must not use it. I am doing here just to tell you how it works.

## Logical Application, Will i get invitation ?

You are going to celebrate a party. You have decided that only those persons are allowed to join the party who are one of the following :

1. Male Unmarried
2. Male Married but below 30
3. Female below 30

Instead of finding each from your contacts, make a program that tells the user if he can join the party or not.

So its simple. we will make a program that will ask the above info and then check the criteria and tell whether he can join or not.

What we had learned till now in the programming world is to break down the problem into simpler steps. So we save sex as male and female, age any, and marital status as yes and no, then the above 3 criteria can be broken as:

1. ( Male ) AND ( Not Married )
2. OR ( Male ) AND ( Married ) AND ( age below 30 )
3. OR ( Female ) AND ( age below 30 )

Now see the code below :

``````#include <stdio.h>
main()
{
int age;
char sex, marr;

printf("Enter age, sex (M or F), martial status (Y or N) in capitals\n");
scanf("%d %c %c", &age, &sex, &marr);

if ( ( sex == 'M' && marr == 'N')
|| ( sex == 'M' && marr == 'Y' && age < 30)
|| ( sex == 'F' && age < 30 ) )
{
printf("Yes you are invited ! \n");
}
else
{
printf("Sorry you are not \n");
}
}
``````

See how beautifully we used the logical operators inside if !

## Prime Number Check

I feel uncomfortable without maths don’t you think so ? Anyways let’s make a program that can check if a number we have entered is a prime number or not.

Prime numbers are those numbers which cannot be divided by any other number. Like 2, 3, 5, 7 …

Note that 1 is not a prime number.

Read this para if you like taking risks : A prime number has two distinct factors. 1 and the number itself. So 3 = 3 x 1. But for 1, we have 1 x 1 = 1. We don’t have two distinct factors for 1 so 1 is not considered as a prime number.

But how we will find it ?

Mathematicians said that :

If the given number is divisible by any of the number from 2 to half of the given number, then it’s not a prime number. Other wise it is a prime number.

For example :

Is 23 a prime number ?

23/2 ~= 11 And we can see that the number is indivisible by any of the number between 2 and 11. So it’s not.

Now see the code below :

``````#include <stdio.h>
main()
{
int num, i;
printf("Enter a number \n");
scanf("%d",&num);

for(i=2; i <= num/2; ++i)
{
if(num % i == 0)
{
printf("It's not a prime \n");
return 0;
}
}

printf("It is a prime number !");
return 0;
}
``````

Its simple. It keeps on dividing the actual number by the numbers between 2 and half of the actual number. If the number gets divided, the program quits saying it’s not a prime. If the number is not divided till the end of loop, then after the loop is finished, them message of “It is a prime number” appears.

That’s enough I think. Now we are going to start a new topic called Program Structure. So stay connected.

Please share this post as much as you can so that we can get connected to more geeks who wants to learn programming but have no clue. Thanks for reading :)

Any doubts ? Meet me at twitter @ramdeoshubham

#### You might also like:

(prev)
(next)

(rand) Simple DIY Pickup to record musical instruments
(rand) Mathematical Reasoning
(rand) Open Source in a minute