Introduction To Programming
Introduction To Programming
INTRODUCTION TO PROGRAMMING
Name: ________________________________________
Batch: ________________________________________
FederalUrduUniversityArts,Science&Technology Page2
Contents
Exp No List of Experiments
FederalUrduUniversityArts,Science&Technology Page3
Experiment No 1
FederalUrduUniversityArts,Science&Technology Page4
Experiment No 2
FederalUrduUniversityArts,Science&Technology Page5
Experiment 3
Command Prompt Commands
FederalUrduUniversityArts,Science&Technology Page6
FederalUrduUniversityArts,Science&Technology Page7
Experiment 4
FederalUrduUniversityArts,Science&Technology Page8
FederalUrduUniversityArts,Science&Technology Page9
FederalUrduUniversityArts,Science&Technology Page10
FederalUrduUniversityArts,Science&Technology Page11
FederalUrduUniversityArts,Science&Technology Page12
FederalUrduUniversityArts,Science&Technology Page13
FederalUrduUniversityArts,Science&Technology Page14
FederalUrduUniversityArts,Science&Technology Page15
FederalUrduUniversityArts,Science&Technology Page16
Experiment No 6
FederalUrduUniversityArts,Science&Technology Page17
FederalUrduUniversityArts,Science&Technology Page18
FederalUrduUniversityArts,Science&Technology Page19
FederalUrduUniversityArts,Science&Technology Page20
FederalUrduUniversityArts,Science&Technology Page21
FederalUrduUniversityArts,Science&Technology Page22
Experiment No 7
FederalUrduUniversityArts,Science&Technology Page23
PROGRAM#1Printingnameandstarpattern
#include<iostream.h>
voidmain()
cout<<"\nImran\n";
cout<<"\t\n*\n";
cout<<"\t\n***\n";
cout<<"\t\n*****\n";
cout<<"\t\n***\n";
cout<<"\t\n*";
FederalUrduUniversityArts,Science&Technology Page24
Experiment No 8
FederalUrduUniversityArts,Science&Technology Page25
PROGRAMSumoftwonumbers
#include<iostream.h>
voidmain()
inta,b;
cout<<"THISPROGRAMISFORADDITIONOFTWONUMBERS:\n\n";
cout<<"ENTER1stNUMBER";
cin>>a
cout<<"ENTER2ndNUMBER";
cin>>b;
cout<<"\n"<<a<<"+"<<b<<"="<<a+b;
FederalUrduUniversityArts,Science&Technology Page26
Experiment No 9
Objective:
To understand the use of If and Else statement.
Theory:
Before discussing the actual structure of the if statement, let us examine
the meaning of TRUE and FALSE in computer terminology. A true statement is one
that evaluates to a nonzero number. A false statement evaluates to zero. When you
perform comparison with the relational operators, the operator will return 1 if the
comparison is true, or 0 if the comparison is false. For example, the check 0 == 2
evaluates to 0. The check 2 == 2 evaluates to a 1. If this confuses you, try to use a cout
statement to output the result of those various comparisons (for example cout<< ( 2 ==
1 );)
When programming, the aim of the program will often require the checking of one
value stored by a variable against another value to determine whether one is larger,
smaller, or equal to the other.
if (condition)
{
// code to execute if condition is true
FederalUrduUniversityArts,Science&Technology Page27
}
else
{
// code to execute if condition is false
}
#include<iostream.h>
void main()
int a,b,c;
cout<<"SIMPLE CALCULATOR\n\n";
cout<<"\n1.ADDITION\n2.MULTIPLICATION\n3.SUBTRACTION\n4
.DIVITION\n";
cin>>a>>b>>c;
if(c==1)
FederalUrduUniversityArts,Science&Technology Page28
else if(c==2)
else if(c==3)
else if(c==4)
FederalUrduUniversityArts,Science&Technology Page29
Experiment No 10
C++ Programming: Switch
Objectives:
To be able to deal with multiple conditions.
Theory:
if and if/else statements can become quite confusing when nested too
deeply, and C++ offers an alternative. Unlike if, which evaluates one
value, switch statements allow you to branch on any of a number of different values.
The general form of the switch statement is:
switch (expression)
{
case valueOne: statement;
break;
case valueTwo: statement;
break;
....
case valueN: statement;
break;
default: statement;
}
expression is any legal C++ expression, and the statements are any legal C++
statements or block of statements. switch evaluates expression and compares the result
to each of the case values. Note, however, that the evaluation is only for equality;
relational operators may not be used here, nor can Boolean operations.
If one of the case values matches the expression, execution jumps to those statements
and continues to the end of the switch block, unless a break statement is encountered. If
nothing matches, execution branches to the optional default statement. If there is no
default and there is no matching value, execution falls through the switch statement and
the statement ends.
It is important to note that if there is no break statement at the end of a case statement,
execution will fall through to the next case statement. This is sometimes necessary, but
usually is an error. If you decide to let execution fall through, be sure to put a
comment, indicating that you didn't just forget the break.
This C++ program illustrates use of the switch statement.
FederalUrduUniversityArts,Science&Technology Page30
Program:
1: //
2: // Demonstrates switch statement
3:
4: #include <iostream.h>
5:
6: int main()
7: {
8: unsigned short int number;
9: cout << "Enter a number between 1 and 5: ";
10: cin >> number;
11: switch (number)
12: {
13: case 0: cout << "Too small, sorry!";
14: break;
15: case 5: cout << "Good job!\n"; // fall through
16: case 4: cout << "Nice Pick!\n"; // fall through
17: case 3: cout << "Excellent!\n"; // fall through
18: case 2: cout << "Masterful!\n"; // fall through
19: case 1: cout << "Incredible!\n";
20: break;
21: default: cout << "Too large!\n";
22: break;
23: }
24: cout << "\n\n";
25: return 0;
26: }
Analysis:
The user is prompted for a number. That number is given to
the switch statement. If the number is 0, the case statement on line 13 matches, the
message Too small, sorry! is printed, and the break statement ends the switch. If the
value is 5, execution switches to line 15 where a message is printed, and then falls
through to line 16, another message is printed, and so forth until hitting the break on
line 20.
FederalUrduUniversityArts,Science&Technology Page31
The net effect of these statements is that for a number between 1 and 5, that many
messages are printed. If the value of number is not 0-5, it is assumed to be too large,
and the default statement is invoked on line 21.
The syntax for the switch statement is as follows:
switch (expression)
{
case valueOne: statement;
case valueTwo: statement;
....
case valueN: statement
default: statement;
}
The switch statement allows for branching on multiple values of expression. The
expression is evaluated, and if it matches any of the case values, execution jumps to
that line. Execution continues until either the end of the switch statement or
Example 1:
switch (choice)
{
case 0:
cout << "Zero!" << endl;
break
case 1:
cout << "One!" << endl;
break;
case 2:
cout << "Two!" << endl;
default:
cout << "Default!" << endl;
FederalUrduUniversityArts,Science&Technology Page32
Example 2:
switch (choice)
choice 0:
choice 1:
choice 2:
break;
choice 3:
break;
default:
FederalUrduUniversityArts,Science&Technology Page33
Experiment No 11
FederalUrduUniversityArts,Science&Technology Page34
Example 1:
#include<iostream.h>
void main()
for(int a=1;a<=5;a++)
cout<<"\Yasir";
FederalUrduUniversityArts,Science&Technology Page35
Example 2:
FederalUrduUniversityArts,Science&Technology Page36
Example 3:
Example 4:
#include <iostream>
using namespace std;
int main ()
{
int n;
for (n=10; n>0; n--)
FederalUrduUniversityArts,Science&Technology Page37
{
cout << n << ", ";
if (n==3)
{
cout << "countdown aborted!";
break;
}
}
return 0;
FederalUrduUniversityArts,Science&Technology Page38
Experiment No 12
FederalUrduUniversityArts,Science&Technology Page39
void main()
int a=5;
while(a>=1)
cout<<"\nArsalan";
a--;
#include<iostream.h>
void main()
int a=0;
do
cout<<"\nArsalan";
a++;
while(a<=4);
FederalUrduUniversityArts,Science&Technology Page40
Program:
#include <iostream>
using namespace std;
int main ()
{
int n;
cout << "Enter the starting number > ";
cin >> n;
while (n>0) {
cout << n << ", ";
--n;
}
FederalUrduUniversityArts,Science&Technology Page41
Experiment No 13
Objective:
To understand the working of array.
Theory:
An array is a series of elements of the same type placed in contiguous memory
locations that can be individually referenced by adding an index to a unique identifier.
That means that, for example, we can store 5 values of type int in an array without
having to declare 5 different variables, each one with a different identifier. Instead of
that, using an array we can store 5 different values of the same type, int for example,
with a unique identifier.
For example, an array to contain 5 integer values of type int called billy could be
represented like this:
where each blank panel represents an element of the array, that in this case are integer
values of type int. These elements are numbered from 0 to 4 since in arrays the first
index is always 0, independently of its length.
Like a regular variable, an array must be declared before it is used. A typical
declaration for an array in C++ is:
type name [elements];
where type is a valid type (like int, float...), name is a valid identifier and
the elements field (which is always enclosed in square brackets []), specifies how
many of these elements the array has to contain.
Therefore, in order to declare an array called billy as the one shown in the above
FederalUrduUniversityArts,Science&Technology Page42
Initializing arrays
When declaring a regular array of local scope (within
a function, for example), if we do not specify otherwise, its elements will not be
initialized to any value by default, so their content will be undetermined until we store
some value in them. The elements of global and static arrays, on the other hand, are
automatically initialized with their default values, which for all fundamental types this
means they are filled with zeros.
In both cases, local and global, when we declare an array, we have the possibility to
assign initial values to each one of its elements by enclosing the values in braces { }.
For example:
The amount of values between braces { } must not be larger than the number of
elements that we declare for the array between square brackets [ ]. For example, in the
example of array billy we have declared that it has 5 elements and in the list of initial
values within braces { } we have specified 5 values, one for each element.
When an initialization of values is provided for an array, C++ allows the possibility of
leaving the square brackets empty [ ]. In this case, the compiler will assume a size for
the array that matches the number of values included between braces { }:
FederalUrduUniversityArts,Science&Technology Page43
After this declaration, array billy would be 5 ints long, since we have provided 5
initialization values.
For example, to store the value 75 in the third element of billy, we could write the
following statement:
billy[2] = 75;
and, for example, to pass the value of the third element of billy to a variable called a,
we could write:
a = billy[2];
Therefore, the expression billy[2] is for all purposes like a variable of type int.
Notice that the third element of billy is specified billy[2], since the first one is billy[0],
the second one is billy[1], and therefore, the third one isbilly[2]. By this same reason,
its last element is billy[4]. Therefore, if we write billy[5], we would be accessing the
sixth element of billy and therefore exceeding the size of the array.
In C++ it is syntactically correct to exceed the valid range of indices for an array. This
can create problems, since accessing out-of-range elements do not cause compilation
errors but can cause runtime errors. The reason why this is allowed will be seen
further ahead when we begin to use pointers.
At this point it is important to be able to clearly distinguish between the two uses that
brackets [ ] have related to arrays. They perform two different tasks: one is to specify
FederalUrduUniversityArts,Science&Technology Page44
the size of arrays when they are declared; and the second one is to specify indices for
concrete array elements. Do not confuse these two possible uses of brackets [ ] with
arrays.
If you read carefully, you will see that a type specifier always precedes a variable or
array declaration, while it never precedes an access.
Some other valid operations with arrays:
1 billy[0] = a;
2 billy[a] = 75;
3 b = billy [a+2];
4 billy[billy[a]] = billy[2] + 5;
Program:
// arrays example
#include <iostream>
using namespace std;
Multidimensional arrays
Multidimensional arrays can be described as "arrays of arrays". For example, a bi
dimensional array can be imagined as a bi dimensional table made of elements, all of
them of a same uniform data type.
FederalUrduUniversityArts,Science&Technology Page45
jimmy represents a bi dimensional array of 3 per 5 elements of type int. The way to
declare this array in C++ would be:
and, for example, the way to reference the second element vertically and fourth
horizontally in an expression would be:
jimmy[1][3]
FederalUrduUniversityArts,Science&Technology Page46
Experiment No 14
C++ Programming: Function
Objective:
To know the operation of function.
Theory:
Functions are building blocks of the programs. They make the programs
more modular and easy to read and manage. All C++ programs must contain the
function main( ). The execution of the program starts from the function main( ). A
C++ program can contain any number of functions according to the needs. The general
form of the function is: -
return_type function_name(parameter list)
The function of consists of two parts function header and function body. The function
header is:-
The return type specifies the type of the data the function returns. The return type can
be void which means function does not return any data type. The function name is the
name of the function. The name of the function should begin with the alphabet or
underscore. The parameter list consists of variables separated with comma along with
their data types. The parameter list could be empty which means the function do not
contain any parameters. The parameter list should contain both data type and name of
the variable. For example,
FederalUrduUniversityArts,Science&Technology Page47
is the function header of the function factorial. The return type is of integer which
means function should return data of type integer. The parameter list contains two
variables n and j of type integer and float respectively. The body of the function
performs the computations.
Function Declaration:
The variables name need not be same as the variables of parameter list of the
function. Another method can be
The variables in the function declaration can be optional but data types are necessary.
Function Arguments:
return expression;
FederalUrduUniversityArts,Science&Technology Page48
The expression evaluates to a value which has type same as the return type specified in
the function declaration. For example the statement,
return(n);
is the return statement of the factorial function. The type of variable n should be
integer as specified in the declaration of the factorial function. If a function has return
type as void then return statement does not contain any expression. It is written as:-
return;
The function with return type as void can ignore the return statement. The closing
braces at the end indicate the exit of the function. Here is a program which illustrates
the working of functions.
Program:
#include<iostream>
int main ()
{
int n1,fact;
cout <<"Enter the number whose factorial has to be calculated" << endl;
cin >> n1;
fact=factorial(n1);
cout << "The factorial of " << n1 << " is : " << fact << endl;
return(0);
}
int factorial(int n)
{
int i=0,fact=1;
if(n<=1)
FederalUrduUniversityArts,Science&Technology Page49
{
return(1);
}
else
{
for(i=1;i<=n;i++)
{
fact=fact*i;
}
return(fact);
}
}
#include <iostream.h>
return (a+b);
return (a-b);
FederalUrduUniversityArts,Science&Technology Page50
int g;
g = (*functocall)(x,y);
return (g);
int main ()
int m,n;
return 0;
FederalUrduUniversityArts,Science&Technology Page51
Experiment No 15
C++ Programming: Pointer
Theory:
A pointer is a variable that is used to store a memory address. The
address is the location of the variable in the memory. Pointers help in allocating
memory dynamically. Pointers improve execution time and saves space. Pointer
points to a particular data type. The general form of declaring pointer is:-
type *variable_name;
type is the base type of the pointer and variable_name is the name of the variable of
the pointer. For example,
int *x;
Pointer Operators
There are two important pointer operators such as * and
&. The & is a unary operator. The unary operator returns the address of the
memory where a variable is located. For example,
int x*;
int c;
x=&c;
variable x is the pointer of the type integer and it points to location of the variable c.
When the statement
x=&c;
is executed, & operator returns the memory address of the variable c and as a result
x will point to the memory location of variable c.
The * operator is called the indirection operator. It returns the contents of the
memory location pointed to. The indirection operator is also called deference
operator. For example,
int x*;
FederalUrduUniversityArts,Science&Technology Page52
int c=100;
int p;
x=&c;
p=*x;
variable x is the pointer of integer type. It points to the address of the location of the
variable c. The pointer x will contain the contents of the memory location of variable
c. It will contain value 100. When statement
p=*x;
is executed, * operator returns the content of the pointer x and variable p will
contain value 100 as the pointer x contain value 100 at its memory location. Here is a
program which illustrates the working of pointers.
Program:
#include<iostream>
int main ()
{
int *x;
int c=200;
int p;
x=&c;
p=*x;
cout << " The address of the memory location of x : " << x << endl;
cout << " The contents of the pointer x : " << *x << endl;
cout << " The contents of the variable p : " << p << endl;
return(0);
FederalUrduUniversityArts,Science&Technology Page53
Program # 1
#include <iostream.h>
int main ()
int numbers[5];
int * p;
p = numbers;
*p = 10;
p++;
*p = 20;
p = &numbers[2];
*p = 30;
p = numbers + 3;
*p = 40;
p = numbers;
*(p+4) = 50;
return 0;
FederalUrduUniversityArts,Science&Technology Page54
Exercise 16
Project List
8. Electric Bill
9. Salary Calculation
FederalUrduUniversityArts,Science&Technology Page55
FederalUrduUniversityArts,Science&Technology Page56