Difference between revisions of "C-and-C++/C2/Arithmetic-Operators/English"
(Created page with ''''Title of script''': Arithmetic Operators in C and C++ '''Author: Ritwik Joshi''' '''Keywords: Arithmetic Operators, Video Tutorial''' {| style="border-spacing:0;" | style…') |
|||
(5 intermediate revisions by 2 users not shown) | |||
Line 13: | Line 13: | ||
|- | |- | ||
| style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:none;padding:0.097cm;"| Slide 1 | | style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:none;padding:0.097cm;"| Slide 1 | ||
− | | style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding:0.097cm;"| Welcome to the spoken tutorial on '''Arithmetic Operators in C ''' | + | | style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding:0.097cm;"| Welcome to the spoken tutorial on '''Arithmetic Operators in C ''' and |
'''C++'''. | '''C++'''. | ||
Line 24: | Line 24: | ||
| style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding:0.097cm;"| In this tutorial, we will learn: | | style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding:0.097cm;"| In this tutorial, we will learn: | ||
− | Arithmetic operators | + | Arithmetic operators like |
+ Addition: eg. a+b. | + Addition: eg. a+b. | ||
Line 33: | Line 33: | ||
<nowiki>* </nowiki>Multiplication: eg. a*b. | <nowiki>* </nowiki>Multiplication: eg. a*b. | ||
+ | and | ||
% Modulus: eg. a%b. | % Modulus: eg. a%b. | ||
Line 69: | Line 70: | ||
| style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding:0.097cm;"| Here is the C program for arithmetic operators. | | style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding:0.097cm;"| Here is the C program for arithmetic operators. | ||
− | In the first | + | In the first two statements the variables are declared and defined. |
In the next two statements, | In the next two statements, | ||
'''a''' is assigned the value of 5. | '''a''' is assigned the value of 5. | ||
− | + | and | |
'''b''' is assigned the value of 2. | '''b''' is assigned the value of 2. | ||
Line 96: | Line 97: | ||
| style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding:0.097cm;"| This '''printf''' statement displays the sum of a and b on the screen. | | style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding:0.097cm;"| This '''printf''' statement displays the sum of a and b on the screen. | ||
− | Here %.2f provides | + | Here %.2f provides a precision of two digits after the decimal point. |
|- | |- | ||
Line 121: | Line 122: | ||
'''<nowiki>*/</nowiki>''' | '''<nowiki>*/</nowiki>''' | ||
− | | style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding:0.097cm;"| | + | | style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding:0.097cm;"| We will comment out the following lines |
Type | Type | ||
Line 131: | Line 132: | ||
|- | |- | ||
| style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:none;padding:0.097cm;"| Click on '''Save''' | | style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:none;padding:0.097cm;"| Click on '''Save''' | ||
− | | style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding:0.097cm;"| Click on '''Save''' | + | | style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding:0.097cm;"| Click on '''Save''' |
Save the file with extension '''.c''' | Save the file with extension '''.c''' | ||
− | I have saved | + | I have saved my file as''' arithmetic.c''' |
|- | |- | ||
Line 145: | Line 146: | ||
− | '''gcc | + | '''gcc space arithmetic.c space -o space arith''' |
Type | Type | ||
+ | '''./arith''' | ||
+ | | style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding:0.097cm;"| To compile the code, type the following on the terminal | ||
− | + | '''gcc space arithmetic.c space -o space arith''' | |
− | + | ||
− | + | ||
− | '''gcc arithmetic.c -o arith''' | + | |
'''Press Enter''' | '''Press Enter''' | ||
Line 170: | Line 170: | ||
It shows, | It shows, | ||
− | Sum of | + | '''Sum of 5 and 2 is 7.00''' |
− | Product of | + | and |
+ | |||
+ | '''Product of 5 and 2 is 10.00''' | ||
|- | |- | ||
| style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:none;padding:0.097cm;"| Assignment | | style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:none;padding:0.097cm;"| Assignment | ||
− | | style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding:0.097cm;"| Now you should try the subtraction operator on your own | + | | style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding:0.097cm;"| Now you should try the subtraction operator on your own. |
Try replacing the addition operator with subtraction operator. | Try replacing the addition operator with subtraction operator. | ||
Line 194: | Line 196: | ||
Now, I will explain the code for division. | Now, I will explain the code for division. | ||
− | Remove the multi line comments | + | Remove the multi line comments here and here. |
|- | |- | ||
Line 203: | Line 205: | ||
− | | style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding:0.097cm;"| In these statements, '''c''' holds the value of integer division of '''a''' | + | | style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding:0.097cm;"| 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. | Please note that in integer division the fractional part is truncated. | ||
Line 219: | Line 221: | ||
| style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding:0.097cm;"| In this statement we are performing real division. | | style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding:0.097cm;"| In this statement we are performing real division. | ||
− | Here one of the operands has to be cast as '''float''' | + | Here one of the operands has to be cast as '''float'''. |
− | + | ||
− | + | ||
− | + | ||
− | + | ||
|- | |- | ||
Line 231: | Line 229: | ||
'''is %.2f\n",c);''' | '''is %.2f\n",c);''' | ||
− | | style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding:0.097cm;"| We have type-cast variable a. | + | | style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding:0.097cm;"| We have type-cast variable''' a'''. |
− | Now a will behave as a float | + | Now '''a''' will behave as a '''float''' variable for a single operation. |
+ | |||
+ | |- | ||
+ | | style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:none;padding:0.097cm;"| Highlight | ||
+ | |||
+ | '''printf" | ||
+ | | style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding:0.097cm;"| The''' printf statement''' displays the output of real division on the screen. | ||
|- | |- | ||
Line 243: | Line 247: | ||
|- | |- | ||
| style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:none;padding:0.097cm;"| Click on '''Save''' | | style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:none;padding:0.097cm;"| Click on '''Save''' | ||
− | | style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding:0.097cm;"| Click on '''Save.''' | + | | style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding:0.097cm;"| Now Click on '''Save.''' |
|- | |- | ||
Line 253: | Line 257: | ||
− | '''gcc | + | '''gcc arithmetic.c -o arith''' |
Type | Type | ||
− | '''./ | + | '''./arith''' |
− | | style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding:0.097cm;"| To compile, type | + | | style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding:0.097cm;"| To compile, type |
− | '''gcc arithmetic.c -o arith''' | + | '''gcc space arithmetic.c space -o space arith''' |
+ | |||
+ | '''Press Enter''' | ||
To execute the code, type | To execute the code, type | ||
'''./arith''' | '''./arith''' | ||
+ | '''Press Enter''' | ||
|- | |- | ||
Line 272: | Line 279: | ||
We have the previous outputs of addition and multiplication operators. | We have the previous outputs of addition and multiplication operators. | ||
− | + | We have the '''Integer Division of 5 by 2 is 2.00''' | |
− | + | ||
− | + | ||
− | + | ||
− | We have the Integer Division of 5 | + | |
We can see that in integer division the fractional part is truncated. | 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 | + | Then we have the '''Real Division of 5 and 2 is 2.50''' |
In real division the result is as expected. | In real division the result is as expected. | ||
Line 290: | Line 293: | ||
| style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding:0.097cm;"| Now suppose, I want to write the same program in C++. | | style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding:0.097cm;"| Now suppose, I want to write the same program in C++. | ||
− | Let see can | + | Let see if I can use the same code in C++, too. |
|- | |- | ||
Line 300: | Line 303: | ||
|- | |- | ||
| style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:none;padding:0.097cm;"| Click on the C++ file in the editor | | style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:none;padding:0.097cm;"| Click on the C++ file in the editor | ||
− | | style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding:0.097cm;"| Here | + | | style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding:0.097cm;"| Here we have the C++ code. |
|- | |- | ||
Line 372: | Line 375: | ||
'''g++ arithmetic.cpp -o arithmetic''' | '''g++ arithmetic.cpp -o arithmetic''' | ||
− | | style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding:0.097cm;"| Let's execute the code and | + | | style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding:0.097cm;"| Let's execute the code and see what results we get. |
− | Open the terminal and type | + | Open the '''terminal''' and type |
− | '''g++ arithmetic.cpp -o arith''' | + | '''g++ space arithmetic.cpp space -o space arith''' |
− | ''' | + | Press '''Enter'''. |
|- | |- | ||
Line 384: | Line 387: | ||
'''./ arithmetic''' | '''./ arithmetic''' | ||
− | | style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding:0.097cm;"| | + | | style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding:0.097cm;"| To execute the code, type |
− | + | '''./arith''' | |
− | + | Press '''Enter'''. | |
− | '''./ arith''' | + | |
|- | |- | ||
| style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:none;padding:0.097cm;"| | | style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:none;padding:0.097cm;"| | ||
− | | style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding:0.097cm;"| Here the output is displayed | + | | style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding:0.097cm;"| Here the output is displayed. |
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | So, we see that the results are | + | So, we see that the results are similar to the C program. |
The only difference is in the precisions of outputs. | The only difference is in the precisions of outputs. | ||
Line 429: | Line 423: | ||
'''About the Spoken Tutorial Project''' | '''About the Spoken Tutorial Project''' | ||
− | * Watch the video available at [http://spoken-tutorial.org/What_is_a_Spoken_Tutorial http://spoken-tutorial.org/What_is_a_Spoken_Tutorial] | + | * Watch the video available at the following link [http://spoken-tutorial.org/What_is_a_Spoken_Tutorial http://spoken-tutorial.org/What_is_a_Spoken_Tutorial] |
* It summarises the Spoken Tutorial project | * It summarises the Spoken Tutorial project |
Latest revision as of 16:00, 5 February 2014
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 space 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. |