Module2 Part1
Module2 Part1
MODULE 2
       It allows the computer to evaluate the expression first and then, depending on whether the value of the
       expression (relation or condition) is ‘true’ (non zero) or ‘false’ (zero), it transfers the control to a particular
       statement. This point of program has two paths to follow, one for the true condition and the other for the
       false condition.
       The if statement may be implemented in different forms depending on the complexity of conditions to be
       tested. They are:
           1. Simple if statement
           2. If….else statement
           3. Nested if …..else statement
           4. if else if ladder
       SIMPLE IF STATEMENT
       The general form of a simple if statement is:
                                             if (test expression)
                                             {
                                                  Statement-block;
                                             }
                                             Statement-x;
       The ‘statement-block’ may be a single statement or a group of statements. If the test expression is true, the
       statement-block will be executed; otherwise the statement-block will be skipped and the execution will
       jump to the statement-x. When condition is true both the statement-block and statement-x are executed in
       sequence.
       int main()
           {
               int age;
               printf("\n Enter age ");
               scanf("%d ", &age);
               if(age>=18)
                          printf(“\n Eligible to vote”);
               if(age<18)
                          printf(“\n Not eligible to vote”);
               return 0;
       }
       In case the statement block contains only one statement, putting curly brackets becomes optional. If there is
       more than one statement in the statement block, putting curly brackets becomes mandatory.
       THE IF….ELSE STATEMENT
       The if…..else statement is an extension of the simple if statement. The general form is:
                                      if (test expression)
                                      {
                                             true-block statement(s);
                                      }
                                      else
                                      {
                                             false-block statement(s);
                                      }
                                      Statement-x;
       If the test expression is true, then the true-block statement(s), immediately following the if statement are
       executed; otherwise, the false-block statement(s), are executed. In either case true-block or false-block will
       be executed, not both.
True False
                                            Test
                                            express
                                            ion?
                                                                False-block
       True-block
                                                                statement
       statement
