C-and-C++/C2/Arithmetic-Operators/English
Title of script: Arithmetic Operators in C and C++
Author: Ritwik Joshi
Keywords: Arithmetic Operators, Video Tutorial
|
|
Slide 1 | Welcome to the spoken tutorial on Arithmetic Operators in C and
C++. |
Slide 2
|
In this tutorial, we will learn:
Arithmetic operators like + Addition: eg. a+b. - Subtraction: eg. a-b. / Division: eg. a/b. * Multiplication: eg. a*b. and % Modulus: eg. a%b. |
Slide 3
|
To record this tutorial, I am using:
Ubuntu 11.10 as the operating system and gcc and g++ Compiler version 4.6.1 in Ubuntu. |
I will now demonstrate the use of these arithmetic operations with the help of a C program. | |
Switch to the file arithmetic.c in gedit | I have already written the program.
So I will open the editor and explain the code. |
Highlight
int a,b; float c; a=5; b=2; |
Here is the C program for arithmetic operators.
In the first two statements the variables are declared and defined. In the next two statements, a is assigned the value of 5. and b is assigned the value of 2. |
Highlight
c=a+b;
|
Now let's see how the addition operator works.
c holds the sum of a and b. |
Highlight
printf("Addition of a and b is %.2f\n",c); |
This printf statement displays the sum of a and b on the screen.
Here %.2f provides a precision of two digits after the decimal point. |
Highlight
c=a*b; |
In the next statement, c holds the product of a and b. |
Highlight
printf("Multiplication of a and b is %.2f\n",c); |
This printf statement displays the product of a and b on the screen.
Let's see how these two operators work. |
Type
/*
|
We will comment out the following lines
Type /* */ |
Click on Save | Click on Save
Save the file with extension .c I have saved my file as arithmetic.c |
press Ctrl, Alt and T keys simultaneously. | Open the terminal window by pressing Ctrl, Alt and T keys simultaneously. |
Type
Type ./arith |
To compile the code, type the following on the terminal
gcc space arithmetic.c space -o arith Press Enter To execute the code, type ./arith |
press Enter | |
Output | The output is displayed on the screen.
It shows, Sum of 5 and 2 is 7.00 and Product of 5 and 2 is 10.00 |
Assignment | Now you should try the subtraction operator on your own.
Try replacing the addition operator with subtraction operator. You should get the result as 3. |
Switch back to the editor
|
Coming back to the program and the last set of statements.
Now, I will explain the code for division. Remove the multi line comments here and here. |
Highlight
c=a/b;
|
In these statements, c holds the value of integer division of a by b.
Please note that in integer division the fractional part is truncated. |
Highlight
printf("Integer Division of a and b is %.2f\n", c); |
The printf statement displays the division output on the screen. |
Highlight
c=(float)a/b; |
In this statement we are performing real division.
Here one of the operands has to be cast as float. |
Highlight
printf("Real Division of a and b is %.2f\n",c); |
We have type-cast variable a.
Now a will behave as a float variable for a single operation. |
Highlight
printf" |
The printf statement displays the output of real division on the screen. |
return 0;
} |
Type return 0 and close the ending curly bracket. |
Click on Save | Now Click on Save. |
Switch back to the terminal. | Coming back to the terminal to compile and execute the code. |
Type
Type ./arith |
To compile, type
gcc space arithmetic.c space -o space arith Press Enter To execute the code, type ./arith Press Enter |
Output | The output is displayed on the screen:
We have the previous outputs of addition and multiplication operators. We have the Integer Division of 5 by 2 is 2.00 We can see that in integer division the fractional part is truncated. Then we have the Real Division of 5 and 2 is 2.50 In real division the result is as expected. We used type-casting to obtain these result. |
Now suppose, I want to write the same program in C++.
Let see if I can use the same code in C++, too. | |
Switch back to the editor | Let's find out.
Let me go back to the editor. |
Click on the C++ file in the editor | Here we have the C++ code. |
// Arithmetic Operators in C++
#include <iostream> using namespace std; |
Notice that the header is different from the C file header.
namespace is also used here. |
int main()
{ int a,b; float c; a=5; b=2; c=a+b; cout <<"Addition of a and b is " <<c <<"\n"; c=a*b; cout <<"Multiplication of a and b is " <<c <<"\n"; c=a/b; cout <<"Integer Division of a and b is " <<c <<"\n"; c=(float)a/b; cout <<"Real Division of a and b is " <<c <<"\n"; return 0; } |
Also, notice that the output statement in C++ is cout. |
So, apart from these differences, the two codes are very similar. | |
Click on Save.
point to .cpp |
Click on Save.
Make sure the file is saved with the extension .cpp I have saved my file as arithmetic.cpp |
On the terminal type
g++ arithmetic.cpp -o arithmetic |
Let's execute the code and see what results we get.
Open the terminal and type g++ space arithmetic.cpp space -o space arith Press Enter. |
On the terminal type
./ arithmetic |
To execute the code, type
./arith Press Enter. |
Here the output is displayed.
So, we see that the results are similar to the C program. The only difference is in the precisions of outputs. | |
Summary: | Let us now summarize the tutorial.
In this tutorial we learnt how to use the arithmetic operators. |
Assignment: | As an assignment:
Write a program to demonstrate the use of modulus operator. Please note that Modulus operator finds the remainder of division. eg. c = a % b; You should obtain the result as 1. |
Slide 4
About the Spoken Tutorial Project
|
* Watch the video available at the following link
|
Slide 5
Spoken Tutorial Workshops The Spoken Tutorial Project Team
|
The Spoken Tutorial Project Team * Conducts workshops using spoken tutorials
|
Slide 6
Acknowledgement
|
Spoken Tutorial Project is a part of the Talk to a Teacher project
|
Remain on previous slide
No slide for this part |
This is Ritwik Joshi from IIT Bombay.
Thank you for joining. |