Operators and Compound Assignment

explanation of various types of operators in C and also on compound assignment, and various ways in which programs can be shortened.

Last Reviewed and Updated on February 7, 2020
Posted by Parveen(Hoven),
Aptitude Trainer and Software Developer

My C/C++ Videos on Youtube

Here is the complete playlist for video lectures and tutorials for the absolute beginners. The language has been kept simple so that anybody can easily understand them. I have avoided complex jargon in these videos.

What is an operator

Operators are used to perform operations on the operand(s). For example, we can use addition operator to add two numbers. A subtraction operator can be used to find the difference between two numbers.

Assignment Operator

Assignment operator is used to assign or copy the value from the right side to it's left side.
int x = 99;
In the above code, the value 99 is being assigned to x.

Unary Operator

Unary operators act on only one operand. For example, in the code below the sign of i is being reversed by the unary minus or negation operator.

int j = 99;
 
// unary negation of i
int k = -i;
 
cout << k << endl;
 

Binary operators

They act on two operands. Some of the binary operators are addition(+), subtraction, etc., common algebraic operators.

Ternary Operators

They have three operands. In the code below the value stored in x is 7, while the value stored in y is 5.

int p = 20;
 
int x = (p > 10 ? 7 : 5);
 
int y = (p < 10 ? 7 : 5);
 
The ternary operator has three operands: op1 ? op2 : op3. If the value of op1 is true, then the result is op2, otherwise the result is op3.

Modulus Operator

The modulus operator is used to find the remainder after division. In the code below, when 8 is divided by 3, the remainder is 2.

int x = 8 % 3;

cout << x << endl; // prints 2
Modulus operator can be used to test divisibility of a number by another. If we want to test whether a number is even, then we can calculate it's mod 2. If it is zero, then the number is divisible by 2, and therefore, it is even.

int x = 9;
 
int rem = x % 2;
 
if (0 == rem)
{
    cout << "Number is even" << endl;
}
else
{
    cout << "Number is odd" << endl;
}
The above code can be written with the ternary operator also.

int x = 9;
 
cout << (0 == x % 2 ? "Even" : "Odd") << endl;

Compound Assignment

The operator += is called the compound assignment addition operator. The usage is shown below.

int x = 9;
 
// same as x = x + 9
x += 8;
 
// prints 17
cout << x << endl;
On the same lines we have other compound assignments like -=, *=, /=, etc.,

Increment and Decrement Operators

The operators ++ and -- are, respectively, called the increment and decrement operators. They are used to increase or decrease the value of their operand by 1. The operator ++ is same as += 1, and the operator -- is same as -= 1.

int x = 7;
 
x++;
 
// prints 8
cout << x << endl;
 
int y = 88;
 
y--;
 
// prints 87
cout << y << endl;

Prefix and Postfix Increment and Decrement

The increment and decrement operators can be prefixed or suffixed to their operand. When the operator is suffixed, it becomes postfix operator. And when it is prefixed, it becomes prefix operator.


int x = 99;

// post fix
x++;

// prefix
++x;

int y = 99;

// displays 100
cout << ++y << endl;

int z = 99;

// displays 99
cout << z++ << endl;


Use of operators

Write a program to Add, subtract, multiply, divide and mod two numbers
#include<iostream>
#include<cstdio>
 
using namespace std;
 
int main()
 
{ 
         
    int add; int sub; int mul; float div; int mod;
     
    add = 30 + 7; 
     
    cout << "Addition of two number = " << add <<endl;
 
    sub = 30 - 7; 
     
    cout << "Subtraction of two number = " << sub <<endl;
     
    mul = 30 * 7; 
     
    cout << "Multiplication of two number = " << mul <<endl;
     
    div = 30.0 / 7; 
     
    cout << "Division of two number = " << div <<endl;
     
    mod = 30 % 7; 
     
    cout << "Modular division of two numbers = " << mod <<endl;
     
    return 0; // exit point for compiler 
     
}

Operators

what will be the output of following expression ? 
 
 x = 4 + 2 * 12 % -3 ; Ans. 4 
#include<iostream>
#include<cstdio>
 
using namespace std; 
 
 int main( ) 
{
    
    float x; 
      
    x = 4 + 2 * 12 % - 3 ;
      
    cout << "Result is = " << x <<endl;
      
    return 0 ;
      
}

Expressions using operators

Program to calculate ( a2 - b2 ). Ask the user to enter two numbers, and display the difference of their squares by using the formula - (a + b) * (a - b).

#include<iostream>
#include<cstdio>
 
using namespace std; 
 
int main() 
{
    
    int a,b,c;
      
    printf("Enter 1st no. = ");
    
    cin >> a;
    
    cout << "Enter 2nd no. = ";
    
    cin >> b;
 
    c = ( a + b ) * ( a - b );
 
    printf("%d",c);
 
     return 0 ;
      
}

Expressions using operators

Write a program to calculate (a+b)2. Use the formula - a * a + b * b + 2 * a * b.

#include<iostream>
#include<cstdio>
 
using namespace std; 
 
int main() 
{
    
    int a,b,c;
      
    printf("Enter 1st no. = ");
    
    cin >> a;
    
    cout << "Enter 2nd no. = ";
    
    cin >> b;
 
    c = ( a * a ) + ( b * b ) + ( 2 * a * b );
 
     printf("%d",c);
 
     return 0 ;
      
}

Predict output

What will be the output of following equation. 
You will have to know operator precedence rules.
[DONT WRITE SUCH CONFUSING CODE IN PRACTICE
USE BRACKETS TO CLARIFY YOUR INTENT]

a = 2 , d = 7 , c = 13 , n = 2.5 

b = 6.6 / a + (2 * a + ( 30 - c ) / a * d ) / ( 2 / n ) 