Statement-x
                                                       Fig: Flowchart
       Program: greatest of two numbers
       #include<stdio.h>
       int main()
       {
              int a, b;
              printf("\n Enter two integer values");
              scanf("%d %d ", &a, &b);
              if(a>b)
                        printf(“%d is greatest”,a);
              else
                        printf(“%d is greatest”,b);
              return 0;
       }
       Program: odd or even
       #include<stdio.h>
       int main()
       {
              int a;
              printf("Enter a value ");
       Program
       #include<stdio.h>
       int main()
       {
       float a,b,c;
       printf("Enter three values\n");
       scanf("%f %f %f", &a, &b, &c);
       if (a>b)
       {
               if (a>c)
                          printf("\n %f is largest", a);
               else
                          printf("%f is largest ", c);
       }
       else
       {
                                                           Output
               if (c>b)
                                                           Enter three values
                          printf("%f is largest ",c);
                                                           23445 67379 88843
               else
                                                           Largest value is 88843.000000
                          printf("%f is largest ", b);
           }
         return 0;
         }
       DANGLING ELSE PROBLEM
                 One of the classic problems encountered when we start using nested if….else statement is the
       dangling else. This occurs when a matching else is not available for an if. The answer to this problem is
       very simple.
                 Always match an else to the most recent unmatched if in the current block. In some cases, it is
       possible that the false condition is not required. In such situations, else statement may be omitted. (else is
       always paired with recent unpaired if in the current block)
                 if(n>0)
                 if(a>b)
                 z=a;
                 else
                 z=b;
       The else is associated with if(a>b). If we want else to be associated with if (n>0), then brackets should be
       used like this:
                 if(n>0)
                 {
                 if(a>b)
                 z=a;
                 }
                 else
                 z=b;
       THE ELSE IF LADDER
       Nested if else is difficult to understand and modify. So we use else if ladder. Also called cascaded if else.
       There is another way of putting ifs together when multipath decision are involved. A multipath decision is a
       chain of ifs in which the statement associated with each else is an if. This construct is known as the else if
       ladder.
                             Syntax:                                              Fig:Flowchart
       #include<stdio.h>
       int main()
       {
                float a, b, c;
                printf("Enter three values\n");
                scanf("%f %f %f", &a, &b, &b);
                if(a>b && a>c)
                printf("%f is greatest", a);
                else if(b>c)
                printf("%f is greatest", b);
                else
                printf("%f is greatest", c);
                return 0;
       }
       #include<stdio.h>
       int main()
       {
       int a;
       printf("Enter a value");
       scanf("%d", &a);
       if(a==0)
       printf("%d is zero", a);
       else if(a>0)
       printf("%d is positive", a);
       else
       printf("%d is negative", a);
       return 0;
       }
       #include<stdio.h>
       int main()
       {
               char a;
               printf("Enter a character");
               scanf("%c", &a);
               a=tolower(a);
               if(a==’a’)
                         printf("%c is a vowel", a);
               else if(a==’e’)
                         printf("%c is a vowel", a);
               else if(a==’i’)
                         printf("%c is a vowel", a);
               else if(a==’o’)
                         printf("%c is a vowel", a);
               else if(a==’u’)
                         printf("%c is a vowel", a);
               else
                         printf("%c is not a vowel or it is a consonant", a);
               return 0;
           }
       Program:
       Consider the following as grading of academic institution: Find the grade of a student if marks is given as
       input.
       Average marks                                       Grade
       80-100                                              Honours
       60-79                                               First division
       50-59                                               Second division
       40-49                                               Third division
       0-39                                                Fail
       #include<stdio.h>
       int main()
       {
                int marks;
                printf("Enter the marks of a student");
                scanf("%d", &marks);
                if(marks>=80)
                       printf(“honours");
                else if(marks>=60)
                       printf(“First division");
                else if(marks>=50)
                       printf(“Second division");
                else if(marks>=40)
                       printf(Third division");
                else
                       printf(“Fail");
                return 0;
       }
       Program - Compute the roots of a quadratic equation by accepting the coefficients. Print appropriate
       messages.
       #include<stdio.h>
       #include<stdlib.h>
       #include<math.h>
         int main()
         {
              float a,b,c, d,root1,root2,real,imag;
              printf("Enter the three coefficients:\n");
              scanf("%f%f%f",&a,&b,&c);
              if(a==0)
              {
                       printf("Invalid coefficients");
                       exit(0);
              }
              d=b*b-4*a*c;
              if(d>0)
              {
                       root1=(-b+(sqrt(d)))/(2.0*a);
                       root2=(-b-(sqrt(d)))/(2.0*a);
                       printf("The roots are real and distinct....\n");
                       printf("root1=%f \n root2=%f\n",root1,root2);
              }
              else if(d==0)
              {
                       root1=root2=-b/(2.0*a);
                       printf("The roots are real and equal....\n");
                       printf("root1=%f \n root2=%f\n",root1,root2);
              }
              else
              {
                       real=-b/(2.0*a);
                       imag= sqrt(fabs(d))/(2.0*a);
                       printf("The roots are complex and imaginary....\n");
                       printf("root1=%f+i %f \n root2= %f-i%f",real,imag,real,imag);
              }
              return 0;
         }
       Program - An electricity board charges the following rates for the use of electricity: for the first 200
       units 80 paise per unit: for the next 100 units 90 paise per unit: beyond 300 units Rs 1 per unit. All
       users are charged a minimum of Rs. 100 as meter charge. If the total amount is more than Rs 400,
       then an additional surcharge of 15% of total amount is charged. Write a program to read the name of
       the user, number of units consumed and print out the charges.
              #include<stdio.h>
              int main()
              {
                      char name[20];
                      int units;
                      float charges=0;
                      printf("\n enter the name of the user :");
                      gets(name);
                      printf("\n enter number of units consumed :");
                      scanf("%d",&units);
                      if(units<=200)
                      {
                               charges=units*0.80;
                      }
                      else if(units<=300 && units>200)
                      {
                               charges=200*0.80+(units-200)*0.90;
                      }
                      else
                      {
                              charges=200*0.80+100*0.90+(units-300)*1.00;
                      }
                      charges=charges+100;
                      if(charges>400)
                              charges=charges+0.15*charges;
                      printf("%s has to pay rupees %f",name,charges);
                      return 0;
            }
       THE SWITCH STATEMENT
              C has a built-in multi-way decision statement known as a switch. The switch statement tests the
       value of a given variable (or expression) against a list of case values and when a match is found, a block of
       statement associated with that case is executed.
       The general form of the switch statement is as shown below
                                  switch (expression)
                                  {
                                         case value-1:
                                                   Statement Block-1;
                                                   break;
                                         case value-2:
                                                   Statement Block-2;
                                                   break;
                                  ……..
                                  ………
                                         default:
                                           default-block;
                                           break;
                                  }
                                  Statement-x;
       The expression evaluates to an integral value (ie) an integer or character value. Value-1, Value-2….are
       constants known as case labels. Each of these values should be unique within a switch statement. Statement
       block-1, block-2…are statement list and may contain zero or more statements. There is no need to put
       braces around these blocks. case has labels end with a colon (:)
              When the switch is executed ,the value of the expression is successfully compared against the values
       value-1,value-2… if a case is found whose value matches with the value of the expression, then the block of
                                                        Fig: Flowchart
                Advantages of Using a switch case Statement
                Switch case statement is preferred by programmers due to the following reasons:
                       Easy to debug
                       Easy to read and understand
                       Ease of maintenance as compared with its equivalent if-else statements
                       Like if-else statements, switch statements can also be nested
                       Executes faster than its equivalent if-else construct
       Program:
       Consider the following as grading of academic institution: Find the grade of a student if marks is given as
       input.
       Average marks                                        Grade
       80-100                                               Honours
       60-79                                                First division
       50-59                                                Second division
       40-49                                                Third division
       0-39                                                 Fail
       The switch statement can be used to grade the students.
       #include<stdio.h>
       int main()
       {
                  int marks,index;
                  printf("Enter the marks of a student");
                  scanf("%d", &marks);
                  index=marks/10;
                  switch (index)
                  {
                         case 10:
                         case 9:
                         case 8:
                                   printf( “\n Honours”);
                                   break;
                         case 7:
                         Case 6:
                                   printf( “\n First Division”);
                                   break;
                         case 5:
                                   printf( “\n Second Division”);
                                   break;
                         case 4:
                                   printf( “\n Third Division”);
                                   break;
                         default:
                                   printf( “\n Fail”);
                  }
                  return 0;
              }
       Program: Simulation of a Simple Calculator.
                  #include<stdio.h>
                  #include<stdlib.h>
                  int main()
                  {
                         int a,b,res;
                      char op;
                      printf("\n Enter a simple arithmetic expression");
                      scanf("%d%c%d",&a,&op,&b);
                      switch(op)
                      {
                             case '+':
                                         res=a+b;
                                         break;
                             case '-':
                                         res=a-b;
                                         break;
                             case '*':
                                         res=a*b;
                                         break;
                             case '/':
                                         if(b!=0)
                                         res=a/b;
                                         else
                                         {
                                                    printf("division by zero is not possible");
                                                    exit(0);
                                         }
                                         break;
                             case '%':
                                         res=a%b;
                                         break;
                             default:
                                         printf("illegal operator");
                                         exit(0);
                      }
                      printf("\n%d%c%d=%d",a,op,b,res);
                      return 0;
              }
       #include<stdio.h>
       #include<ctype.h>
       int main()
           {
                 char a;
                 printf("Enter a character");
                 scanf("%c", &a);
                 a=tolower(a);
                 switch(a)
                 {
                           case ‘a’:
                           case ‘e’:
                           case ‘i’:
                           case ‘o’:
                           case ‘u’:
                                   printf("%c is a vowel", a);
                                   break;
                           default:
                                   printf("%c is not a vowel or consonant", a);
                 }
                 return 0;
       }
       THE ? : OPERATOR (The Ternary Operator)
                 The C language has an unusual operator, useful for making two-way decisions. This operator is a
       combination of ? and : and takes three operands. This operator is popularly known as the conditional
       operator.
Syntax:                                conditional expression? expression1:expression2
       The conditional expression is evaluated first. If the result is non zero, expression1 is evaluated and is
       returned as the value of the condition expression. Otherwise, expression2 is evaluated and its value is
       returned. For example, the segment
       if(a>b)
               big=a;
       else
               big=b;
       Can be written as
               big=(a>b) ? a : b;
       Conditional operator can be nested for evaluating complex conditions.
       Advantages: When the conditional operator is used, the code becomes more concise and perhaps, more
       efficient.
       Disadvantages: readability is poor.
       Program: Smallest of two numbers
       #include<stdio.h>
       int main()
       {
               int a,b,small;
               printf("enter two numbers");
               scanf("%d%d",&a,&b);
               small=(a<b)?a:b;
               printf("%d is smallest",small);
               return 0;
       }
       Program: Smallest of three numbers
       #include<stdio.h>
       int main()
       {
               int a,b,c,small;
               printf("enter three numbers");
               scanf("%d%d%d",&a,&b,&c);
               small=(a<b&&a<c)?a:(b<c?b:c);
               printf("%d is smallest",small);
               return 0;
       }
       Decision making and Looping (Iterative statements) – executes one or more statements repeatedly until
       some condition is met. (while, do while, for)
       Depending on the position of the control statement in the loop, a control structure may be classified either
       as the entry-controlled loop or as the exit-controlled loop.
       In the entry controlled loop, the control conditions are tested before the start of the loop execution. If the
       conditions are not satisfied, and then the body of the loop will not be executed.
       In the case of an exit-controlled loop, the test is performed at the end of the body of the loop and therefore
       the body is executed unconditionally for the first time. The entry-controlled loops and exit controlled loops
       also known as pre-test and post-test loops respectively.
       Entry-controlled loop                               Exit- controlled loop
       Control conditions are tested before the body Control conditions are tested after the body of
       of the loop                                         the loop
       If the control condition is false for the first Body of the loop is executed at least one time
       time, body of the loop is never executed            irrespective of the condition
       It is called pre-test loop                          It is called post-test loop
       Example – while, for                                Example – do while
       Flow-chart shown below                              Flow-chart shown below
       The C language provides for three constructs for performing loop operations
           1. The while statement
           2. The do while statement
           3. The for statement
       {
              int i,n;
              printf(“enter n:”);
              scanf(“%d”,&n);
              i=1;
              while(i<=n)
              {
                         printf(“%d\t”,i);
                         i++;
              }
              return 0;
       }
       Program (print first n natural numbers in reverse)
       #include<stdio.h>
       int main()
       {
              int i,n;
              printf(“enter n:”);
              scanf(“%d”,&n);
              i=n;
              while(i>=1)
              {
                         printf(“%d\t”,i);
                         i--;
              }
              return 0;
       }
       Program (print the sum of first n numbers)
       #include<stdio.h>
       int main()
       {
              int i,n,sum=0;
              printf(“enter n:”);
              scanf(“%d”,&n);
              i=1;
              while(i<=n)
              {
                      sum=sum+i;
                      i=i+1;
              }
              printf(“sum=%d”,sum);
              return 0;
       }
       Program (print the sum of first n squares)
       #include<stdio.h>
       void main()
       {
              int i,n,sum=0;
              printf(“enter n:”);
              scanf(“%d”,&n);
              i=1;
              while(i<=n)
              {
                      sum=sum+i*i;
                      i=i+1;
              }
              printf(“sum=%d”,sum);
       }
       THE do while LOOPING STATEMENT
       On some, occasions it might be necessary to execute to the body of the loop before the test is performed.
       Such situations can be handled with the help of the do statement.
       Syntax:
                                     do
                                     {
                                          body of the loop;
                                     }
while(test-condition);
       Since the test-condition is evaluated at the bottom of the loop, the do…while is an exit-controlled loop.
       Program (print first n natural numbers)
       #include<stdio.h>
       int main()
       {
              int i,n;
              printf(“enter n:”);
              scanf(“%d”,&n);
              i=1;
              do
              {
                         printf(“%d\t”,i);
                         i++;
              }
              while(i<=n);
              return 0;
       }
       THE FOR STATEMENT
       Simple ‘for' Loops
       The for loop is another entry-controlled loop that provides a more concise loop control structure.
       The general form of the for loop is
       Syntax:
              scanf(“%d”,&n);
              for(i=1;i<=n;i++)
              {
                         printf(“%d\t”,i);
              }
              return 0;
       }
       Program (print even numbers from 2 till n)
       #include<stdio.h>
       int main()
       {
              int i,n;
              printf(“enter n:”);
              scanf(“%d”,&n);
              for(i=2;i<=n;i=i+2)
              {
                         printf(“%d\t”,i);
              }
              return 0;
       }
       Program (print the sum of first n numbers)
       #include<stdio.h>
       int main()
       {
              int i,n,sum=0;
              printf(“enter n:”);
              scanf(“%d”,&n);
              for(i=1;i<=n;i++)
              {
                         sum=sum+i;
              }
              printf(“sum=%d”,sum);
              return 0;
       }
       Program (print the factorial of n)
       #include<stdio.h>
       int main()
       {
              int i,n,f=1;
              printf(“enter n:”);
              scanf(“%d”,&n);
              for(i=1;i<=n;i++)
              {
                      f=f*i;
              }
              printf(“factorial of %d =%d”,n,f);
              return 0;
       }
       Additional Features of for loop
           1. More than one variable can be initialized at a time in the for statement.
                               for (p=1,n=0; n<17; ++n)
           2. Like the initialization section, the increment section may also have more than one part. For example
               the loop
                               for(n=1, m=50; n<=m; n=n+1, m=m-1)
                               {
                                      p=m/n;
                                      printf(“%d %d %d\n”, n, m, p);
                               }
              is perfectly valid. The multiple arguments in the Increment section are separated by commas.
           3. The third feature is that the test-condition may have any compound relation and the testing need not
               be limited only to the loop control variable. Consider the example below:
                                      sum 0;
                                      for (i =1; i < 20 && sum < 100; ++i)
                                      {
                                               sum = sum+i;
                                               printf("%d %d\n”, i, sum);
                                       }
           4. Another unique aspect of for loop is that one or more sections can be omitted, if necessary.
                 Consider the following statements
                                       m=5;
                                       for (; m !=100;)
                                       {
                                                  printf(“%d\n”,m);
                                                  m=m+5;
                                       }
           5. we can set up time delay using the null statement(;) as follows:
                         for(i=1;i<=1000;i++);
              The loop is executed 1000 times without producing any output; it simply causes a time delay.
       Nesting of for loop
       Nesting of for loops that is one for statement within another for statement, is allowed in C. For example
       two loops can be nested as follows:
       Syntax:
       Write a C program using nested for loop to print the following pattern.
           i) *                        ii)        * * * *
                 * *                              * * *
                 * * *                            * *
                 * * * *                          *
                                #include<stdio.h>
                                int main()
                                {
                                       int i,j;
                                       for (i=1;i<=4; i++)
                                       {
                                                  for (j=1;j<=i; j++)
                                                  {
                                                          printf(“*\t”);
                                                }
                                                printf(“\n”);
                                     }
                                     return 0;
                             }
                             #include<stdio.h>
                             int main()
                             {
                                     int i,j;
                                     for (i=4;i>=1; i--)
                                     {
                                                for (j=1;j<=i; j++)
                                                {
                                                        printf(“*\t”);
                                                }
                                                printf(“\n”);
                                     }
                                     return 0;
                             }
       Just like for loop, even while and do while can be nested.
       #include<stdio.h>
       int main()
       {
              int x,y,sum;
              x=1;
              while(x<=2)
              {
                      y=1;
                      while(y<=2)
                      {
                             sum=x+y;
                             printf(“\nx=%d”,x);
                             printf(“\ny=%d”,y);
                              printf(“\nsum=%d”,sum);
                              y++;
                      }
                      x++;
               }
       }
       Output:
       x=1
       y=1
       sum=2
       x=1
       y=2
       sum=3
       x=2
       y=1
       sum=3
       x=2
       y=2
       sum=4
       Decision Making and Unconditional jump statements in C:
       goto, break, continue, exit, return
       THE GOTO STATEMENT
       The goto requires a label in order to identify the place where the branch is to be made. A label is any valid
       variable name, and must be followed by a colon. The label is placed immediately before the statement
       where the control is to be transferred.
       Syntax:
       During running of a program when a statement like - goto begin; is met, the flow of control will jump to
       the statement immediately following the label begin: This happens unconditionally.
       If the label: is before the statement goto label; a loop will be formed and some statements will be executed
       repeatedly. Such a jump is known as a backward jump. On the other hand, if the label: is placed after the
       goto label; some statements will be skipped and the jump is known as forward jump.
       Program (use goto to write a program to perform the sum of first n natural numbers) -
               Backward jump works like loop.
       #include<stdio.h>
       int main()
       {
               int i,n,sum;
               printf(“enter n”);
               scanf(“%d”,&n);
               sum=0;
               i=0;
               loop:
               sum=sum+i;
               i=i+1;
               if(i<=n)
                          goto loop;
               printf(“sum=%d”, sum);
               return 0;
       }
Program (use goto to write a program to check if the input marks is pass or fail)
#include<stdio.h>
       int main()
       {
               int marks;
               printf(“enter marks”);
               scanf(“%d”,&marks);
               if(marks<50)
                       goto fail;
               else
                       goto pass;
               fail:
                       printf(“failed”);
                       exit(0);
               pass:
                       printf(“passed”);
               return 0;
       }
       Avoiding goto
       It is a good practice to avoid using goto. Using many of them makes a program logic complicated and
       renders the program unreadable. The goto jumps shown in below fig would cause problems and therefore
       must be avoided
          Program (to print first 10 even numbers not greater than 10)
       #include<stdio.h>
       int main()
       {
               int i;
               for(i=2;i<=20;i=i+2)
               {
                        if(i>10)
                        break;
                        printf(“%d\t”,i);
               }
               return 0;
       }
       Skipping a part of loop using continue statement
       During the loop operations, it may be necessary to skip a part of the body of the loop under certain
       conditions. Unlike the break which causes the loop to be terminated, the continue, causes the loop to be
       continued with the next iteration after skipping any statements in between. The continue statement tells the
       compiler, "SKIP THE FOLLOWING STATEMENTS AND CONTINUE WITH THE NEXT
       ITERATION". The format of the continue statement is simply
Syntax:
                    continue;
The use of the continue statement in loops is Illustrated
 1 ARITHMETIC OPERATORS
              C provides all the basic arithmetic operators. The operators +,-,*, and / all work the same way as
 they do in the other languages. The unary minus operator, in effect, multiples its single operand by -1. Therefore,
 a number preceded by a minus sign changes its sign.
       Integer division truncates any fractional part. The modulo division operation produces the remainder of
 an integer division. Examples of use of arithmetic operators are:
                             a-b                    a+b
                             a*b                    a/b
                             a%b                    -a*b
       Here a and b are variables and are known as operands. The modulo division operator % cannot be used on
 floating point data.
       Integer Arithmetic
              When both the operands in a single arithmetic expression such as a+b are integers, the expression is
       called an integer expression, and the operation is called integer arithmetic. Integer arithmetic always yields
       an integer value. The largest integer values depends on the machine, as pointed out earlier. In the above
       examples, if a and bare integers, then for a=14 and b=4 we have the following results:
              a – b = 10
              a + b = 18
              a * b = 56
              a / b = 3 (decimal part truncated)
              a % b = 2 (remainder of division)
       Similarly, during modulo division, the sign of the result is always the sign of the first operand (the
       dividend). That is,
                           -14 % 3 = -2
                           -14 % -3 = -2
                           14 % -3 = 2
       Real Arithmetic / Floating Point arithmetic
              An arithmetic operation involving only real operands is called real arithmetic. If x, y, and z are floats,
       then we will have:
                                  x = 6.0 / 7.0 = 0.857143
                                  y = 1.0 / 3.0 = 0.333333
                                  z = -2.0 / 3.0 = -0.666667
       The operator % cannot be used with real operands.
       Mixed-mode Arithmetic
                When one of the operands is real and the other is integer, the expression is called a mixed-mode
       arithmetic expression. If either operand is of the real type, then only the real operation is performed and the
       result is always a real number. Thus
                                  15 / 10.0 = 1.5
                Whereas
                                  15 / 10 = 1
       Program: Addition of two numbers
       #include<stdio.h>
       int main()
       {
                int a=10,b=20,c;
               c=a+b;
               printf(“result=%d”,c);
               return 0;
       }
       2 RELATIONAL OPERATORS
               We often compare two quantities depending on their relation, take certain decisions. These
       comparisons can be done with the help of relational operators. We have already used the symbol ‘<’ ,
       meaning ‘less than’. An expression such as
                                        a<b    or   1<20
       Containing a relational operator is termed as a relational expression. The value of a relational expression is
       either one or zero. It is one if the specified relation is true and zero if the relation is false.
       For example
                                                10 < 20 is true
       but
                                                20 < 10 is false
       C supports six relational operators in all. These operators and their meanings are shown under.
       A simple relational expression contains only one relational operator and takes the following form:
                                        ae-1 relational operator ae-2
       ae-1 and ae-2 are arithmetic expressions, which may be simple constants, variables or combination of them.
                         4.5 <= 10    TRUE
                         4.5 <-10    FALSE
       When arithmetic expressions are used on either side of a relational operator, the arithmetic expressions will
 be evaluated first and then the results compared. That is, arithmetic operators have a higher priority over
 relational operators.
       Relational Operator Complements
       Among the six relational operators, each one is a complement of another operator.
                                        >       is complement of         <=
                                        <       is complement of         >=
                                        ==      is complement of         !=
       We can simplify an expression involving the not and the less than operators using the complements as
 shown below:
              Actual one                                      Simplified one
                         !(x < y)                                              x >= y
                         !(x > y)                                              x <= y
                         !(x != y)                                             x == y
                         !(x <= y)                                             x>y
                         !(x >= y)                                             x<y
                         !(x == y)                                             x != y
       Program: Greatest of two numbers
       #include<stdio.h>
       int main()
       {
              int a,b;
              printf(“enter two numbers”);
              scanf(“%d%d”,&a,&b);
              if(a>b)
                         printf(“%d is greatest”,a);
              else
                         printf(“%d is greatest”,b);
              return 0;
       }
       3 LOGICAL OPERATORS
       In addition to the relational operators, C has the following three logical operators.
                                &&      meaning logical AND
                                ||      meaning logical OR
                                !       meaning logical NOT or Logical negation
       The logical operators && and | | are used when we want to test more than one condition and make decisions.
       An example is:
                                        a > b && x==10
       An expression of this kind, which combines two or more relational expressions, is termed as a logical
       expression.
 op1                                                   !op1
 0                                                     1
 1                                                     0
       Program: Greatest of three numbers
       #include<stdio.h>
       int main()
       {
       int a,b,c;
       printf(“enter three numbers”);
       scanf(“%d%d%d”,&a,&b,&c);
       if((a>b)&&(a>c))
               printf(“%d is greatest”,a);
       else if(b>c)
               printf(“%d is greatest”,b);
       else
               printf(“%d is greatest”,c);
       return 0;
       }
       4 ASSIGNMENT OPERATORS
       Assignment operators are used to assign the result of an expression to a variable. We have seen the usual
       assignment operator, ‘=’.
                              v =exp;
       In addition, C has a set of ‘shorthand’ assignment operators of the form:
                                              v op= exp;
       Where v is a variable, exp is an expression and op is a C binary arithmetic operator. The operator op= is
       known as the shorthand assignment operator.
       The assignment statement
                                              v op= exp;
       is equivalent to
                                              v = v op (exp);
       Consider an example x += y;
       This is the same as the statement x = x + y;
                                                      Example/
                                   Operators          Description
                                                      sum         =    10;
                                                      10 is assigned to
                                   =                  variable sum
                                                      a += 10;
                                                      This is same as a = a
                                   +=                 + 10
                                                      a - = 10;
                                                      This is same as a = a
                                   -=                 – 10
                                                      a *= 10;
                                                      This is same as a = a
                                   *=                 * 10
                                                      a /= 10;
                                                      This is same as a =
                                   /=                 a / 10
                                                      a %= 10;
                                                      This is same as a = a
                                   %=                 % 10
                                                      a &= 10;
                                                      This is same as a = a
                                   &=                 & 10
                                   |=                 a |= 10;
                                                      This is same as a = a
| 10
                                                           a ^= 10;
                                                           This is same as a = a
                                    ^                      ^ 10
                                                           a <<= 10;
                                                           This is same as a = a
                                    <<                     << 10
                                                           a >>= 10;
                                                           This is same as a = a
                                    >>                     >> 10
       A prefix operator first adds 1 to the operand and then the result is assigned to the variable on the left. On
       the other hand, a postfix operator first assigns the value to the variable on left and then increments the
       operand.
       y=++x            x=x+1              y=x++                y=x
                        y=x                                     x=x+1
                        Ex : x=10                               Ex : x=10
                        x=11                                    y=10
                        y=11                                    x=11
           Program:
           #include<stdio.h>
           int main()
           {
               int a=10;
               printf(“%d”,a++);
               return 0;
           }
           Output: 10
       6 CONDITIONAL OPERATOR
       A ternary operator pair “? :” is available in C to construct conditional expressions of the form
                                             exp1 ?exp2 : exp3
       where exp1, exp2 and exp3 are expressions.
       The operator ? : works as follows: exp1 is evaluated first, if it is nonzero (true), then the expression exp2 is
       evaluated and becomes the value of the expression. If exp1 is false, exp3 is evaluated and its value becomes
       the value of the expression.
                                             a = 10;
                                               b = 15;
                                        big = (a>b) ?a : b;
       In this example, big will be assigned the value of b. This can be achieved using the if..else statements as
       follows:
                                               if (a > b)
                                                         big = a;
                                               else
                                                         big = b;
       Program: Smallest of two numbers
       int main()
       {
       inta,b,small;
       printf(“enter two numbers”);
       scanf(“%d%d”,&a,&b);
       small=a<b?a:b;
       printf(“%d is smallest”,small);
       return 0;
       }
       Program: Smallest of three numbers
       int main()
       {
       inta,b,c,small;
       printf("enter three numbers");
       scanf("%d%d%d",&a,&b,&c);
       small=a<b&&a<c?a:(b<c?b:c);
       printf("%d is smallest",small);
       return 0;
       }
   7   BITWISE OPERATORS
           C has special operators known as bitwise operators for manipulation of data at bit level. These operators
           are used for testing the bits, or shifting them right or left. Bitwise operators may not be applied to float
           or double. (bitwise complement ~ is also a bitwise operator which complements the bit (ie)
           complement of 0 is 1, complement of 1 is 0.)
 op1                                                    ~op1
 0                                                      1
 1                                                      0
C supports some special operators such as comma operator, sizeof operator, pointer operators (& and
                  sinh(x)hyperbolic sine of x
                  tanh(x)    hyperbolic tangent of x
                  ceil(x) x rounded to nearest integer
                  exp(x) e to the x power (ex)
                  abs(x) absolute value of x
                  fabs(x)absolute value of float x
                  log(x) natural log of x, x>0
                  pow(x,y) x to the power y (xy)
                  sqrt(x) square root of x, x>=0
   ARITHMETIC EXPRESSIONS
           An arithmetic expression is a combination of variables, constants, and operators arranged as per the
           syntax of the language. C can handle any complex mathematical expressions. Some of the examples of
           C expressions are shown below: