Mathematics & Computer Programming: Toqeer Mahmood

Download as pptx, pdf, or txt
Download as pptx, pdf, or txt
You are on page 1of 15

Mathematics & Computer

Programming

Toqeer Mahmood
Dept. of Civil Engg.
UET, Taxila
Computer System
Key Board’s Keys Description
Lecture
#
03
The “Define” Directive
It is a preprocessor directive.
It is used to define a constant quantity.
It is used at the beginning of the program.

Syntax:
◦ # define identifier constant
where
◦ identifier it specifies the character to which the constant
value is to be assigned
◦ constant it specifies the constant value that is to be assigned to
the identifier
e.g.
◦ # define p 3.1429
Compound Assignment Statement
It can be used to assign one value to many
variables.
e.g. int x, y, z;
x = y = z = 20;
20 will be assigned to all the variables
declared (i.e. x, y, z)
Comment Statement
It is a non-executable statement.
It is used to add remarks or comments in a program.
Comments are usually given to explain logic of the program.
The compiler ignores the comments given in the program.
Double forward slashes “//” are used to mark the single line
comments statement.
We uses /* ….. Statements ….. */ for multiline comments
e.g.
// This is my first program in c++
// This program is about comment statement.

/* This is my first program in c++


This program is about multi-line comment statement. */
Comment Statement
Sample Program
#include <iostream.h>
#include <conio.h>
int main ()
{
clrscr();
cout << "Hello World! \n” ; // this statement will prints Hello World!
cout << “This is a C++ program” ; /* this statement will This is a C++ program */
getch();
return 0;
}

Output:
Hello World!
This is a C++ program
The Escape Sequence
Special non-printing characters which are used to control
printing on the output device are called escape sequence.
These characters are written inside the “ ” quotes and are
not printed when used in the statement.
An escape sequence is a combination of backslash ‘ \ ’ and
a code character. (‘\’ is known as control character)
These characters are used inside string constants or
independently.
e.g. cout<<“This lecture is about escape sequence \n”;
An escape sequence can be used anywhere in the string,
1) at the beginning, 2) in the middle or 3) at the end of
the string.
e.g. cout<<“\n This lecture is about \n escape sequence \n”;
The Escape Sequence
Escape Sequence Explanation
\a (alert or alarm) This character produced a beeb sound
\b (backspace) This character shifts the print control on the output
device to move one space back
e.g. cout<<“First\b”;
cout<<“Year”;
Output: FirsYear
\t (tab) This character creates 5 character spaces on output
device. e.g. cout<<“A\tB\tC\tD\tE\tF”;
Output: A B C D E F
\n (new line) This character generates and shift’s the control to the
new line.
e.g. cout<<“\n This lecture is about \n escape sequence
”;
Output: This lecture is about
escape sequence
The Escape Sequence
Escape Sequence Explanation
\f (form feed) Used to print the output on the printer after feeding a
blank form
\r (carriage return) It moves the cursor to the beginning of the current line
\\ (backslash Used to print a backslash
character) e.g. cout <<“C:\\Folder”;
Output: C:\Folder
\” (quotation mark) Used to print a double quotation mark
e.g. cout <<“ \” Folder”;
Output: “Folder
\’ (quotation mark) Used to print a single quotation mark
e.g. cout <<“ \’ Folder”;
Output: ‘Folder
Prefix and Postfix Operator
Prefix Operator
◦ When an increment operator is used in prefix
mode in an expression
 Increment Operator (++)
 Add 1 to the value of the variable before the value of the
variable is used in the expression. e.g. ++a
 Decrement Operator (--)
 Subtract 1 to the value of the variable before the value of
the variable is used in the expression. e.g. --a
Prefix and Postfix Operator
Postfix Operator
 When an increment operator is used in postfix mode
in an expression
 Increment Operator (++)
 Add 1 to the value of the variable after the value of the
variable is used in the expression. e.g. a++
 Decrement Operator (--)
 Subtract 1 to the value of the variable after the value of the
variable is used in the expression. e.g. a--
Prefix and Postfix Operator
Sample Program
#include <iostream.h>
#include<conio.h>
void main()
{
clrscr();
int a, b, c, sum;
a=1;
b=2;
c=3;
sum=a+b+(++c); Output
cout <<“Sum = ” <<sum; Sum = 7
cout<<“C = ”<<c; C=4
sum=a+b+(c--)
cout <<“Sum = ” <<sum; Sum = 7
cout<<“C = ”<<c; C=3
getch();
}
Lab Work
 Input two values at runtime and write a program to compute
addition, subtraction, multiplication, division and remainder
 Write a program to find the perimeter of a circle i.e.
Perimeter = 2πr (Where π=3.1429)
 Write a program to compute and print the volume of a
cylinder. Input Radius “r” and height “h” at runtime
[ Vol = π h r2 ]
 Write a program to get your name and age in year. Calculate
your age in months and print your name and age in months.
 For input a value in a variable at runtime:
 e.g. cin >> variable name;
cin >> a;

You might also like