Difference between revisions of "C-and-C++/C2/Arithmetic-Operators/English"
Nancyvarkey (Talk | contribs) |
|||
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 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 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 150: | Line 151: | ||
'''./incr''' | '''./incr''' | ||
− | | 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 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 arithmetic.c -o arith''' | + | '''gcc space arithmetic.c space -o arith''' |
'''Press Enter''' | '''Press Enter''' |
Revision as of 15:22, 30 January 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. 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 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 ./incr |
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 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. |