C-and-C-Plus-Plus/C2/Arithmetic-Operators-in-C-and-C-Plus-Plus/English-timed

From Script | Spoken-Tutorial
Jump to: navigation, search
Time' Narration


00.02 Welcome to the spoken tutorial on Arithmetic Operators in C 'C++'.
00.08 In this tutorial, we will learn about
00.10 Arithmetic operators like

+ Addition: eg. a+b.

- Subtraction: eg. a-b.

/ Division: eg. a/b.

*  Multiplication: eg. a*b.

% Modulus: eg. a%b.

00.28 To record this tutorial, I am using:Ubuntu 11.10 as the operating system
00.33 gcc and g++ Compiler version 4.6.1 in Ubuntu.
00.39 I will now demonstrate the use of these arithmetic operations with the help of a C program.
00.45 I have already written the program.
00.47 So I will open the editor and explain the code.
00.50 Here is the C program for arithmetic operators.
00.57 the first two statements the variables are declared and defined.
01.03 In the next two statements,
01.05 a is assigned the value of 5.
01.07 b is assigned the value of 2.
01.11 Now let's see how the addition operator works.
01.15 c holds the sum of a and b.
01.19 This printf statement displays the sum of a and b on the screen.
01.29 Here %.2f provides the precision of two digits after the decimal point.
01.37 In the next statement, c holds the product of a and b.
01.43 This printf statement displays the product of a and b on the screen.
01.49 Let's see how these two operators work.
01.53 We will comment out the following lines
01.56 Type /* */
02.05 Click on 'Save
02.08 Save the file with extension .c
02.11 I have saved the file as arithmetic.c
02.15 Open the terminal window by pressing Ctrl, Alt and T keys simultaneously.
02.23 compile the code, type the following on the terminal.
02.27 gcc arithmetic.c -o arith
02.38 Press Enter
02.41 To execute the code, type ./arith
02.48 press Enter
0250 The output is displayed on the screen.
02.53 It shows,
02.54 Sum of 5 and 2 is 7.00 and
02.59 Product of 5and 2 is 10.00
03.05 Now you should try the subtraction operator on your own
03.09 Try replacing the addition operator with subtraction operator.
03.14 You should get the result as 3.
03.19 Coming back to the program and the last set of statements.
03.23 Now, I will explain the code for division.
03.26 Remove the multi line comments from here and here.
03.35 In these statements, c holds the value of integer division of a by b.
03.41 Please note that in integer division the fractional part is truncated.
03.47 The printf statement displays the division output on the screen.
03.58 In this statement we are performing real division.
04.02 Here one of the operands has to be cast as float
04.10 We have type-cast variable a.
04.14 Now a will behave as a float varible for a single operation.


04.24 The printf statement displays the output of real division on the screen.


04.31 Type return 0 and close the ending curly bracket.
04.37 Click on Save.
04.40 Coming back to the terminal to compile and execute the code.
04.45 To compile, typegcc arithmetic.c -o arith. Press Enter
04.59 To execute the code, type ./arithPress Enter


05.05 The output is displayed on the screen:
05.08 We have the previous outputs of addition and multiplication operators.
05.17 we have the integer Division of 5 by 2 is 2.


05.22 We can see that in integer division fractional part is truncated.


05.29 then we have the real division of 5 by 2 is 2.50.
05.35 In real division the result is as expected.
05.38 We used type-casting to obtain these result.
05.45 Now suppose, I want to write the same program in C++.
05.50 Let see if i can use the same code in C++, too?
05.55 Let's find out.
05.56 Let me go back to the editor.
06.01 Here is a C++ code.
06.05 Notice that the header is different from the C file header.
06.13 namespace is also used here.
06.19 Also, notice that the output statement in C++ is cout.
06.25 So, apart from these differences, the two codes are very similar.
06.32 Click on Save.
06.33 Make sure the file is saved with the extension .cpp
06.37 I have saved my file as arithmetic.cpp
06.42 Let's execute the code and seewhat results we get.
06.49 Open the terminal and type g++ arithmetic.cpp -o arith .'Press Enter
07.09 To execute the code Type ./ arithPress Enter


07.17 Here the output is displayed:
07.19 So, we see that the results are similar to C program.
07.23 The only difference is in the precisions of outputs.
07.30 Let us now summarize the tutorial.
07.32 In this tutorial we learnt how to use the arithmetic operators.
07.37 As an assignment:
07.38 Write a program to demonstrate the use of modulus operator.
07.43 Please note that Modulus operator finds the remainder of division. eg. c = a % b;
07.51 You should obtain the result as 1.
07.55 Watch the video available at the following link
07.58 It summarises the Spoken Tutorial project
08.01 If you do not have good bandwidth, you can download and watch it


08.05 The Spoken Tutorial Project Team . Conducts workshops using spoken tutorials
08.10 Gives certificates for those who pass an online test
08.14 For more details, please write to contact at spoken hyphen tutorial dot org


08.21 Spoken Tutorial Project is a part of the Talk to a Teacher project
08.25 It is supported by the National Mission on Education through ICT, MHRD, Government of India
08.31 More information on this Mission is available at
08.33 spoken hyphen tutorial dot org slash NMEICT hyphen Intro


08.41 This is Ritwik Joshi from IIT Bombay.

Thank you for joining.

Contributors and Content Editors

Sneha