Thursday, July 14, 2011

OPERATORS

OPERATORS
In the description of a process, that portion which indicates the action to be performed on operands is called operators. The data items that operate upon are called operands. Some operators require two operands, while others act upon only one operand. A few operators permit only single variable as operand.
There are four main classes of operators in C. They are
  • Arithmetic operator
  • Relational operator
  • Logical operator
  • Assignment operator
  • Unary operator
  • Ternary operator
In addition to these operators, there are some special operators for particular task.

Arithmetic operators

A symbol used in an arithmetic expression to indicate the type of arithmetic operation to be performed. There are five arithmetic operators in C. They are:       
       operator
Meaning
+
Addition
-
Subtraction
*
Multiplication
/
Division
%
[Modulus] Remainder after integer division
The % operator is sometimes referred to as the modulus operator.
Example:
10%3 = 1
38%5 = 3
30%10 = 0
7%14 = 7
Program : WAP to find out simple interest (SI) and Amount (A), given that SI=PRT/100 and A=P+SI.
#include<stdio.h>
#include<conio.h>
main()
{
   float p,t,r,A,SI;
  
   printf("Enter value p, t, r ");
   scanf("%f %f %f",&p,&t,&r);

   //calculating simple interest
   SI=(p*t*r)/100;

   //calculating Amount
   A=SI+p;

   printf("Simple interest=%f, Amount=%f", SI, A);
   getch();
}
Program: Ask a four digit number from user. WAP to find out sum of each individual digit.
#include<stdio.h>
#include<conio.h>

void main()
{
   int x;
   int a,b,c,d, sum;

   printf("Enter any number");
   scanf("%d", &x);
  
   //separating numbers into individual
   a=x%10;
   x=x/10;
   b=x%10;
   x=x/10;
   c=x%10;
   x=x/10;
   d=x%10;
   x=x/10;
   sum=x+a+b+c+d;

   printf("sum of individual digits is %d", sum);
   getch();
}

Relational Operators

An operator that compares between two operands and returns either true or false:
Operator
Description
Example
Evaluation
==
equal
5 == 4
FALSE
5 == 5
TRUE
!=
not equal
5 != 4
TRUE
5 != 5
FALSE
greater than
5 > 4
TRUE
5 > 5
FALSE
>=
greater than or equal
5 >= 4
TRUE
5 >= 5
TRUE
less than
5 < 4
FALSE
5 < 5
FALSE
<=
less than or equal
5 <= 4
FALSE
5 <= 5
TRUE

Logical Operators

An operator that act upon operands which are themselves logical expressions. The net effect is to combine the individual logical expression into more complex condition that are either true or false.
Operator
Description
Example
Evaluation
&&
AND
(5 > 3) AND (5 > 6)
FALSE
(5 > 3) AND (5 > 4)
TRUE
||

OR
(5 > 3) OR (5 > 6)
TRUE
(5 > 3) OR (5 > 4)
TRUE
!

NOT
!(5 > 3)
FALSE
!(5 > 6)
TRUE
Suppose x = 8, y = 49, z =1.
if (x < 7 && y > 50 || z < 2) is evaluated as if (((x < 7) && (y > 50))   ||   (z < 2)) which is TRUE,
 not as if ((x < 7)   &&   ((y > 50) || (z < 2)) which is FALSE.

Assignment Operators

An operator that assigns value to operands.
Operator
Description
Example
Evaluation
=
equal to
x=10
x=10
+=
plus equal to
x+=10
x=x+10
-=
minus equal to
x-=10
x=x-10
*=
multiply equal to
x*=10
x=x*10
/=
divide equal to
x/=10
x=x/10
%=
modules equal to
x%=10
x=x%10
Unary Operators
An operator used to increment and decrement operand value by 1.
     Operator                             Description                            Example                                Evaluation
      ++                              Increment by 1                 x=10; x++                       x=11
      --                               decrement by 1                 x=10; x--                         x=9
     Example:
#include<stdio.h>
main()
{
int x=5;
printf("%d\t", x++);/*x value is first displayed and then it is incremented*/
printf("%d\t", ++x);/* x value is incremented and then displayed */
printf("%d\t",x--);/* x value is displayed and then decremented */
printf("%d\t",--x);/* x value is decremented and then displayed */
printf("%d\t", x);
}
Output:
5      7     7     5     5

Ternary Operator [Conditional Operator]:

?: is used as follows:
condition ? value if true : value if false
The condition is evaluated true or false as a Boolean expression. On the basis of the evaluation of the Boolean condition, the entire expression returns value if true if condition is true, but value if false otherwise. Usually the two sub-expressions value if true and value if false must have the same type, which determines the type of the whole expression. The importance of this type-checking lies in the operator's most common use-in conditional assignment statements. In this usage it appears as an expression on the right side of an assignment statement, as follows:
variable = condition ? value if true : value if false
The ?: operator is similar to the way conditional expressions (if--else constructs) work in functional programming languages, like Scheme, ML, and Haskell, since if-then-else forms an expression instead of a statement in those languages.
Program: WAP a program to find out the entered number is even or odd.
#include<stdio.h>
#include<conio.h>

main()
{
   int x;
   printf("Enter any number");
   scanf("%d", &x);
         x%2==0 ? printf("Even No."): printf("Odd No.");
   getch();
}

0 comments:

Post a Comment