### AND and OR Operators

# AND and OR Operators

welcome to chapter 6 of C programming series.

So in the last chapters we have just discussed the business problem. Now we will write a program for that.

Well, this is your business problem, let’s write the code to execute it. From this table, we understand that income tax is based on 3 factors, income, age, and gender. We have three inputs. Income is a float, age is integer and gender is a character.

We will also use a variable to declare tax percentage and tax amount. We will set a tax percentage as 0, in no condition, income tax has to be zero.

Let’s start writing our code. We have our main. Let’s declare three variables.

**main() **

**{ **

**float income;**

**int age;**

**char gender;**

**}**

Use printf statement followed by a scanf statement to take the input.

First, we will ask the user to enter income, followed by scanf. Then we will ask the user to enter age and will take the input.

**main() **

**{ **

**float income;**

**int age;**

**char gender;**

**printf(“Enter income”);**

**scanf(“%f”,&income);**

**printf(“Enter age”);**

**scanf(“%d”,&age);**

**}**

Now in the third statement we will ask the user to enter the gender. In bracket, we were asking him to enter M for male and F for female. Now we will write scanf, please make a note that here we have used %c. %c is the placeholder for the character.

**main()**

**{**

**float income, tax_amount;**

**int age, tax_percentage;**

**char gender;**

**printf(“Calculate INCOME TAX”);**

**printf(“\nEnter value for Income : “);**

**scanf(“%f”, &income);**

**printf(“\nEnter value for Age : “);**

**scanf(“%d”,&age);**

**printf(“\nEnter Gender (M for Male) (F for Female) : “);**

**scanf(” %c”, &gender);**

**}**

We will write the condition as per business requirement. Here we will write the condition in reverse order. We will explain to you why but first let’s write our program.

The **First condition** is if income is greater than 500,000 income tax is 40%.

**main()**

**{**

**float income, tax_amount;**

**int age, tax_percentage;**

**char gender;**

**printf(“Calculate INCOME TAX”);**

**printf(“\nEnter value for Income : “);**

**scanf(“%f”, &income);**

**printf(“\nEnter value for Age : “);**

**scanf(“%d”,&age);**

**printf(“\nEnter Gender (M for Male) (F for Female) : “);**

**scanf(” %c”, &gender);**

**if(income > 500000)**

**{**

**tax_percentage = 40;**

**}**

**}**

The **second condition** is if income is greater than 200,000 income tax is 30 %. It’s straight forward.

**main()**

**{**

**float income, tax_amount;**

**int age, tax_percentage;**

**char gender;**

**printf(“Calculate INCOME TAX”);**

**printf(“\nEnter value for Income : “);**

**scanf(“%f”, &income);**

**printf(“\nEnter value for Age : “);**

**scanf(“%d”,&age);**

**printf(“\nEnter Gender (M for Male) (F for Female) : “);**

**scanf(” %c”, &gender);**

**if(income > 500000)**

**{**

**tax_percentage = 40;**

**}**

**if(income > 200000)**

**{**

**tax_percentage =30;**

**}**

**}**

**Next condition** is if income is greater than 100,000 and gender is Female, then tax is 10%. So here we are writing two conditions.

Next condition is if income is greater than 100,000 and age is > 60 than tax is 10%.

Next condition is if income is greater than 100,000 and age is less than 60 and gender is Male than tax is 20%. Here we have three conditions. In such cases, it is better to group then using curve brackets.

So here is our program. We will write the formula to calculate the percentage amount. And finally, we will display the result using the printf statement.

**main()**

**{**

**float income, tax_amount;**

**int age, tax_percentage;**

**char gender;**

**tax_percentage=0;**

**tax_amount=0;**

**printf(“Calculate INCOME TAX”);**

**printf(“\nEnter value for Income : “);**

**scanf(“%f”,&income);**

**printf(“\nEnter value for Age : “);**

**scanf(“%d”,&age);**

**printf(“\nEnter Gender (m for Male, f for Female) : “);**

**scanf(” %c”,&gender);**

**if(income > 500000)**

**{**

**tax_percentage = 40;**

**}**

**if(income > 200000)**

**{**

**tax_percentage = 30;**

**}**

**if(income > 100000 && age > 60 || gender == ‘f’)**

**{**

**tax_percentage = 10;**

**}**

**if(income > 100000 && age < 60 && gender == ‘m’)**

**{**

**tax_percentage = 20;**

**}**

**tax_amount = income * tax_percentage / 100;**

**printf(“\nINCOME Tax Amount is %f and Tax percentage is %d percent”, tax_amount, tax_percentage);**

**}**

Let’s execute it. And see the result.

You can view the source code from Newtum github repository link over here.

## Output :

**Calculate INCOME TAX**

**Enter value for Income : 150000**

**Enter value for Age : 25**

**Enter Gender (m for Male, f for Female) : m**

