Saturday, June 17, 2023

C++ Operators

 

C++ Operators

  • Arithmetic Operators
  • Relational Operators
  • Logical Operators
  • Bitwise Operators
  • Assignment Operator
  • Unary operator
  • Ternary or Conditional Operator
  • scope resolution operator

   C++ Arithmetic Operators

Operator

Operation

+

Addition

-

Subtraction

*

Multiplication

/

Division

%

Modulo Operation (Remainder after division)

 

Arithmetic Operators

#include <iostream.h>

#include<conio.h>

 

void main() {

int a, b;

clrscr();

cout<<"Enter a & b value ";

cin>>a>>b;

cout << "a + b = " << (a + b) << endl;

cout << "a - b = " << (a - b) << endl;

cout << "a * b = " << (a * b) << endl;

cout << "a / b = " << (a / b) << endl;

cout << "a % b = " << (a % b) << endl;

getch();

}

Output

Enter a & b value

5  3

a + b = 8

a - b = 2

a * b = 15

a / b = 1

a % b = 2

Conditional Operator

 

(condition)  ?  true_value  :  false_value;

 

#include <iostream.h>

#include <conio.h>

void main()

{

    int a=10,b=5,c;

    c=(a>b) ? a : b;

    cout<<"Greatest number is "<<c;

    getch();

}

Output

Greatest number is 10

C++ Assignment Operators

Operator

Example

Equivalent to

+=

a += 3;

a = a + 3;

-=

a -= 2;

a = a - 2;

*=

a *= 3;

a = a * 3;

/=

a /= 2;

a = a / 2;

 

#include <iostream.h>

#include<conio.h>

 

void main() {

int a, b;

clrscr();

cout<<"Enter a & b value ";

cin>>a>>b;

cout << "a + = " << (a +=3 ) << endl;

cout << "a - = " << (a - =2) << endl;

cout << "a * = " << (a * =3) << endl;

cout << "a / = " << (a / =2) << endl;

getch();

}

Output

Enter a & b value

5 

a +  = 8

a -  = 6

a *  = 18

a /  = 9

Relational Operators

operator

Meaning

Example

==

Is Equal To

3 == 5 gives us false

!=

Not Equal To

3 != 5 gives us true

> 

Greater Than

3 > 5 gives us false

< 

Less Than

3 < 5 gives us true

>=

Greater Than or Equal To

3 >= 5 give us false

<=

Less Than or Equal To

3 <= 5 gives us true

 

 

#include <iostream.h>

#include<conio.h>

 

void main() {

int a, b;

clrscr();

cout<<"Enter a & b value ";

cin>>a>>b;

cout << "a < b = " << (a < b) << endl;

cout << "a <= b = " << (a <= b) << endl;

cout << "a > b = " << (a > b) << endl;

cout << "a >= b = " << (a >= b) << endl;

cout << "a == b = " << (a == b) << endl;

cout << "a != b = " << (a != b) << endl;

getch();

}

Output

Enter a & b value

5  3

a < b = 0

a <= b = 0

a > b = 1

a >= b = 1

a == b = 0

a != b = 1

C++ Logical Operators

Logical operators are used to check whether an expression is true or false. If the expression is true, it returns 1 whereas if the expression is false, it returns 0.

Operator

Example

Meaning

&&

expression1 && expression2

Logical AND.
True only if all the operands are true.

||

expression1 || expression2

Logical OR.
True if at least one of the operands is true.

!

!expression

Logical NOT.
True only if the operand is false.

 

#include <iostream.h>

#include<conio.h>

 

void main() {

int a, b;

clrscr();

cout<<"Enter a & b value ";

cin>>a>>b;

cout << ((a==5)&&(b==3)) << endl;

cout << ((a==5)||(b==3)) << endl;

cout << (!a) << endl;

getch();

}

Output

Enter a & b value

0

1

0

C++ Bitwise Operators

Operator

Description

&

Binary AND

|

Binary OR

^

Binary XOR

~

Binary One's Complement

<< 

Binary Shift Left

>> 

Binary Shift Right

 

#include <iostream.h>

#include<conio.h>

 

