Showing posts with label operators. Show all posts
Showing posts with label operators. Show all posts

C++ Operators

 

   :) Hello Reader..  Good  Day!/Morning!/Evening! to you. (According to the time you are reading this post). You are welcome to "The inspirational blog for programming", inspire++. Today we are going to make some cool , amazing & simple programs and spend some good time together. :) So.. Come with me for the next 20 minutes.

     We know how to create variables. Can you remember? Yes it is really simple. All you have to do is just three things.


  1. Decide what type of variable we want. (ex: It may be int , float , string , char or boolean)
  2. Leave a Space & Decide a name for our variable. (ex: name , age , height , etc)
  3. Finally, Assign a value to it. 
ex: int age;
     age = 25;
     Here first we decided the type of the variable ( int ), secondly we gave it a name ( age ), and finally we assigned a value to it. ( 25 ).

     Though we did this in 2 lines we could do this in a single line too.
ex: int age=25;     float height = 6.1; string name = "shehan"; 

     Now if we want to print those values , we can do that by using   cout   function,
ex: cout<< "My name is "<< name<<" and I'm "<<height<<" ft tall "<<endl;

     Ok. As you can see in above example , you can assign values to your variables. But I feel it is not enough. Don't you feel the same? If the only thing we can do with variables is just assigning values and that's all? I'm not happy with that..!! We want to do things with variables. Right?!

     So is it possible? If so.., then what  else we can do with variables? How can we do that?.. Well! that's where we need operators...

     " C++ Operators " are the bad guys in the language, who can get things done. They are really cool. They can easily manipulate any variable or values to do different operations.

     C++ has different types of operators to do different different things to our variables. 



  • Arithmetic Operators
  • Relational Operators
  • Logical Operators
  • Bitwise Operators
  • Assignment Operators
  • Misc Operators


     Let's think we want to add the values of  two variables. We can use + addition , which is an Arithmetic operator.

ex: int a=5; int b=10; int c;
       c=a+b;
       cout<<c<<endl;
     There are specific operator for each Arithmetic operation in C++.

  • +     -     Addition.
  • -      -     Substraction.
  • *     -     Multiplication.
  • /      -     Devision. 
  • %    -    Modulas operator
  • ++  -    Increment Operator
  • --     -    Decrement Operator
     Ok...   Now I want you to turn on your Code Blocks and Start to Code with me Some Programs..

Ok..!! 
Now... Let's do a cool exercise using operators. Now what I want you to do is create a simple calculator program. How to do that? Use the practice you have..

Here's what you need to do.

     In your program, you should take two integer numbers as inputs. You can do this using cin function. Then your program should return the addition, subtraction, multiplication, devision of those two numbers.

If you find that hard please follow this video.




Great.. :)  Now you know how to use arithmetic operators. In our next post we can discuss about other 4 types of operators. 

     Before I go, Here's a challenge for you, 
          create two integer variables in a program int a , b ; , Assign two different values to them,  now type a code to assign the value of  b to variable a and value of a to variable b, without using third variable.  If you can comment your answer down comments section. If  you couldn't I will reveal you the answer in next post. Until then bye bye. :)

Life of a Systems Engineer

  Hello, my dear readers. ✌😀✋ I'm talking to you after a very long time. And I am thrilled to talk-to and hear-from you after all thi...