modify operator

Modify Operators in C Language : Modify operators also called Unary operators or Increment and Decrement operators. This Post explain clearly how to evaluate expressions including modify operators.

C Arithmetic Operators :

An arithmetic operator performs mathematical operations such as addition, subtraction, multiplication, division etc on numerical values (constants and variables).

Arithmetic Operators/Operation
Example
+ (Addition)
A+B
– (Subtraction)
A-B
* (multiplication)
A*B
/ (Division)
A/B
% (Modulus)
A%B

Example 1: Arithmetic Operators :

 

  1. // Working of arithmetic operators
  2. #include <stdio.h>
  3. int main()
  4. {
  5. int a = 9,b = 4, c;
  6. c = a+b;
  7. printf("a+b = %d \n",c);
  8. c = a-b;
  9. printf("a-b = %d \n",c);
  10. c = a*b;
  11. printf("a*b = %d \n",c);
  12. c = a/b;
  13. printf("a/b = %d \n",c);
  14. c = a%b;
  15. printf("Remainder when a divided by b = %d \n",c);
  16. return 0;
  17. }

 

Output :

 


                            a+b = 13
                            a-b = 5
                            a*b = 36
                            a/b = 2
                            Remainder when a divided by b=1

The operators +, – and * computes addition, subtraction, and multiplication respectively as you might have expected.

In normal calculation, 9/4 = 2.25. However, the output is 2 in the program.

It is because both the variables a and b are integers. Hence, the output is also an integer. The compiler neglects the term after the decimal point and shows answer 2 instead of 2.25.

The modulo operator % computes the remainder. When a=9 is divided by b=4, the remainder is 1. The % operator can only be used with integers.

C Increment and Decrement Operators

CCAT always ask lots of confusing snippets on this operator so do lots of Practice for these all operators .
C programming has two operators increment ++ and decrement — to change the value of an operand (constant or variable) by 1.

Increment ++ increases the value by 1 whereas decrement — decreases the value by 1. These two operators are unary operators, meaning they only operate on a single operand.

Example : Increment and Decrement Operators:

 

  1. // Working of increment and decrement operators
  2. #include <stdio.h>
  3. int main()
  4. {
  5. int a = 10, b = 100;
  6. float c = 10.5, d = 100.5;
  7. printf("++a = %d \n", ++a);
  8. printf("--b = %d \n", --b);
  9. printf("++c = %f \n", ++c);
  10. printf("--d = %f \n", --d);
  11. return 0;
  12. }

 

Output :

 


                        ++a = 11
                        --b = 99
                        ++c = 11.500000
                        ++d = 99.500000
 

Here, the operators ++ and — are used as prefixes. These two operators can also be used as postfixes like a++ and a– . There are two kinds of increment and decrement operator i.e prefix and postfix.

If the operator is used before the variable i.e ++a then it is called prefix increment operator.

If the operator is used after variable i.e a++ then it is called postfix increment operator.

In the prefix operator, first 1 is added and then the value is assigned to the variable.

Assignment Operator in C:

We have already used the assignment operator ( = ) several times before. Let’s discuss it here in detail.

The assignment operator ( = ) is used to assign a value to the variable. Its general format is as follows:

variable = right_side

The operand on the left side of the assignment operator must be a variable and operand on the right-hand side must be a constant, variable or expression. Here are some examples:

The operand on the left side of the assignment operator must be a variable and operand on the right-hand side must be a constant, variable or expression. Here are some examples: x = 18 // right operand is a constant

y = x // right operand is a variable

z = 1 * 12 + x // right operand is an expression

The precedence of the assignment operator is lower than all the operators we have discussed so far and it associates from right to left.

Relational Operators :

Relational operators are used for comparison of two values. Let’s see them one by one:

1. ‘==’ operator checks whether the two given operands are equal or not. If so, it returns true. Otherwise it returns false. For example, 5==5 will return true.

2. ‘!=’ operator checks whether the two given operands are equal or not. If not, it returns true. Otherwise it returns false. It is the exact boolean complement of the ‘==’ operator. For example, 5!=5 will return false.

3. ‘>’ operator checks whether the first operand is greater than the second operand. If so, it returns true. Otherwise it returns false. For example, 6>5 will return true.

