Difference between revisions of "C-and-C++/C2/Arithmetic-Operators/English"
(3 intermediate revisions by the same user not shown) | |||
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 75: | Line 75: | ||
'''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 122: | 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;"| We will comment the following lines | + | | 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 146: | 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 | | 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 | + | '''gcc space arithmetic.c space -o space arith''' |
'''Press Enter''' | '''Press Enter''' | ||
Line 171: | Line 170: | ||
It shows, | It shows, | ||
− | '''Sum of | + | '''Sum of 5 and 2 is 7.00''' |
and | and | ||
− | '''Product of | + | '''Product of 5 and 2 is 10.00''' |
|- | |- | ||
Line 197: | 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 239: | Line 238: | ||
'''printf" | '''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. | | 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 251: | 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 261: | 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 280: | 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 | + | We have the '''Integer Division of 5 by 2 is 2.00''' |
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 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 294: | 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 380: | Line 379: | ||
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'''. | Press '''Enter'''. | ||
Line 390: | Line 389: | ||
| 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 | | 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''' |
+ | Press '''Enter'''. | ||
|- | |- | ||
Line 396: | Line 396: | ||
| 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 423: | 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. |