Arithmetic Operators in c++ | Coding for beginners | Lecture 6

  • Guys in lecture 5  we studied about types of operators in C++ .Now we will go in deep of them one by one.
  • Currently we study the Arithmetic operators in C++ in this lecture .
  • Arithmetic operators are divided into two parts in C++ .They  are 
    1. Unary Operators
    2. Binary Operators
  • In these to first we will learn about Binary Operators because many of us are familiar with them

  • Binary Operators

  • Binary operator are operators between two values or variables .Confused?? don't worry be with patience I'll explain with examples .
  • Eg.1.,int z = 6 + 9 ; | Here in this example '+' is adding two numbers so this is called Binary Operator .
  • As we discussed in previous lectures some of those arithmetic operators comes under Binary Operator .They are  { +, - , *, /, %,etc., } .
  • Currently the main thing be need to know is Binary operators returns a value .Again confused?? don't worry ,continue reading .
  • It means in above Eg.1., '+' operator added two  numbers 6 , 9 and given the result to z . In coding we call this as returning values . Hence we can say that in above example '+' operator returned a value to z that value is nothing but 15 .So, z = 15
  • Similarly we can use can use { - ,* , / } but %  is different from {+  - , * , /} . 
  • '%' is also called as modulo operator .
  • What is Modulo Operator ' %' ??
  • A modulo operator always returns remainder  ,if confused see below example
  • Eg.2., int m = 7 % 4 ; in this example m will be equals to 3 because if we divide 7 by 4 then we will get remainder 3 ,Hence m = 3  ,
  • Some more examples for you .
    • int k = 10 % 6; 
    • int L = 23 % 11;
    • Here k = 4 and L =1;
  • Still confusion ?? comment in our comment box ,we will explain some more .
  • Now we will move to Unary Operators .

  • Unary Operators

  • Unary Operators are completely different from Binary Operators ,So guys please concentrate.
  • As we know that Binary operator works on two values or variables in above concept ,here Unary Operators works on only one variable .How ?? Confused?? don't worry continue reading you will understand .
  • As we discussed in previous lectures some of those arithmetic operators comes under Unary Operators .They are  { ++ , -- } .
  • Many of us know + , - but what are these " ++ , -- " ??
  • These are also called increment operator (++) and decrement operator (--) .
  • What is Increment Operator ??
  • Increment operator is one of the unary operator which is used to increase the value of a variable. Don't confuse now we will provide examples.
    • Eg.3., int b = 20 ;
      • b++;
    • In above example variable b stores value , after that we applied increment operator to b like b++ .So b value increases by 1 and final b value is 21
    • Similarly eg.3.,  int z = 9 ;
      • z++;
    • So final z value is 10 .
  • We can also place ++ infront of b and z like ++b and ++z .But there is small difference which we will explain in future lectures ,present  you think ++b and b++ are same .
  • What is Decrement Operator ??
  • Decrement operator is one of the unary operator which is used to decrease the value of a variable. Don't confuse now we will provide examples.
    • Eg.4., int b = 20 ;
      • b--;
    • In above example variable b stores value after that we applied increment operator to b like b++ .So value increases by 1 and final b value is 19
    • Similarly eg.5.,  int z = 9 ;
      • z--;
    • So final z value is 8 .
  • We can also place -- in front of b and z like --b and --z .But there is small difference which we will explain in future lectures ,present  you think --b and b-- are same .
  • Thus a Unary Operator works only on a single variable also we can observe a difference that Unary Operator won't return a value like Binary Operator as explained in Eg.1., above.
  • Guys below we provided a code please go through it. Don't worry if you got confused I'll also provide an compilation video below it

  1. #include<bits/stdc++.h>
  2. using namespace std;

  3. int main()
  4. {
  5.     int a = 47;
  6.     int b = 6;
  7.     
  8.     cout<<"Biary Operators"<<endl;
  9.     
  10.     int plu_op = a + b;
  11.     int min_op = a - b;
  12.     int mul_op = a * b;
  13.     int div_op = a / b;
  14.     int mod_op = a % b;
  15.     
  16.     cout<<plu_op<<endl<<min_op<<endl;
  17.     cout<<mul_op<<endl<<div_op<<endl;
  18.     cout<<mod_op<<endl;;
  19.     
  20.     cout<<"Unary Operators"<<endl;
  21.      a++;
  22.      --b;
  23.      
  24.      cout<<a<<endl<<b<<endl;
  25.      return 0;


*****Don't worry if you are confused you will know everything in further concepts ,please read previous lectures too,Don't give up if you are confused . Don't Panic ,be patient we will learn everything*****

***** If you have any doubts please ask in comment box*****


 

Guys if you have any doubts, please let me know

Post a Comment (0)
Previous Post Next Post

How to start Coding | coding for beginners | Lecture 1