4. ‘<‘ operator checks whether the first operand is lesser than the second operand. If so, it returns true. Otherwise it returns false. For example, 6 < 5 will return false.

5. ‘>=’ operator checks whether the first operand is greater than or equal to the second operand. If so, it returns true. Otherwise it returns false. For example, 5>=5 will return true.

6. ‘<=’ operator checks whether the first operand is lesser than or equal to the second operand. If so, it returns true. Otherwise it returns false. For example, 5<=5 will also return true.

Logical Operators :

1.They are used to combine two or more conditions/constraints or to complement the evaluation of the original condition in consideration. They are described below:

2.Logical AND: The ‘&&’ operator returns true when both the conditions in consideration are satisfied. Otherwise it returns false. For example, a && b returns true when both a and b are true (i.e. non-zero).

3.Logical OR: The ‘||’ operator returns true when one (or both) of the conditions in consideration is satisfied. Otherwise it returns false. For example, a || b returns true if one of a or b is true (i.e. non-zero). Of course, it returns true when both a and b are true.

4.Logical NOT: The ‘!’ operator returns true the condition in consideration is not satisfied. Otherwise it returns false. For example, !a returns true if a is false, i.e. when a=0.

Bitwise Operators in C :

1. & (bitwise AND) Takes two numbers as operands and does AND on every bit of two numbers. The result of AND is 1 only if both bits are 1.

2. | (bitwise OR) Takes two numbers as operands and does OR on every bit of two numbers. The result of OR is 1 if any of the two bits is 1.

3. ^ (bitwise XOR) Takes two numbers as operands and does XOR on every bit of two numbers. The result of XOR is 1 if the two bits are different.

4. << (left shift) Takes two numbers, left shifts the bits of the first operand, the second operand decides the number of places to shift.

5. >> (right shift) Takes two numbers, right shifts the bits of the first operand, the second operand decides the number of places to shift.

6. ~ (bitwise NOT) Takes one number and inverts all bits of it

Control statement in C

IF STATEMENT

The if statement allows you to control the execution of code based on a particular condition. The syntax of the if statement is as follows:

if(expression){
/* unit of code to be executed */
}

Example :
1. #include <\stdio.h>
2. #include <\stdlib.h>
int main()
{
int x;
printf(“Please enter a number:”);
scanf (“%d”,&x);
if(x > 0){
printf(“\nThe number %d is greater than 0”,x);
}
return 0;
}

The program asked users to enter a number. If the number is greater than zero, it displays a message showing that otherwise, it does nothing.

C if else statement :

Sometimes you want to execute a piece of code in case of the expression in the if statement evaluates to false. You can use the second form of the if statement which is known as if else statement. The following illustrates the syntax of the if else statement:

if(expression){
/* code block of if statement */
}
else{
/* code block of else statement */
}
In Below program user is asked to enter the age and based on the input, the if..else statement checks whether the entered age is greater than or equal to 18. If this condition meet then display message “You are eligible for voting”, however if the condition doesn’t meet then display a different message “You are not eligible for voting”.

                        #include <stdio.h>
                        int main()
                        {
                           int age;
                           printf("Enter your age:");
                           scanf("%d",&age);
                           if(age >=18)
                           {
                            /* This statement will only execute if the
                             * above condition (age>=18) returns true
                             */
                            printf("You are eligible for voting");
                           }
                           else
                           {
                            /* This statement will only execute if the
                             * condition specified in the "if" returns false.
                             */
                            printf("You are not eligible for voting");
                           }
                           return 0;
                        }

 

Output of above Program:

 


                        Enter your age:14
                        You are not eligible for voting

C Nested If..else statement:

When an if else statement is present inside the body of another “if” or “else” then this is called nested if else.

Syntax of Nested if else statement:


                        if(condition) {
                        //Nested if else inside the body of "if"
                        if(condition2) {
                           //Statements inside the body of nested "if"
                        }
                        else {
                           //Statements inside the body of nested "else"
                        }
                    }
                    else {
                        //Statements inside the body of "else"
                    }



