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…') |
Nancyvarkey (Talk | contribs) |
||
Line 69: | Line 69: | ||
| 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, | ||
Line 121: | Line 121: | ||
'''<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;"| Now let us comment | + | | style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding:0.097cm;"| Now let us comment the following lines |
Type | Type | ||
Line 170: | Line 170: | ||
It shows, | It shows, | ||
− | Sum of a and b is 7.00 | + | '''Sum of a and b is 7.00''' |
− | Product of a and b is 10.00 | + | and |
+ | |||
+ | '''Product of a and b 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 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''' 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. | ||
+ | |||
+ | For real division, one of the operands has to be cast as '''float'''. | ||
− | |||
|- | |- | ||
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 '''Integer Division of 5 and 2 is 2.00''' | |
− | + | ||
− | + | ||
− | + | ||
− | We have | + | |
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 | + | Then we have '''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 we use the same code in C++, too | + | Let see can we 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++ arithmetic.cpp -o 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''' | '''./ arith''' | ||
Line 392: | Line 393: | ||
|- | |- | ||
| 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 identical. | So, we see that the results are identical. |
Revision as of 16:03, 13 December 2013
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
C++. |
Slide 2
|
In this tutorial, we will learn:
Arithmetic operators + Addition: eg. a+b. - Subtraction: eg. a-b. / Division: eg. a/b. * Multiplication: eg. a*b. % 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. 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 the 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
/*
|
Now let us comment the following lines
Type /* */ |
Click on Save | Click on Save to save the program.
Save the file with extension .c I have saved the 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 ./incr |
To compile, type the following on the terminal
gcc arithmetic.c -o arith Press Enter To execute the code, type ./arith |
press Enter | |
Output | The output is displayed on the screen.
It shows, Sum of a and b is 7.00 and Product of a and b 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 from 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.
For real division, one of the operands has to be cast as float.
|
return 0;
} |
Type return 0 and close the ending curly bracket. |
Click on Save | Click on Save. |
Switch back to the terminal. | Coming back to the terminal to compile and execute the code. |
Type
Type ./incr |
To compile, type the following on the terminal
gcc arithmetic.c -o arith To execute the code, type ./arith |
Output | The output is displayed on the screen:
We have the previous outputs of addition and multiplication operators. We have Integer Division of 5 and 2 is 2.00 We can see that in integer division the fractional part is truncated. Then we have 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 can we 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++ arithmetic.cpp -o arith Press Enter. |
On the terminal type
./ arithmetic |
To execute the code, type
./ arith |
Here the output is displayed.
So, we see that the results are identical. 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. |