Lesson # 4
Performing arithmetic operations
In C++, we can perform arithmetic operations such as
addition (+), subtraction (-), multiplication (*) and division ( / ). The
symbols (+,-,*,/) of these operations are known as arithmetic operators. Addition
to these operators there is also a modulus operator % in C++ which returns the
remainder. All these operators are binary operator which means they need two
operands. Operators are used to combine expressions. For example, cout<<
20 + 40 will return 60 as output. In similar way, we can subtract by using -,
multiply by using * (asterisk) , divide by using / (slash) and get remainder by
using % (percentage). We can performs arithmetic operations both on constant
literals and variables for example cout<<55 + 5 and cout<<x + y.
using namespace std;
#include<iostream>
main()
{
cout<<20+40;
cout<<endl;
int x=20, y=15;
cout<<x+y;
cout<<endl;
cout<<x-y;
cout<<endl;
cout<<x*y;
cout<<endl;
cout<<x/y;
cout<<endl;
cout<<x%y;
cout<<"\n";
system("pause");
}
Although, the code is very simple let’s discuss it.
using namespace std;
#include<iostream>
main()
{
cout<<20+40; //
Displays the result (sum) two numbers i.e. 20 + 40
cout<<endl; //
Inserts a new line / Move the cursor to next line
int x=20, y=15; //
Declares and initializes the variable x with value 20 and variable y with 15
cout<<x+y; //
Displays the sum of two variables values
i.e. values of x and y.
cout<<endl; //
Inserts a new line / Move the cursor to next line
cout<<x-y; //
Displays the subtraction result of x minus y.
cout<<endl; //
Inserts a new line / Move the cursor to next line
cout<<x*y; // Displays the
multiplication result of x times y. Note that * multiplies two
operands.
cout<<endl; //
Inserts a new line / Move the cursor to next line
cout<<x/y; // Displays the
division result of x divided by y. Mind it, that division
operator truncates output as it returns only whole part of the answer and
ignore the fractional part. In this case 20 / 15 = 1.33 but complier returns
whole part of the answer i.e. 1.
cout<<x%y; // Displays the
remainder after dividing the left side operand on right side operand. In this
case, the compliers returns only remainder i.e. 5.
cout<<"\n";
system("pause");
}
Code & Output:
Order of Precedence / Priority of operators:
The order in which operators are computed is called the
order of precedence or priority. In C++ each operator has a priority in which
it is calculated, the multiply (*) has first priority, division has the second
priority, addition and subtraction has third and fourth priority respectively.
For example in the
expression 3+5-5*2/2, Multiplication
(5*2) is computed first which gives 10 as result, then division 10/ 2 which returns 5, now addition is performed 3+5
= 8 and at last subtraction is carried out 8 – 5 so the final result of the
expression is 3. We can enforce the complier to perform a specific operation
first by putting parenthesis. For example in (9+3+2)*4/2+3 expression (9+3+2) are computed first because
of the parenthesis around them. An example can make it even clearer.
using namespace std;
#include<iostream>
main()
{
cout<<3+5-5*2/2;
cout<<endl;
cout<<9+3+2*4/2+3;
cout<<endl;
cout<<(9+3+2)*4/2+3;
cout<<"\n";
system("pause");
}
The code is simple and straight forward; anyhow let’s do a
little discussion about it.
using namespace std;
#include<iostream>
main()
{
cout<<3+5-5*2/2; // Displays result of expression
3+5-5*2/2 which is 3, C++ and other programming languages follow the natural
order of precedence in arithmetic operations. The word PEMDAS (Parenthesis,
Exponentiation, Multiplication, Division, Addition, Subtraction) can remind the
natural order of precedence.
cout<<endl; //
Move the cursor to next line.
cout<<9+3+2*4/2+3; //
Displays result of the expression 9+3+2*4/2+3 which is 19.
cout<<endl; //
Mover the cursor to next line.
cout<<(9+3+2)*4/2+3; // Displays the result of expression
(9+3+2)*4/2+3 in which (9+3+2) is evaluated first because of parenthesis.
cout<<"\n";
system("pause");
}
Code & Output:
Constant Variables
A constant variable is a variable which is initialized at
time of declaration and once the value has assigned, it does not change for
life time of the program. It is a good programming practice to use constant
where the value of variable does not need to change. The keyword const is used to
declare a variable as constant. We must assign a value to constant variable at
time of declaration, otherwise later the complier will not let us to change the
value of the constant. Let’s write a program which calculates the average of
given numbers.
using namespace std;
#include<iostream>
main()
{
int num1, num2,num3,num4,sum;
const int totalnum=4;
double average;
num1=50;
num2=52;
num3=55;
num4=54;
sum=num1+num2+num3+num4;
average=sum/totalnum;
cout<<average;
cout<<"\n";
system("pause");
}
Let’s discuss how this code works to find out average.
using namespace std;
#include<iostream>
main()
{
int num1, num2,num3,num4,sum; // Declares five variables
const int
totalnum=4; //
Declares a constant int variable totalnum and initializes it with literal 4.
Remember that value of a const variable can’t be change once it is initialized.
double average; //
Declares double variable average
num1=50; //
Assigns 50 to num1 variable
num2=52; //
Assigns 52 to num2 variable
num3=55; //
Assigns 55 to num3 variable
num4=54; //
Assigns 54 to num4 variable
sum=num1+num2+num3+num4; // Assigns the sum of
num1+num2+num3+num4 to sum variable
average=sum/totalnum; //
Calculates the average i.e. dividing sum by totalnum;
cout<<average; //
Displays the average on the screen
cout<<"\n";
system("pause");
}
Code & Output:
Doing Math in C++
There is a cmath library which provides us with advance
mathematical functions such as pow() function used for power. In similar way,
the Sqrt() function returns the square root of a number. To use these
mathematical functions, we have to include cmath library using
#include<cmath>.
using namespace std;
#include<iostream>
#include<cmath>
main()
{
int x=45;
double y=5;
cout<<"The power of 5 : "
<<pow(y,2)<<endl;
cout<<"The square root of 25 : "
<<sqrt(25)<<endl;
cout<<"The absoluate value of -133 : "
<<abs(-133)<<endl;;
cout<<"The sin of x (45) : "
<<sin(x)<<endl;
cout<<"The tan of x (45) : "
<<tan(x)<<endl;
system("pause");
}
Here is the discussion of the code line by line.
using namespace std;
#include<iostream>
#include<cmath> //
Includes c standard math library i.e. cmath
main()
{
int x=45; //
declares and initializes x variable
double y=5; //
declares and initializes y variable
cout<<"The
power of 5 : " <<pow(y,2)<<endl; // pow() function calculates the power of y variable, which takes
two arguments, first argument y as base and second argument as power. In our
case y is base and 2 is power.
cout<<"The
square root of 25 : " <<sqrt(25)<<endl; // sqrt() function calculates the square root of the number
25
cout<<"The
absoluate value of -133 : " <<abs(-133)<<endl; // abs() function calculates the absolute value for
passing value.
cout<<"The
sin of x (45) : " <<sin(x)<<endl; // sin() function calculates the sin value of passing value
cout<<"The
tan of x (45) : " <<tan(x)<<endl; // tan() function calculates the tan value of passing value
system("pause");
}
No comments:
Post a Comment