**INCOME Tax Amount is 30000.000000 and Tax percentage is 20 percent**

Let’s iterate through it quickly. After taking the input system will check for the first condition, its false, second is also false since income is 150,000. Next condition gender is Female, but income is in the right slab, but AND operator will give the wrong result.

Coming to 4th condition. Here we have 2 conditions. Well the first result over here, it is true since income is 1,50,000 and second is age, which is false. So the end result is false.

The fifth condition has three different valuations, first is the income slab, which is true. Now the Second condition is clubbed. It Checks age as well as Gender. With the current input, both age and gender are true.

So the second clubbed condition is true. Now it’s being checked with the income slab condition. Well, the result of this, is true again. This is how we club conditions in the C Programming language.

Now, look at the business requirement once again. You see these two conditions one is age greater than 60 and Gender being Female. In both cases the result is the same.

But we have written different conditions for these two cases. We can club this together since the percentage tax is the same in both cases i.e 10%.

In 3rd and 4th condition income slab is common. We won’t touch that. Now you will have a 10% income tax if your age is > 60 or Gender is Female. Again I am repeating you will have a 10% income tax if your age is > 60 or Gender is Female. Look at the word OR Here we have or condition since if anyone of that is true then you will have 10% slab.

**main()**

**{**

**float income, tax_amount;**

**int age, tax_percentage;**

**char gender;**

**tax_percentage=0;**

**tax_amount=0;**

**printf(“Calculate INCOME TAX”);**

**printf(“\nEnter value for Income : “);**

**scanf(“%f”,&income);**

**printf(“\nEnter value for Age : “);**

**scanf(“%d”,&age);**

**printf(“\nEnter Gender (m for Male, f for Female) : “);**

**scanf(” %c”,&gender);**

**if(income > 500000)**

**{**

**tax_percentage = 40;**

**}**

**if(income > 200000)**

**{**

**tax_percentage = 30;**

**}**

**if(income > 100000 && gender == ‘f’)**

**{**

**tax_percentage = 10;**

**}**

**if(income > 100000 && age > 60 || gender == ‘f’)**

**{**

**tax_percentage = 10;**

**}**

**if(income > 100000 && age < 60 && gender == ‘m’)**

**{**

**tax_percentage = 20;**

**}**

**tax_amount = income * tax_percentage / 100;**

**printf(“\nINCOME Tax Amount is %f and Tax percentage is %d percent”, tax_amount, tax_percentage);**

**}**

So we remove condition number 3, and we add OR with age comparison. What happens here is system will check whether income slab is greater than 150,000 and it will check if age is > 60 or gender is Female.

## Output :

**Enter value for Income : 150000**

**Enter value for Age : 65**

**Enter Gender (m for Male, f for Female) : f**

**INCOME Tax Amount is 15000.000000 and Tax percentage is 10 percent**

Run the program again, We will input income as 1,50,000, age as 35 and gender as Female. The output is again correct.

## Output :

**Calculate INCOME TAX**

**Enter value for Income : 150000**

**Enter value for Age : 35**

**Enter Gender (m for Male, f for Female) : f**

**INCOME Tax Amount is 15000.000000 and Tax percentage is 10 percent**

To understand this OR condition lets iterate through the program again. Consider our inputs as 1,50,000 income and age as 65 and gender as Male.

## Output :

**Calculate INCOME TAX**

**Enter value for Income : 150000**

**Enter value for Age : 65**

**Enter Gender (m for Male, f for Female) : m**

**INCOME Tax Amount is 15000.000000 and Tax percentage is 10 percent**

The system failed the first two if’s since income is less than 400,000 and 500,000 respectively.

In the third if, the first condition is true, income is greater than 100,000. This one. Now analyze the second condition. when we combine the two IFs it forms the second condition.

Let’s first take out the output of age > 60 or gender equal to Female.

Age is 65 but gender is Male in our input. So one part gives a true result and other gives the false. But since we have used the OR Operator, the net result is true.

Please try to understand this again, the result of this combined condition is true and first conditions is also true. These two parts are joined by &&. Hence the complete result is true. Remember for AND Condition both the parameters have to be true, but for OR only one parameter is good enough.

So the system sets the income tax @10% and displays results.

In the coming chapters, we will use a lot of if and else. You will get more clarity, but you should practice this stuff. It’s not very easy to be a software developer.

Well, there is one drawback with the program. We will run the program again. This time we will input the value 600,000. Now see the output. Tax is 40% as expected.

Let’s iterate through the program again. The system checks the first condition. And it’s true. Tax value is set at 40 percent. But still, the program will continue to run, in 2,3,4 “if” and it will get false results. No impact in the output. This will cause longer execution time. Because though we obtain the result in very first if condition, it will check all the conditions.

If we can avoid this we will save some execution time.

To handle this part, C programming language has else if statement. We will rewrite this program using else if statement. See how this works.