Ans. 82.675 
#include<iostream>
#include<cstdio>

using namespace std; 
 
int main( )
{ 

    float a = 2 , d = 7 , c = 13 , n = 2.5 , b ; 
     
    b= 6.6 / a + (2 * a + (30 - c ) / a * d ) / ( 2 / n );
     
    cout << "Result is = " << b << endl;
     
    return 0;
 
}    

use of ++ , - - operators

Write program using following with example . pre increment, post increment , pre decrement , post decrement
#include<iostream>
#include<cstdio> 
 
using namespace std; 
 
int main() 
{
 
    int a = 5 ; 
     
    int b = 15 ; 
 
    cout << " Post increment = " << a++ << endl ; 
 
    cout << "a = " << a << endl ;
 
    cout << "Pre increment, a is = " << ++a << endl;
 
    cout << "-------------- "<< endl <<endl; 
 
    cout << "b = 15 " << endl;
 
    cout << "Post decrement =" << b-- << endl ;
 
    cout << "b = " << b << endl ;
 
    cout << " Pre decrement for b is = " << --b << endl ;
 
    cout << " -------------- " << endl ;
 
    return 0;
 
}

Obtain output

Ramesh's basic salary is input though keyboard. His dearness allowance is 40% of basic salary and house rent allowance is 20% of basic salary. Write a program to calculate his gross salary. The formula to use = basic + (basic * 40)/100 + (basic * 20)/100
#include<iostream>
#include<cstdio> 

using namespace std; 
 
int main() 
{
 
    cout << " Enter salary = ";
 
    int basicSalary; 
    
    cin >> basicSalary;
 
    int dearnessAllow = (int)((basicSalary * 40) / 100);
 
    int houseRent = (int)((basicSalary * 20) / 100);
 
    int grossSalary = basicSalary + dearnessAllow + houseRent ;
 
    cout << " Gross Salary is = " << grossSalary;
 
    return 0 ;
 
}

Calculate sum

Ask the user to enter a five digit number. Split the number and display the sum of individual digits.
#include<iostream>
#include<cstdio>
 
using namespace std ;
 
int main() 
{ 
 
    cout << "Enter 5 digit number = ";
 
    int num1; 
 
    cin >> num1;
 
    int a = num1 % 10;
 
    num1 = num1 / 10;
 
    int b = num1 % 10;
 
    num1 = num1 / 10;
 
    int c = num1 % 10;
 
    num1= num1 / 10;
 
    int d = num1 % 10;
 
    int last = num1 / 10;
 
    int sum = a + b + c + d + last;
 
    cout << "Total sum of digits is = " << sum ;
    
     return 0 ;
 
}

Calculate distance

Write a program to convert and print distance in meters, feet, inches and centimeters. The distance between two cities (in K.M.) is input though keyboard.
#include<iostream>
#include<cstdio> 
 
using namespace std; 
 
int main()
{
    
    cout << "Enter the distace in K.M = "; 
    
    int num1; cin>>num1; 
    
    double meters = num1 *1000;
    
    double feet = meters * 3.5;
    
    double inch = feet * 12 ;
    
    double centimeter = meters * 100 ;
        
    cout << "Distance in K.M = " << num1 <<endl;
    
    cout << "Distance in Meters = " << meters <<endl;
    
    cout << "Distance in Feet = " << feet<<endl;
    
    cout << "Distance in Inch = " << inch<<endl;
    
    cout << "Distance centimeter = " << centimeter ;
        
    return 0 ;
 
}

Classroom Training

Classroom training in C/C++ is available at our training institute. Click here for details. You can also register yourself here.

Video Tutorial

We have created a video tutorial that can be downloaded and watched offline on your windows based computer. It is in the form of an exe file. No installation is required, it runs by double clicking the file. Since the exe file is hosted on the Google Drive, it should be very safe because Google itself checks for any virus or malware. The video can be watched offline, no internet is required.

Pricing

This single video will cost you USD $5. When you download the video, you will be able to watch the first few minutes for free, as a sample. The video app will prompt you for payment through PayPal and other payment options. If you want ALL VIDEOS, NOT JUST THIS ONE then go here: Complete Set of C/C++ Video Tutorials The entire set is priced at USD $50.

Screenshots

This is a screenshot of the software that should give you a good idea of the available functionality. Click on the image to see a larger view.screenshot of the video being played in the software

Installation

This software doesn't require any installation. Just download and double-click to run it. It doesn't require administrative priviliges to run. It runs in limited access mode, so should be very safe.

supports windows xp and later Supported Operating Systems

These videos can be run on 32-bit as well as 64-bit versions of the Microsoft Windows Operating Systems. Windows XP and all later OS are supported. If you want to run it on Windows XP and Windows Vista, then you must also have the .NET Framework Version 3.5 installed on your machines. Those users who have Windows 7 and later donot have to worry because these operating systems already have the .NET Framework installed on them.

Download Links

IMPORTANT NOTE: This download contains ONLY ONE video. The video is about the topic of this tutorial.

Want ALL Videos? If you want ALL VIDEOS, NOT JUST THIS ONE then go here: Complete Set of C/C++ Video Tutorials. TIP: If you plan to buy these videos, then it would be more economical to buy the entire set.

DISCLAIMER: Even though every care has been taken in making this software bug-free, but still please take a proper backup of your PC data before you use it. We shall not be responsible for any damages or consequential damages arising out of the use of this software. Please use it solely at your own risk.

Please download the software here.
download the video Operators and Compound Assignment



Creative Commons License
This Blog Post/Article "Operators and Compound Assignment" by Parveen (Hoven) is licensed under a Creative Commons Attribution-NonCommercial-ShareAlike 4.0 International License.
Updated on 2020-02-07. Published on: 2015-10-05