0% found this document useful (0 votes)
6 views6 pages

C++ Program

The document provides an overview of various operators in C++, including arithmetic, compound assignment, relational, logical, and increment/decrement operators. It includes example code snippets demonstrating the usage of these operators. Each section illustrates how the operators function and their effects on variable values.

Uploaded by

tibemekonen
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
6 views6 pages

C++ Program

The document provides an overview of various operators in C++, including arithmetic, compound assignment, relational, logical, and increment/decrement operators. It includes example code snippets demonstrating the usage of these operators. Each section illustrates how the operators function and their effects on variable values.

Uploaded by

tibemekonen
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 6

1.

Arithmetic Operators
These operators can operate on any arithmetic operations in C++. Let's have a look at an example

#include <iostream>
using namespace std;
main() {
int a = 21;
int b = 10;
int c ;
c = a + b;
cout << "Line 1 - Value of c is :" << c << endl ;

c = a - b;
cout << "Line 2 - Value of c is :" << c << endl ;

c = a * b;
cout << "Line 3 - Value of c is :" << c << endl ;

c = a / b;
cout << "Line 4 - Value of c is :" << c << endl ;

c = a % b;
cout << "Line 5 - Value of c is :" << c << endl ;
return 0;
}
2, Compound assignment operators

1. #include <iostream>

int main() {
int a = 10;
int b = 5;

std::cout << "Initial values: a = " << a << ",


b = " << b << std::endl;

// Addition assignment
a += b; // a = a + b
std::cout << "a += b: a = " << a << std::endl;

// Subtraction assignment
a -= b; // a = a - b
std::cout << "a -= b: a = " << a << std::endl;

// Multiplication assignment
a *= b; // a = a * b
stdenda::cout << "a *= b: a = " << a <<
std::endl;

// Division assignment
a /= b; // a = a / b
std::cout << "a /= b: a = " << a << std::endl;

// Modulus assignment
a %= b; // a = a % b
std::cout << "a %= b: a = " << a << std::endl;

return 0;
}
2.
3, the Relational Operators
// CPP Program to demonstrate the Relational Operators
#include <iostream>
using namespace std;

int main()
{
int a = 6, b = 4;

// Equal to operator
cout << "a == b is " << (a == b) << endl;

// Greater than operator


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

// Greater than or Equal to operator


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

// Lesser than operator


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

// Lesser than or Equal to operator


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

// true
cout << "a != b is " << (a != b) << endl;

return 0;
}
4. Logical Operators(!, &&, ||)
// CPP Program to demonstrate the Logical Operators
#include <iostream>
using namespace std;

int main()
{
int a = 6, b = 4;

// Logical AND operator


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

// Logical OR operator
cout << "a || b is " << (a || b) << endl;

// Logical NOT operator


cout << "!b is " << (!b) << endl;

return 0;
}
5. Increment/Decrement Operators: (++) and (--)
When an increment or decrement is used as part of an expression, there is an important
difference in prefix and postfix forms. If you are using prefix form then increment or decrement
will be done before rest of the expression, and if you are using postfix form, then increment or
decrement will be done after the complete expression is evaluated.

#include <iostream>
using namespace std;

main() {
int a = 21;
int c ;

// Value of a will not be increased before assignment.


c = a++;
cout << "Line 1 - Value of a++ is :" << c << endl ;

// After expression value of a is increased


cout << "Line 2 - Value of a is :" << a << endl ;

// Value of a will be increased before assignment.


c = ++a;
cout << "Line 3 - Value of ++a is :" << c << endl ;
return 0;
}

You might also like