void main() {

int a, b;

clrscr();

cout<<"Enter a & b value ";

cin>>a>>b;

cout << "a & b = " << (a & b) << endl;

cout << "a | b = " << (a | b) << endl;

cout << "a ^ b = " << (a ^ b) << endl;

cout << "~a = " << (~a) << endl;

cout << "a <<2 = " << (a<<2 ) << endl;

cout << "a >>2 = " << (a>>2 ) << endl;

getch();

}

Output

Enter a & b value

2  3

a & b = 2

a | b = 3

a ^ b = 1

~a = -3

a <<2 =8

a>>2  =0

 Scope Resolution Operator

    #include <iostream.h>  

    #include<conio.h>

// declare global variable  

     int a= 50;  

     void main ()  

{  

// declare local variable   

      int a= 100;  

  

// print the value of the variables  

     cout << " The value of the local variable a: " << a;  

  

     // use scope resolution operator (::) to access the global variable   

     cout << "\n The value of the global variable a: " << ::a;   

    getch();  

      }  

Output

The value of the local variable a: 100

 The value of the global variable a: 50

 



















Unary Operator

 

 

 

 

PreIncrement                            Post  Increment

int a=5                                       int a=5

int b=++a                                   int b=a++

b=6                                            b=5

a=6                                            a=6

Predecrement                          Post  decrement

int a=5                                       int a=5

int b=--a                                   int b=a--

b=4                                            b=5

a=4                                            a=4

 

PreIncrement

#include <iostream.h>

#include <conio.h>

void main()

{

    int a=5,b=++a;

    clrscr();

    cout<<"b="<<b<<endl<<"a="<<a;

    getch();

}

Post Increment

#include <iostream.h>

#include <conio.h>

void main()

{

    int a=5,b=a++;

    clrscr();

    cout<<"b="<<b<<endl<<"a="<<a;

    getch();

}

Predecrement

#include <iostream.h>

#include <conio.h>

void main()

{

    int a=5,b=--a;

    clrscr();

    cout<<"b="<<b<<endl<<"a="<<a;

    getch();

}

Post decrement

#include <iostream.h>

#include <conio.h>

void main()

{

    int a=5,b=a--;

    clrscr();

    cout<<"b="<<b<<endl<<"a="<<a;

    getch();

} 
















C++ Manipulators

#include<iomanip.h>

example:1

#include<iostream.h>

#include<iomanip.h>

#include<conio.h>

void main(void)

{

int a,b;

a=100;b=200;

cout<<a<<b;

cout<<a<<'\t'<<b;// one tab space inserted in output

cout<<setw(5)<<a<<setw(5)<<b<<endl;

cout<<setw(6)<<a<<setw(6)<<b<<endl;

cout<<setw(7)<<a<<setw(7)<<b<<endl;

getch();

}

output

200300

200 300

     2       0     0                3   0     0

      2       0   0                3   0      0

               2    0    0                   3   0    0

example:2

#include<iostream.h>

#include<iomanip.h>

#include<conio.h>

void main(void)

{

int a,b;

a=100;b=200;

cout<<setfill('*');

cout<<setw(5)<<a<<setw(5)<<b<<endl;

cout<<setw(6)<<a<<setw(6)<<b<<endl;

cout<<setw(7)<<a<<setw(7)<<b<<endl;

getch();

}

output

* *     2   0  0    *    *   3   0    0

*  *     *   2  0    0    *   *   *    3    0    0

*   *     *   *  2    0    0   *   *    *  *    3   0  0 

example:3

#include<iostream.h>

#include<iomanip.h>

#include<conio.h>

void main()

{

float a,b,c;

a=5;b=3;c=a/b;

cout<<setprecision(1)<<c<<endl;

cout<<setprecision(2)<<c<<endl;

cout<<setprecision(3)<<c<<endl;

cout<<setprecision(4)<<c<<endl;

cout<<setprecision(5)<<c<<endl;

cout<<setprecision(6)<<c<<endl;

getch();

}

output:

1.7

1.67

1.667

1.6667

1.66667

1.666667


0 comments:

Post a Comment