Most of the part is the same, we will just change the plain IF and ELSE statement to incorporate ELSE IF statement.

No changes in the first condition. It’s the same. After the first condition we will write else if and then our condition. We will do the same thing for all remaining conditions. Here you are telling the program that executes the next statement if the first statement is false. Similar to subsequent conditions. If the first statement is true, the statement is jumping directly to the end of all IF statements. Over here. And it will calculate the income tax.

**main()**

**{**

**float income, tax_amount;**

**int age, tax_percentage;**

**char gender;**

**tax_percentage=0;**

**tax_amount=0;**

**printf(“Calculate INCOME TAX”);**

**printf(“\nEnter value for Income : “);**

**scanf(“%f”,&income);**

**printf(“\nEnter value for Age : “);**

**scanf(“%d”,&age);**

**printf(“\nEnter Gender (m for Male, f for Female) : “);**

**scanf(” %c”,&gender);**

**if(income > 500000)**

**{**

**tax_percentage = 40;**

**}**

**else if(income > 200000)**

**{**

**tax_percentage = 30;**

**}**

**else if(income > 100000 && age > 60 || gender == ‘f’)**

**{**

**tax_percentage = 10;**

**}**

**else if(income > 100000 && age < 60 && gender == ‘m’)**

**{**

**tax_percentage = 20;**

**}**

**tax_amount = income * tax_percentage / 100;**

**printf(“\nINCOME Tax Amount is %f and Tax percentage is %d percent”, tax_amount, tax_percentage);**

**}**

Let’s run the program again. We will enter the input 700,000, well we got the output as 40% Tax. Not much change in the output but it increases efficiency and helps to write big and complicated conditional logic.

You can view the source code from Newtum github repository link over here.

## Output :

**Calculate INCOME TAX**

**Enter value for Income : 700000**

**Enter value for Age : 25**

**Enter Gender (m for Male, f for Female) : m**

**INCOME Tax Amount is 280000.000000 and Tax percentage is 40 percent**

Well, this was ELSE IF statement. Now we need to look at NOT operator. We will write a very small program to check whether the number is greater than b or not.

## Compare- two numbers

Let’s write the program. To compare- two numbers we need two inputs.

Let’s declare the variable a and b. Ask the input from user using printf statement.

Take the input in variables using scanf statement. Please make a note that we are using only one scanf statement to input two variables. This can be done using different placeholders for different variables. Just like printf.

Now write the condition, if A > B, print a is greater than b else print a is less than b .

Well, let’s run this simple program. We entered value 5 and 10, the output is a is less than b.

**main()**

**{**

**int A, B;**

**printf(“Please Enter value for A and B : “);**

**scanf(“%d %d”,&A,&B);**

**if(A > B)**

**printf(“A is Greater than B”);**

**else**

**printf(“A is Less than B”);**

**}**

You can view the source code from Newtum github repository link over here.

## Output :

**Please Enter value for A and B : 5**

**10**

**A is Less than B**

Now let’s see how can we use the not operator. Suppose you want to print a is less than b first with the same set of conditions. What we will do is we will add not operator. We will have a>b condition in the bracket and before that append, not operator. Not operator reverses the results.

**main()**

**{**

**int A, B;**

**printf(“Please Enter value for A and B : “);**

**scanf(“%d %d”,&A,&B);**

**if(!(A > B))**

**{**

**printf(“A is Less than B”);**

**}**

**else**

**{**

**printf(“A is Greater than B”);**

**}**

**}**

Now if you enter the same input 5 and 10, the system will check the first condition, a >b its false, but not operator will make it true. So the system will print A is less than B Correctly.

## Output :

**Please Enter value for A and B : 5**

**10**

**A is Less than B**

You will use this in very complicated logic in the future. For now just assume there is a NOT operator which make true a false and vice-versa.

Well, a lot of if and else. But you will have to study, these are the only ways to write complicated if and else logic. Well, we have one more thing called a conditional operator. We will have a look at that in upcoming chapters.

So let’s summarize what we have studied over here.

The conditional statement uses if and else. If the condition is true IF part is executed and if the condition is false ELSE part is executed. If we have more than one statement we have to use curly brackets to enclose the statements.

**Six types of operators namely**

- Greater than — x > y
- Greater than equal to — x>= y
- Less than — x < y
- Less than equal to — x<= y
- Equal to — x==y
- Not equal to — x!=y

If and else can have multiple conditions using **AND**, **OR** and **Not** Operators.

We can use else if statements to avoid unwanted execution. Well, that’s about **IF and ELSE**. We will study more about it in coming chapters. Practice well.

Next chapters is very important, it’s about loops. But before going for that please practice all the examples of if and else.

I hope you don’t have any doubts in your** IFs ORs** and **ANDs**. Even if you do you can always go back and check our previous chapter.