HAL 102 - Lab
HAL 102 - Lab
HAL 102 - Lab
Lab Manual
102 HAL
Introduction to Computer-2
Contents
Lab# Description
1 Introduction to C++
3 Operators
10 Arrays
11 Functions
12 Application programs
Lab - 1
Introduction to C++
Lab - 1
Introduction to C++
Statement Purpose:
This lab will give you an overview about C++ editor program.
Activity Outcomes:
This lab teaches you the following topics:
• Introduction about C++ software.
• On which operating system does the C++ program run?
• How to get C++ environment to write programs?
• Procedure for compilation
• Handling the errors.
• What is the word debugging?
• How to execute the programs?
• How to create the new programs?
• How to open the existing programs? ( Previous programs )
• Getting exposed about the C++ editor.
Page |2
Lab Manual: Intro. To Computers - 2
•
Lab - 1
Introduction to C++
Page |3
Lab Manual: Intro. To Computers - 2
Problem:
Write a C++ program to print “ Welcome to C++ ” on the Screen.
C++ Source code:
#include<iostream.h> // preprocessor
void main( ) // function with return type void
{ // is a comment
cout<<"Welcome to C++”; // cout is an object in C++
}
3. Exercises:
• Write a C++ program to print the student’s number, Name, age and Phone number on the screen.
Output:
Id: 123456789
Name: Abdullah
Age: 20 years
Phone: 0500500500
Page |4
King Khalid University
College of Computer Science
Department of Computer Science
Lab - 2
Input/Output Statements
and Data Types
Lab - 2
Input/Output Statements and Data
Types
Statement Purpose:
This lab will give you concepts of input, output statements and data types.
Activity Outcomes:
This lab teaches you the following topics:
• All data types like int , float, char and string etc.
• Identify the type of the instructions.
• Get idea about the input instruction, that is cin>>
• Apply division and other operations on two numbers.
Page |6
Lab Manual: Intro. To Computers - 2
•
Lab - 2
Input/Output Statements and Data Types
1. Problem1:
Write a C++ program which accepts one integer, one float and one character from keyboard and display
them on monitor.
C++ Source code:
#include<iostream.h>
void main( )
{
int a;
float b;
char c;
cout<<"ENTER AN INTEGER"<<endl;
cin>>a;
cout<<"ENTER A FLOAT"<<endl;
cin>>b;
cout<<"ENTER A CHARACTER"<<endl;
cin>>c;
cout<<"INTEGER= "<<a<<"\tFLOAT= "<<b<<"\tCHARACTER= "<<c<<endl;
}
2. Problem2 :
Write a C++ Program to find Sum and Average of two integer numbers.
C++ Source code:
#include<iostream.h>
void main( )
{
float a,b;
float sum,avg;
cout<<"ENTER TWO NUMBERS"<<endl;
cin>>a>>b;
sum = a + b;
avg = sum/2;
cout<<"SUM = "<<sum<<"\n AVERAGE = "<<avg<<endl;
}
Page |7
Lab Manual: Intro. To Computers - 2
3. Problem3 :
4. Exercises:
Page |8
King Khalid University
College of Computer Science
Department of Computer Science
Lab - 3
Operators
Lab - 3
Operators
Statement Purpose:
This lab will give you the concept to implement operators.
Activity Outcomes:
This lab teaches you the following topics:
• Students should aware of different types of operators available in C++.
• What are the uses of operators?
• How operators are help full in the programming?
• Importance of operator precedence.
• Finally, how can you apply the operators in programs?
P a g e | 10
Lab Manual: Intro. To Computers - 2
Lab - 3
Operators
1. Problem1:
Write a C++ program to find area of a circle. Where A=3.14*r2 and r is a radius.
C++ Source code:
#include<iostream.h>
void main( )
{
float r,area;
const float pi=3.147;
cout<<"ENTER RADIUS OF CIRCLE"<<endl;
cin>>r;
area = pi*r*r;
cout<<"AREA OF CIRCLE = "<<area<<endl;
}
2. Problem2:
Write a C++ program find area of a rectangle. Where A=len * wid and len is a length, wid is a width.
C++ Source code:
#include<iostream.h>
void main( )
{
int len, wid, area;
cout<<"ENTER LENGTH OF RECTANGLE"<<endl;
cin>>len;
cout<<"ENTER WIDTH OF RECTANGLE"<<endl;
cin>>wid;
area = len*wid;
cout<<"AREA OF RECTANGLE = "<<area<<endl;
}
P a g e | 11
Lab Manual: Intro. To Computers - 2
3. Problem3:
4. Exercises:
Write a C++ program to compute the circumference of circle, rectangle and square.
Where:
• Circumference of circle = 2 * 3.14 * R and R is a radius.
• Circumference of rectangle = 2 * width + 2 * length.
• Circumference of square = 4 * side.
P a g e | 12
King Khalid University
College of Computer Science
Department of Computer Science
Lab - 4
Control Instructions
“ if- else “
Lab - 4
Control Instructions
“ if-
if- else “
Statement Purpose:
This lab will give you understanding the concepts of conditional statements ( if – else ).
Activity Outcomes:
This lab teaches you the following topics:
• The importance of selection instructions.
• Write code for different type of conditional instructions.
• The difference between if and else keywords.
P a g e | 14
Lab Manual: Intro. To Computers - 2
Lab - 4
1. Problem1:
2. Problem2:
P a g e | 15
Lab Manual: Intro. To Computers - 2
3. Problem3:
4. Exercises:
P a g e | 16
King Khalid University
College of Computer Science
Department of Computer Science
Lab - 5
Control Instructions
“ switch- case “
Lab - 5
Control Instructions
“ switch-
switch- Case “
Statement Purpose:
This lab will give you understanding the concepts of conditional statements ( switch – case ).
Activity Outcomes:
This lab teaches you the following topics:
• The difference between if and case instructions.
• The importance of case conditions.
• Developing At least two programs using case by students themselves.
• The functionality of “break” and “default “keywords.
P a g e | 18
Lab Manual: Intro. To Computers - 2
Lab - 5
1. Problem1:
P a g e | 19
Lab Manual: Intro. To Computers - 2
2. Problem2:
P a g e | 20
King Khalid University
College of Computer Science
Department of Computer Science
Lab - 6
Menu Driven Programming
“ switch- case “
Lab - 6
Menu Driven Programming
“ switch-
switch- Case “
Statement Purpose:
This lab will give you an idea about menu driven programming.
Activity Outcomes:
This lab teaches you the following topics:
• Get clear idea about importance of menu driven programming.
• Developing small menu driven programs.
P a g e | 22
Lab Manual: Intro. To Computers - 2
Lab - 6
1. Problem1:
Write a C++ program to implement menu driven program ( + , * , - , %) using switch case statement.
C++ Source code:
#include<iostream.h>
void main()
{
int n1, n2;
char op;
cout<<"Enter first number"; cin >> n1;
cout<<"Enter second number"; cin >> n2;
cout<<"Enter operator(*, /, + -)"; cin >> op;
switch (op)
{
case '*':
cout<<n1<<op<<n2<<"="<<(n1*n2)<<endl;
break;
case '/':
cout<<n1<<op<<n2<<"="<<(n1/n2)<<endl;
break;
case '+':
cout<<n1<<op<<n2<<"="<<(n1+n2)<<endl;
break;
case '-':
cout<<n1<<op<<n2<<"="<<(n1-n2)<<endl;
break;
default:
cout<<"Invalid oprator"<<endl;
break;
}
}
2. Exercises:
P a g e | 23
King Khalid University
College of Computer Science
Department of Computer Science
Lab - 8
Control Instructions
“ loops “
Lab - 8
Control Instructions
“ loops “
Statement Purpose:
This lab will give you the concept implementation of loops.
Activity Outcomes:
This lab teaches you the following topics:
• Understanding the concept of looping (for, while and do…..while loop).
• How it is use full in the programming for repetitive instructions with number of times.
• The difference between the while and do- while loop.
• Finally, developing the programs using loops.
P a g e | 25
Lab Manual: Intro. To Computers - 2
Lab - 8
Control Instructions “ loops “
1. Problem1:
Write a C++ program to print natural numbers from 1 to 30 using for loop.
C++ Source code:
#include <iostream.h>
void main()
{
int i;
for (i=0;i<=30;i++)
{
cout<<" "<<"i="<<i<<endl;
}
}
2. Problem2:
Write a C++ program to print natural numbers from 1 to 30 using while loop.
C++ Source code:
#include<iostream.h>
void main()
{
int a=1;
while(a<=30)
{
cout<<a<<endl;
a++;
}
}
P a g e | 26
Lab Manual: Intro. To Computers - 2
3. Problem3:
Write a C++ program to print natural numbers from 1 to 30 using do while loop.
C++ Source code:
#include<iostream.h>
void main()
{
int a=1;
do
{
cout<<a<<endl;
a++;
}
while(a<=30);
}
4. Problem4:
Write a C++ program to display first 10 Odd numbers using for loop.
C++ Source code:
#include <iostream.h>
void main()
{
int i;
for (i=1;i<=20;i=i+2)
cout<<" i="<<i<<endl;
}
5. Problem5:
Write a C++ program to display first 10 even numbers using for loop.
C++ Source code:
#include <iostream.h>
void main()
{
int i;
for (i=2;i<=20;i=i+2)
cout<<" i="<<i<<endl;
}
P a g e | 27
King Khalid University
College of Computer Science
Department of Computer Science
Lab - 9
Control Instructions
“ Nested – loops “
Lab - 9
Control Instructions
“ Nested – loops “
Statement Purpose:
This lab will give you understanding the concepts of nested loops.
Activity Outcomes:
This lab teaches you the following topics:
• Developing different programs using nested-loops.
P a g e | 29
Lab Manual: Intro. To Computers - 2
Lab - 9
Control Instructions “ Nested – loops “
1. Problem1:
for(k=0;k<m;k++)
cout<<" *";
cout<<endl;
m--;
}
}
P a g e | 30
King Khalid University
College of Computer Science
Department of Computer Science
Lab - 10
Arrays
Lab - 10
Arrays
Statement Purpose:
This lab will give you understanding the concepts of arrays.
Activity Outcomes:
This lab teaches you the following topics:
• The importance of arrays while using data declaration.
• How to use array concept in the programming?
• How you will reduce the number of variables using arrays?
P a g e | 32
Lab Manual: Intro. To Computers - 2
Lab - 10
Arrays
1. Problem1:
Write a C++ program to store 5 numbers in an array then print them.
C++ Source code:
#include<iostream.h>
void main()
{
int a[5],i;
for(i=0;i<5;i++)
{
cout<<"enter any Number :"<<endl;
cin>>a[i];
}
cout<<"The Elements are as below : "<<endl;
for(i=0;i<5;i++)
{
cout<<a[i]<<endl;
}
}
2. Problem2:
Write a C++ program to store 5 numbers in an array then print them in reverse order.
C++ Source code:
#include<iostream.h>
void main()
{
int a[5],i;
for(i=0;i<5;i++)
{
cout<<"enter any Number :"<<endl;
cin>>a[i];
}
cout<<"The elements are as below : "<<endl;
for(i=4;i>=0;i--) // Reverse Order
{
cout<<a[i]<<endl;
}
}
P a g e | 33
King Khalid University
College of Computer Science
Department of Computer Science
Lab - 11
Functions
Lab - 11
Functions
Statement Purpose:
This lab will give you understanding the concepts of functions.
Activity Outcomes:
This lab teaches you the following topics:
• Learning the correct meaning of the reuse and how it is implementing in the programming by
using function concept.
• Importance of functions.
• Learning about return and void related to functions.
P a g e | 35
Lab Manual: Intro. To Computers - 2
Lab - 11
Functions
1. Problem1:
Write a C++ program to add two numbers using function.
C++ Source code:
#include <iostream.h>
int add(int x, int y);
void main()
{
int a, b;
cout<<" Enter value of a ";
cin>>a;
cout<<" Enter value of b ";
cin>>b;
add(a,b);
}
int add(int x, int y)
{
int res;
res = x+y;
cout<<" Sum = "<<res<<endl;
return res;
}
2. Problem2:
Write a C++ program to create function for calculating the square of any integer.
C++ Source code:
#include<iostream.h>
int sq(int);
void main()
{
int x;
cout<<"enter the value of x ";
cin>>x;
cout<<"the square of "<<x<<"="<<sq(x)<<endl;
}
int sq(int x)
{
return x*x;
}
P a g e | 36
Lab Manual: Intro. To Computers - 2
3. Problem3:
Write a C++ program to create function to find the factorial of a number.
C++ Source code:
#include<iostream.h>
int fact(int);
void main()
{
int x,f;
cout<<"enter the number";
cin>>x;
f=fact(x);
cout<<"The factorial"<<f;
}
int fact(int a)
{
int i,f=1;
for(i=1;i<=a;i++)
f=f*i;
return f;
}
4. Exercises:
P a g e | 37
King Khalid University
College of Computer Science
Department of Computer Science
Lab - 12
Application Programs
Lab - 12
Application Programs
Statement Purpose:
This lab will give you How C++ programs are giving solutions to general examples.
Activity Outcomes:
Outcomes:
This lab teaches you the following topics:
• Applying the concepts to general problems like Fibonacci series and Factorial programs etc.
• Get little bit confidence on programming.
P a g e | 39
Lab Manual: Intro. To Computers - 2
Lab - 12
Application Programs
1. Problem1:
Write a C++ program to find the Factorial of Number using do while loop.
C++ Source code:
#include<iostream.h>
void main()
{
int n,i=1,fact=1;
cout<<" enter the number ";
cin>>n;
if(n>=0)
{
do
{
fact=fact*i;
i++;
}
while(i<=n);
cout<<"fact="<<fact<<"\n";
}
else
{
cout<<”\nFactof Zero is 0”;
}
}
2. EXERCISES:
1) Write a C++ program to generate Fibonacci numbers up to 100 using for loop.
2) Write a C++ program to generate multiplication table of any number using while loop.
3) Write a C++ program to print all prime numbers between two limits by using do while loop.
P a g e | 40
Lab Manual: Intro. To Computers - 2
P a g e | 41