Laboratory Manual C++: Programming Fundamentals CSCO-1201
Laboratory Manual C++: Programming Fundamentals CSCO-1201
Laboratory Manual C++: Programming Fundamentals CSCO-1201
Abstract
Programming Fundamental all labs and practice are available in this manual and we will learn
all the content by practically every student having a responsibility to perform all the labs at
their own and ask any question under sign.
shoaib nawaz
[email protected]
Table of Content
Lab-01 .................................................................................................................................. 1
Lab-02 ................................................................................................................................ 11
Lab-03 ................................................................................................................................ 22
Lab-04 ................................................................................................................................ 35
Lab-05 ................................................................................................................................ 47
Lab-06 ................................................................................................................................ 57
Lab-07 ................................................................................................................................ 73
Lab-08 ................................................................................................................................ 85
Lab-09 ................................................................................................................................ 96
Lab-10 .............................................................................................................................. 107
Lab-11 .............................................................................................................................. 116
Lab-12 .............................................................................................................................. 122
Lab-13 .............................................................................................................................. 135
Lab-14 .............................................................................................................................. 145
Lab-15 .............................................................................................................................. 152
Programming
Fundamentals
Lab-01
Dev C ++ is the most famous and easy to use IDE for learning C++. It has all you need for
C++ development. IDE is nothing but Integrated Development Environment in which one can
develop, run, test and debug the program. The C++ Developing Environment is a screen
display with windows and pull-down menus. The program listing, error messages and other
information are displayed in separate windows. The menus may be used to invoke all the
operations necessary to develop the program, including editing, compiling, debugging and
program execution.
Figure 1
Statement terminator
is called. All functions use braces to indicate the beginning and end of their
definitions.
Line 5: Body of main function (std::cout<<"Hello World!"; )
This statement has three parts: First, std::cout, which identifies the standard
character output device (usually, this is the computer screen).
Second, the insertion operator (<<), which indicates that what follows is inserted
into std::cout. Finally, a sentence within quotes ("Hello world!"), is the content
inserted into the standard output.
Notice that the statement ends with a semicolon (;). All C++ statements must end
with a semicolon character.
Statement terminator (;)
A programming statement must be terminated by a semi-colon (;), just like an
English sentence ends with a period.
Line 6: return 0;
The return value of 0 indicates normal termination; while non-zero (typically 1) indicates
abnormal termination. C++ compiler will automatically insert a " return 0;" at the end of
the the main() function, thus this statement can be omitted.
Note: First make a folder to save the source files. We can make this folder of any
name and locate it in any directory. For example: Make a new folder named
“Executables” and save it on the desktop. All the source files we make in Dev
C++, will be saved in this folder. This is done once.
To Save, Compile and Run the program, press the menu bar button. Dev C++
will show an option to save the program first, Save the Source file in the folder we
created earlier. After saving the file the program will automatically compile and
run to give the following output.
1.6. Exiting Dev C++ IDE
A Program window may be closed in a number of different ways. You can click on
the menu bar button to close the source file, you can select close from the file
menu, or you can press the short cut key Ctrl + W.
To exit from the IDE, select Exit from the File Menu or press Alt+f4, or press the
close button on top right corner of the IDE.
1.6.1. Program No 1
Compile & Run the following code, and write its output:
#include<iostream>
int main()
{
std::cout<<"welcome";
}
1.6.2. Program No 2
Code of Program No 2:
1.6.3. Program No 3
Write a C ++ Program in Dev C++ to produce the following output without using
the std:: prefix.
Hello World!!!
I am a C++ Program
Hint: Use “using namespace std;” after the preprocessor directive line.
Code of Program No 3:
Code of Program No 4:
1.6.5. Program No 5
Write a C++ program to print the following arrow on the screen using cout statements:
Code of Program No 5:
1.6.6. Program No 6
Code of Program No 6:
Run the following program segment, and find errors. Then remove the errors and execute the
program.
Include <iostream>
Int main ()
{
std:cout>>”I am trying to find bugs”;
}
Code of Program No 7:
1. What statement allows the short names cout and endl to be used
instead of std::cout and std::endl?
1. Variable Declaration
2. Variable Initialization
3. Datatypes
4. Expression
5. Limits.h Header File
6. Sizeof Operator
7. Compound Assignment Statement
8. Compound Assignment Operator
2.2. VARIABLES
A Variable is a location in the computer memory where a value can be stored for use by a
program.
2. Type variablename(value);
Example: int number(10);
3. Type variablename{value};
Example: int number{10};
All the examples above are initializing the variable number from 10.
Rules:
1. A variable name is a series of characters consisting of letters, digits and underscores,
that doesn’t begin with a digit.
2.3. Datatypes
1. Char
2. Int
3. Float
4. Double
The limits of all datatypes can be found in header file ‘limits.h’ header file. To check limits
we can use the following commands. For example “cout<<CHAR_MIN<<endl;” will give -
128 output.
Datatype Minimum Value MaximumValue
Char CHAR_MIN CHAR_MAX
Int INT_MIN INT_MAX
Short SHRT_MIN SHRT_MAX
Long LONG_MIN LONG_MAX
Unsigned long - ULONG_MAX
Float FLT_MIN FLT_MAX
Double DBL_MIN DBL_MAX
Long double LDBL_MIN LDBL_MAX
2.5. Expressions
A statement that evaluates to a value is known as expression. It consists of operands and
operators.
Example:
A+B;
In the above statement, + is the operator, A and B are operands
Example Program:
Write a Program that add two floating point numbers and print their result.
#include <iostream>
using namespace std;
int main(){
float number1=2.5;
float number2=2.5;
float sum ;
sum=number1+number2;
cout<<"The sum of "<<number1<<" and "<<number2<<"is "<<sum;
2.9.2. Program No 2
Write a program to determine the value of following expression, where a =1.5, b=1.1, c=2.89
2+4ab*c+5
Code of Program No 2:
Code of Program No 3:
Write a program to find number of bytes occupied by various data types using the sizeof
operator.
int a;
char b;
float c;
long int d;
bool e;
unsigned int j;
unsigned long k;