Arithmetic, logical and relational operators

Arithmetic operators
OperatorDetils
+Addition of two operands
-Subtraction of two operands
*Multiplication of two operands
/Division of two operands
%Reminder of a division
++Increment by one
--Decrement by one
Examples of Arithmetic Operators.
#include <stdio.h>
int main()
{
int x=30 ,y=20;
int result;

result = x + y; // Addition 
printf(" Output : %d \n", result);

result = x - y; // Subtraction  
printf(" Output : %d \n", result);

result = x * y; // Multiplication  
printf(" Output : %d \n", result);

result = x / y; // Division
printf(" Output : %d \n", result);

result = x % y; // reminder of the Division
printf(" Output : %d \n", result);
}
output
 Output : 50
 Output : 10
 Output : 600
 Output : 1
 Output : 10
increment and decrement operators
int i=45;
i++;
printf(" Output : %d \n", i );

i=20;
i--;
printf(" Output : %d \n", i );
Output
 Output : 46
 Output : 19
Increase by more than 1
int i=45,j=0;
i+=5;
printf(" Output : %d \n", i ); // 50
When we use assignment operator with increment and decrement operator then assignment is executed first and then increment ( or decrement ) is executed.
int i=45,j=0;
// first value is assigned to j, then increment is done to i
j=i++;
printf(" Output : %d \n", j ); // 45
In above code output is 45 ( NOT 46 ), but if you change to display value of i ( in place of j inside printf() ) , we will get 46. Logical operators
OperatorDetils
&&And operator. Output true only if all operands are true
||Or operator. Output true if any one of the operand is true
!Not operator. True if out put of condition is false
Examples ( Output is after comment )
#include <stdio.h>
int main()
{
int x=40 ,y=50;
int result;

result= (x==40) && (y ==30);
printf(" Output : %d \n", result );  //  0

result= (x==40) || (y ==30);
printf(" Output : %d \n", result );  //  1

result= !(x==40) || (y ==30);
printf(" Output : %d \n", result );  // 0
}

Relational operators

Here output is always True or False. It can be 1 or 0
OperatorDetils
==Equal To
<Less than
>Greater than
<=Less than or equal to
>=Greater than or equal to
!=Not equal to
Test on Operators Example of Relational Operators ( output are shown with comments )
#include <stdio.h>
int main()
{
int x=30 ,y=20;

printf(" Output : %d \n", x == y);  // 0
printf(" Output : %d \n", x <  y);  // 0
printf(" Output : %d \n", x >  y);  // 1
printf(" Output : %d \n", x <= y);  // 0
printf(" Output : %d \n", x >= y);  // 1
printf(" Output : %d \n", x != y);  // 1
}

Practice Questions on logical and relational operators

  1. Write the expected output of these lines of code

    int x=50,y=40;
    printf(" Output : %d \n", x == y);
    printf(" Output : %d \n", x < y);
    printf(" Output : %d \n", x > y);
    printf(" Output : %d \n", x <= y);
    printf(" Output : %d \n", x >= y);
    printf(" Output : %d \n", x != y);

  2. Understanding && and || operators ( AND OR ), write the expected outputs

    int x=45,y=25;
    int result;

    result= x==45;
    printf(" Output : %d \n", result );

    result= x!=45;
    printf(" Output : %d \n", result );

    result= x<y;
    printf(" Output : %d \n", result );

    result= (x==40) && (y ==30);
    printf(" Output : %d \n", result );

    result= (x==40) || (y ==30);
    printf(" Output : %d \n", result );

    result= !(x==40) || (y ==30);
    printf(" Output : %d \n", result );

  3. Understanding increment and decrement operators

    int i=45;
    i++;
    printf(" Output : %d \n", i );

    i=20;
    i--;
    printf(" Output : %d \n", i );

  4. Assignment with increment and decrement operators

    int i=45,j=0;
    // first value is assigned to j, then increment is done to i
    j=i++;
    printf(" Output : %d \n", j ); // 45

    i=20;
    // first value is assigned to j, then decrement is done to i
    j=i--;
    printf(" Output : %d \n", j ); // 20

  5. In above two printf() functions , change the variable to dispaly from j to i and see the difference. ( Why ? )

Subscribe

* indicates required
Subscribe to plus2net

    plus2net.com



    Post your comments , suggestion , error , requirements etc here




    We use cookies to improve your browsing experience. . Learn more
    HTML MySQL PHP JavaScript ASP Photoshop Articles FORUM . Contact us
    ©2000-2024 plus2net.com All rights reserved worldwide Privacy Policy Disclaimer