Lab_2 Manual
Lab_2 Manual
1) Header Files
A header file is a file which contains C++ function declarations and macro definitions and to be shared between
several source files. You request the use of a header file in your program by including it, with the C++
preprocessing directive #include like you have seen inclusion of iostream header file, which comes along with
your compiler.
3) Main Function
void main() is the first statement executed whenever the C++ program is run. It is the starting point of the
program. If main function is not included in the program, the compiler will show an error. Void is a data type of
function main, it shows the program is not returning any value
4) cout<<”Hello World”;
This line is a statement. Statements in C++ always end with semi-colon. Statements are always executed in the
order they appear. The cout corresponds to a standard output stream. It is used to display the output on the screen.
The symbol “<<” refers to an insertion operator. Its function is to direct the string constants to cout which display
it onto screen.
5) cin>>
cin corresponds to a standard input stream and is used to take input from input stream.
6) Comment
It makes a program more readable and gives an overall description of the code.
Comments are helpful in skipping the execution of some parts of the code.
Every time a program or code is reused after long periods of time, the comment recaps all the information of the
code quickly.
Types of comments:
1
Lab Manual Programming Fundamental
// Single Line Comment
/*Multi Line Comment*/
Variable:
Variables and Literals Variables represent storage locations in the computer’s memory. Literals are constant values
that are assigned to variables. Variables allow you to store and work with data in the computer’s memory. They
provide an “interface” to RAM. Part of the job of programming is to determine how many variables a program will
need and what types of information they will hold.
Declaration of Variables:
Declaration of variables means that compiler assigns a memory location for the variable. Declaration of a variable
consists of data type and the variable name.
Data_type var_name;
e.g. int number;
This is called a variable definition. It tells the compiler the variable’s name and the type of data it will hold. This line
indicates the variable’s name is number.
int var, number;
float var, num, sum;
char ch;
Initialization of variable:
Initialization of variables assigns an initial value to the variable declared. Variable value is initialized by
giving variable name a number consisting of equal sign followed by constant value.
Data type var_name=value;
e.g: int num=6;
float num=5.4,sum=0;
char ch=’A’;
Examples 01:
#include<iostream>
Using namespace std;
int main()
{
int number; //initialization of variable
number=6;
cout<<”The value in Number is:”<<number<<endl; //ptint variable
return 0;
}
Output:
2
Lab Manual Programming Fundamental
Examples 02:
This program has a variable
#include<iostream>
using namespace std;
int main()
{
int number;
number=6;
cout<<”The value in Number is:”;
cin>>number;
return 0;
}
Output:
The value in Number is:6
Example 03:
#include<iostream>
using namespace std;
int main()
{
Int num1,num2,sum;
num1=2;
num2=3;
sum=num1+num2;
cout<<”Sum of two numbers are:”<<sum<<endl;
return 0;
}
Output:
Sum of two number is:5
3
Lab Manual Programming Fundamental
Example 04:
This program illustrate the sum of two numbers
#include<iostream>
using namespace std;
int main()
{
Int num1,num2,sum;
Task
1) Write a program to display
*
* *
* *
* *
*
1) Write a program to display
******
*
******
*
******
1
12
123
1234
12345
3) Write a program that get two integer numbers from user and store them in variables. and preformed the basic Arithmetic
Operations (+, - ,* , /, %). Print the results of operations as bellow
4
Lab Manual Programming Fundamental
4) Write a program in C++ to compute the total and average of five numbers.
5) Write a program that prompt user to input student name, address, id and cgpa.
6) Write a program to calculate the area of triangle.
7) The distance between two objects (in km.) is input through the keyboard. Write a program to convert and print this distance
in meters, feet, inches and centimeters.
Hint: Numbers are taken from user