[go: up one dir, main page]

0% found this document useful (0 votes)
182 views43 pages

CMS 102-2 Lab Manual

The document outlines a lab manual for a Computation Skills 2 course, providing 13 labs that introduce students to key C++ concepts like input/output, data types, operators, control instructions, arrays, functions and application programs, with each lab containing problems and code examples to demonstrate the topic.

Uploaded by

Magdy Ahmed
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
182 views43 pages

CMS 102-2 Lab Manual

The document outlines a lab manual for a Computation Skills 2 course, providing 13 labs that introduce students to key C++ concepts like input/output, data types, operators, control instructions, arrays, functions and application programs, with each lab containing problems and code examples to demonstrate the topic.

Uploaded by

Magdy Ahmed
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 43

King Khalid University

College of Computer Science


Department of Computer Science

Lab Manual
CMS 102
Computation Skills 2

‫عملي‬
Lab Manual: CMS 102 Computation Skills

Contents
Lab# Description

1 Introduction to C++

2 Input/Output statements and data types

3 Operators

4 Control instructions “ if- else “

5 Control instructions “ switch-case ”

6 Menu driven programming “ switch-case ”

7 Practical Mid Examination

8 Control instructions “ loops “

9 Control instructions “ Nested – loops “

10 Arrays

11 Functions

12 Application programs

13 Practical Final Examination


King Khalid University
College of Computer Science
Department of Computer Science

Lab - 1
Introduction to C++
Lab Manual: CMS 102 Computation Skills 2

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.

CMS 102 : Lab - 1

Page |2
Lab Manual: CMS 102 Computation Skills 2

Lab - 1
Introduction to C++

1. An overview about C++ editor program

 To Open C++ program?


o Start  programs  Microsoft Visual Studio 6.0  Microsoft Visual C++ 6.0
o StartRunType MSDEV
o Just click the C++ icon on the desktop to get C++ editor environment.

 To Open New C++ program?


o File  New  win 2 console application  write project name  OK  Finish  OK
o File  New C++ source File  write File name with extension of CPP  OK

 To Compile the C++ program?


o Build  Compile.cpp
o Select compile icon  from the menu bar
o Press CTRL + F7

 To Execute the C++ program?


o Build  Execute .exe
o Select Execute icon  from the menu bar
o Press CTRL + F5

 Symbols used while writing program


{ Open flower bracket
} Close flower bracket
# Hash sign or number sign (shift+3)
( ) Small brackets
“ ” Double quotes
‘ ‘ Single quotes
, Comma
; Semicolon

CMS 102 : Lab - 1

Page |3
Lab Manual: CMS 102 Computation Skills 2

2. Understand the structure of the C++ program and environment

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

CMS 102 : Lab - 1

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 - 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
3. Problem3 :

Write C++ Program to find Sum and Average of three numbers.


C++ Source code:
#include<iostream.h>
void main( )
{ float a,b,c;
float sum,avg;
cout<<"ENTER THREE NUMBERS"<<endl;
cin>>a>>b>>C;
sum = a + b + c;
avg = sum/3;
cout<<"SUM = "<<sum<<"\n AVERAGE = "<<avg<<endl;
}
.

4. Exercises:

1) Write a C++ program to find the difference between two numbers.


2) Write a C++ program to find the product of three numbers.

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 - 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
3. Problem3:

Write a C++ Program to find area of a square. Where A=side * side.


C++ Source code:
#include<iostream.h>
void main( )
{
int side, area;
cout<<"ENTER SIDE OF SQUARE"<<endl;
cin>>side;
area = side*side;
cout<<"AREA OF SQUARE = "<<area<<endl;
}

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- 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 - 4

Control Instructions “ if- else “

1. Problem1:

Write a C++ program to find Number is MAX or MIN


C++ Source code:
#include<iostream.h>
void main()
{
int a,b;
cout<<"Enter two numbers\n";
cin>>a>>b;
if(a>b)
cout<<a<<" is MAXIMUM "<<b<<" is MINIMUM\n";
else
cout<<b<<" is MAXIMUM "<<a<<" is MINIMUM\n";
}

2. Problem2:

Write a C++ program to find the given number is ODD or EVEN


C++ Source code:
#include<iostream.h>
void main()
{
int num;
cout<<"Enter a number\n";
cin>>num;
if(num%2==0)
cout<<num<<" is an Even Number\n";
else
cout<<num<<" is an Odd Number\n";
}

P a g e | 15
3. Problem3:

Write a C++ program to find the Grade of student A, B, C, D or F.


C++ Source code:
#include<iostream.h>
void main()
{
int mark;
char grade;
cout<<"Enter mark";
cin >> mark;
if(mark >= 90 && mark <= 100 )
grade='A';
if(mark >= 80 && mark <= 89 )
grade='B';
if(mark >= 70 && mark <= 79 )
grade='C';
if(mark >= 60 && mark <= 69 )
grade='D';
if(mark < 60 )
grade='F';
cout<<"Mark"<<"\t"<<"Grade"<<endl;
cout<<mark<<"\t"<<grade<<endl;

4. Exercises:

1) Write a C++ program to find the largest among three numbers.


2) Write a C++ program to find whether the given number is palindrome or not.
Where: a palindrome number is read from right as you read from left: 4224.

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- 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 - 5

Control Instructions “ switch- case “

1. Problem1:

Write a C++ program to check the day of week by using switch-case.


C++ Source code:
#include<iostream.h>
void main()
{
int x;
cout<<"Enter number"<<endl;
cin>>x;
switch(x)
{
case 1:
cout<<"Saturday"<<endl;
break;
case 2:
cout<<"Sunday"<<endl;
break;
case 3:
cout<<"Monday"<<endl;
break;
case 4:
cout<<"Tuesday"<<endl;
break;
case 5:
cout<<"Wednesday"<<endl;
break;
case 6:
cout<<"Thursday"<<endl;
break;
case 7:
cout<<"Friday"<<endl;
break;
default:
cout<<"Error"<<endl;
}
}

P a g e | 19
2. Problem2:

Write a C++ program to find month by using a switch case statement.


C++ Source code:
#include<iostream.h>
void main()
{
int m;
cout<<"Enter Months Number :"<<endl;
cin>>m;
switch(m)
{
case 1:cout<<"JAN";break;
case 2:cout<<"FEB";break;
case 3:cout<<"MAR";break;
case 4:cout<<"APR";break;
case 5:cout<<"MAY";break;
case 6:cout<<"JUN";break;
case 7:cout<<"JUL";break;
case 8:cout<<"AUG";break;
case 9:cout<<"SEP";break;
case 10:cout<<"OCT";break;
case 11:cout<<"NOV";break;
case 12:cout<<"DEC";break;
default:cout<<"ERROR";
}
}

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- 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 - 6

Menu Driven Programming “ switch- case “

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:

Write a menu driven program to calculate different currency conversions.

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 - 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
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 - 9
Control Instructions “ Nested – loops “

1. Problem1:

Write a C++ program to display stars in a triangle shape.


C++ Source code:
#include <iostream.h>
void main()
{
int i,j,k,n,m;
cout<<"Enter a number";
cin>>n;
m=n;
for(i=0;i<n;i++) // * * *
{ // * *
for(j=0;j<i;j++) // *
cout<<" ";

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 - 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 - 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
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:

 Write a C++ program to interchange two numbers by using function.

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:
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 - 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
King Khalid University
College of Computer Science
Department of Computer Science

P a g e | 41

You might also like