Example of Nested If Else :


                        #include <stdio.h>
                        int main()
                        {
                           int var1, var2;
                           printf("Input the value of var1:");
                           scanf("%d", &var1);
                           printf("Input the value of var2:");
                           scanf("%d",&var2);
                           if (var1 != var2)
                           {
                            printf("var1 is not equal to var2\n");
                            //Nested if else
                            if (var1 > var2)
                            {
                                printf("var1 is greater than var2\n");
                            }
                            else
                            {
                                printf("var2 is greater than var1\n");
                            }
                           }
                           else
                           {
                            printf("var1 is equal to var2\n");
                           }
                           return 0;
                        }

 

Note : Hope you guys understand very well if else

Lets do some exercises on if else.

1. Write a C program to find maximum between two numbers.

2. Write a C program to check whether a number is even or odd.

3. Write a C program to check whether a character is alphabet or not.

4. Write a C program to check whether a number is divisible by 5 and 11 or not.

Loops In C

In any programming language including C, loops are used to execute a set of statements repeatedly until a particular condition is satisfied. Types of Loop :
There are 3 types of Loop in C language, namely:

while loop
for loop
do while loop

While Loop :

while loop can be addressed as an entry control loop. It is completed in 3 steps.
Variable initialization.(e.g int x = 0;)
condition(e.g while(x <= 10))
Variable increment or decrement ( x++ or x– or x = x + 2 )

Example: Program to print first 10 natural numbers


                    #include<stdio.h>

                    void main( )
                    {
                        int x;
                        x = 1;
                        while(x <= 10)
                        {
                            printf("%d\t", x);
                            /* below statement means, do x = x+1, increment x by 1*/
                            x++;
                        }
                    }

 

for loop:

for loop is used to execute a set of statements repeatedly until a particular condition is satisfied. We can say it is an open ended loop.. General format is,

for(initialization; condition; increment/decrement)
{
statement-block;
}

In for loop we have exactly two semicolons, one after initialization and second after the condition. In this loop we can have more than one initialization or increment/decrement, separated using comma operator. But it can have only one condition. The for loop is executed as follows:
It first evaluates the initialization code.
Then it checks the condition expression.
If it is true, it executes the for-loop body.
Then it evaluate the increment/decrement condition and again follows from step 2.
When the condition expression becomes false, it exits the loop.

Program to print first 10 natural numbers :




                      #include<stdio.h>

                      void main( )
                      {
                          int x;
                          for(x = 1; x <= 10; x++)
                          {
                              printf("%d\t", x);
                          }
                      }

 

do while loop :

In some situations it is necessary to execute body of the loop before testing the condition. Such situations can be handled with the help of do-while loop. do statement evaluates the body of the loop first and at the end, the condition is checked using while statement. It means that the body of the loop will be executed at least once, even though the starting condition inside while is initialized to be false. General syntax is,


                      do
                      {
                          .....
                          .....
                      }
                      while(condition)

 



Program to print first 10 multiples of 5 . :

#include<stdio.h>

                  void main()
                  {
                      int a, i;
                      a = 5;
                      i = 1;
                      do
                      {
                          printf("%d\t", a*i);
                          i++;
                      } 
                      while(i <= 10);
                  }

Jumping Out of Loops:

Sometimes, while executing a loop, it becomes necessary to skip a part of the loop or to leave the loop as soon as certain condition becomes true. This is known as jumping out of loop.

1) break statement :
When break statement is encountered inside a loop, the loop is immediately exited and the program continues with the statement immediately following the loop.

continue statement:

It causes the control to go directly to the test-condition and then continue the loop process. On encountering continue, cursor leave the current cycle of loop, and starts with the next cycle.

Lets Do Some exercises on loops dont copy code from google :
1. Write a C program to find the sum of first 10 natural numbers

2. Write a program in C to display the cube of the number upto given an integer.

Ex .
Input number of terms : 5
Expected Output :
Number is : 1 and cube of the 1 is :1
Number is : 2 and cube of the 2 is :8
Number is : 3 and cube of the 3 is :27
Number is : 4 and cube of the 4 is :64
Number is : 5 and cube of the 5 is :125
Means you need to run any loop upto the give no .


3. Write a program in C to display the multiplication table of a given integer. Go to the editor

4. Write a program in C to display the pattern like right angle triangle using an asterisk

The pattern like :
*
**
***
****