Download as pdf or txt
Download as pdf or txt
You are on page 1of 15

LAB MANUAL

Computer Programming

The Institute of Management Sciences


Powered by: Teacher and Instructor Name

Faculty of Computer Science

Student Lab Performance Record


Computer Programming
Lab#1 Input / Output
Sr# Programs
Write a program that inputs name, age and address from the user and then displays
1
these values on the screen.
Write a program that input height and width of a square, calculate and print area of
2
square with the given height and width.
Write a program that inputs radius from the user and calculate area and
3
circumference of circle using formula area   r 2 and Circumference  2 r .
Write a program that inputs two numbers, swap their values without using third
4
variable and display them.
5 Write a program that inputs time in seconds and convert it into HH:MM:SS format.
Write a program that inputs two numbers and apply mathematical operation like
6 addition, subtraction, multiplication, division and remainder. And display the result
on screen.
7 Write a program that explain use of setw manipulator.
Write a program that displays the values of different variables using setprecision
8
manipulator.
Write a program that displays the values of different variables using showpoint
9
manipulator.
Write a program that displays the values of different variables using setfill
10
manipulator.
11 Write a program that input a character and display its ASCII code.
Write a program that will prompt the user to enter number of hours. It computes
12 and displays the number of weeks, days and hours within the input number of
hours.
Write a program that will prompt the user to enter the current rates of electricity,
13 gas and petrol per unit. Give each item’s rate an increment of 10%. Computer and
display the new prices per unit of electricity, gas and petrol.

Lab#2 If –Else / use of logical operators in IF statement


Sr# Programs
Write a program that inputs marks and displays “Congratulations! You have
1
passed.” If the marks are 40 or more.
Write a program that inputs marks of three subjects. If the average of marks is more
2 than 80, it displays two messages. “You are above standard!” and “Admission
granted”.
Write a program that inputs a number and finds whether it is even or odd using if-
3
else statement.
Write a program that inputs salary and grade. It adds 50% bonus if grade is greater
4 than 15. It adds 25% bonus if the grade is 15 or less and then displays the total
salary.
Write a program that inputs test score of a student and displays his grade according
to the following criteria:
Test score Grade
>= 90 A
5 80 – 89 B
70 – 79 C
60 – 69 D
BELOW 60 F
Write a program that calculates the electricity bill. The rates of electricity per unit
are as follows:
a. If the units consumed are <=300, then cost is Rs. 2 per unit.
b. If the units consumed are >300 and <=500, then cost is Rs. 5 per unit.
6 c. If the units consumed exceeds 500, then cost is Rs. 7 per unit.
A line rent of Rs. 150 is also added to the total bill and a surcharge of 5% extra if
the bill exceeds Rs. 2000. Calculate the total bill with all the conditions given
above.

Write a program that inputs salary. If salary is 20,000 or more, it deduces 7% of


salary. If salary is 10,000 or more but less than 20,000, it deduces 1000 from
7
salary. If salary is less than 10,000 it deduces nothing. It finally displays the net
salary.
Write a program that inputs three numbers and displays the maximum and
8
minimum number by using logical operators.
Write a program that allows the user to enter any character through keyboard and
9
determine whether is capital letter, small letter, a digit number or a special symbol.
Write a program that inputs three digits and displays all possible combinations of
10
these digits.

Lab#3 Nested IF / Switch Structure


Sr# Programs
Write a program that inputs three numbers and displays the smallest number by
1
using nested if condition.
Write a program that inputs three numbers and displays whether all numbers are
2
equal or not by using nested if condition.
Write a program that inputs number of week’s day and display the name of the day.
3
For example if user enters 1, it displays “Friday” and so on.
Write a program that inputs a character from user and checks whether it is a vowel
4
or consonant.
Write a program that displays the following menu of a health club:
a. Standard adult membership
b. Child membership
c. Senior citizen membership
d. Quit the program
5 It should choice and number of months and calculates membership charges as
follows
Sr# Choices Charges per month
1 Standard adult membership Rs. 50
2 Child membership Rs.20
3 Senior citizen membership Rs. 30

Write a program that inputs a floating point number, an operator and another
floating point number. It displays the result by performing the operation on the
6 given number. If the operator is division, it should check to make sure the divisor is
not equal to zero. If the operator is not a +,-,*, or / then the program should print an
error.
Write a program that converts ASCII number to character and vice versa. The
program should display the following menu:
7
a. Convert ASCII value to character
b. Covert character to ASCII value

Lab#4 Loop Introduction / For Loop


Sr# Programs
1 Write a program that displays “Pakistan” for five times using for loop.
2 Write a program that displays counting from 1 to 10 using for loop.
3 Write a program that displays first five numbers and their sum using for loop.
Write a program that inputs a number from the user and displays a table of that
4
number using for loop.
Write a program that displays sum of the following series using for loop.
5
1  1/ 2  1/ 4  1/ 6  ............1/100
Write a program that inputs starting and ending number from the user and displays
6
all even numbers in the given range using for loop.
Write a program that finds the sum of the squares of integers from 1 to n. where n
7
is a positive value entered by the user. ( sum  12  22  32  ......  n2 )
8 Write a program that displays counting from 1 to 10 using while loop.
9 Write a program that displays first five numbers and their sum using while loop.
Write a program that inputs a number from the user and displays a table of that
10
number using while loop.
Lab#5 While Loop / Do While Loop
Sr# Programs
Write a program that uses a while loop to enter number from the user and then
1
display it. The loop is terminated when user enters -1.
Write a program that inputs a number from the user and displays a table of that
2
number using while loop.
Write a program that inputs an integer and displays the sum of its digits. For
3
example the program should displays 9 if user enter 135.
Write a program that displays sum of the following series using while loop.
4
1  1/ 2  1/ 4  1/ 6  ............1/100
Write a program that inputs a number and check whether it is an Armstrong
number or not. A number is armstrong if the sum of the cubes of its digits is equal
5
to the number itself. For example, 371 is an armstrong number since
33  73  13  371
Write a program that inputs numbers until the user enters a negative number. The
6
program calculates the average, maximum and minimum of all positive numbers.
7 Write a program that displays back counting from 10 to 1 using do-while loop.
Write a program that gets two numbers from the user and displays the result of first
8
number raise to power of second number using do-while loop.
Write a program that inputs a number and checks whether it is a palindrome or not.
9 A palindrome is a number that reads the same backwards as forwards such 62526
and 4994
Write a program that reads the current state of a telephone line. The user should
10 enter w for working state and d for dead state. Any other input should be invalid.
Use do while loop to force the user to enter a valid input value.

Lab#6 Nested Loop


Sr# Programs
Write a program to display the following pattern:
* * *
* * *
1
* * *
* * *
* * *
Write a program that inputs the height of triangle and displays a triangle of
characters. For example, if the user enters 5, it displays the following:
A
2 BC
DEF
GHIJ
KLMNO
Write a program that displays the following output:
3 *******
******
*****
****
***
**
*
Write a program that displays the following using do-while loop.
4444
4 333
22
1
Write a program that displays the following using nested for loop
*****
****
5
***
**
*
Write a program that displays the following output
123456
12345
6 1234
123
12
1
Write a program that displays the following output
1
2 4
7
3 6 9
2 4
1
Write a program that inputs a character from the user and draws the following
shape using the character entered by the user:
& &
8 & &
& &
& &
&
9 Write a program that displays all possible combination of 1,2 and 3
Write a program that displays a diamond of asterisks using loop.
*
***
*****
*******
10
*********
*******
*****
***
*
Lab#7 1D Array
Sr# Programs
Write a program that inputs five integers from user and stores them in an array. It
1
then displays all values in the array without using loop.
Write a program that inputs five integers from user and stores them in an array. It
2
then displays all values in the array using loop.
Write a program that inputs five values from the user, store them in an array and
3
displays the sum and average of these values.
Write a program that inputs current day and month from user. It then calculates and
4
displays the total number days in current year till the entered date.
Write a program that inputs the age of different persons and counts the number of
5 persons in the age group 50 and 60. Then it displays age of all persons and count of
persons in the age group 50 and 60.
Write a program that uses four arrays, number, square, cubes, sums each consisting
of 5 elements. The number array stores the values of its indexes, the square array
6 stores square of its indexes, the cube array stores cube of its indexes, and sums
array stores sum of corresponding indexes of three arrays. The programs should
display the values of all arrays and the total of all values in sum array.
Write a program that inputs ten numbers from the user in an array and displays the
7
minimum number.
Write a program that inputs ten numbers from the user in an array and displays the
8
maximum number.
Write a program that inputs five numbers in an array and display them in actual and
9
reverse order.

Lab#8 2D Arrays
Sr# Programs
Write a program that initialize a two dimensional array of 2 rows and 3 columns
1
and then displays its values.
Write a program that initialize a two dimensional array of 2 rows and 4 columns
2
and then displays minimum and maximum numbers in the array.
Write a program to add two matrices. Input order of matrix (i.e rows and columns).
The matrices must be of same size to be added. Get the input for each element of
3
the first matrix and then second matrix. Add the two matrices and store the values
in third matrix. Then display all matrices.
Write a program that inputs the number of rows and columns from user. It then
inputs the elements to store in the matrix. The program calculates the sum of each
4
row and each column and displays on the screen. I it is a square matrix, it also
calculates the sum of diagonal elements and displays it on screen.
Write a program that inputs integer values in a 4x4 matrix and displays sum of
5
diagonal elements of the matrix.
Write user defined functions for square matrix to calculate
6 1. Left diagonal sum
2. Right diagonal sum
Write a user-defined function in C++ to display the multiplication of row
7
element of two-dimensional array A[4][6] containing integer.
8 Write a program to multiply array A and B of order NxL and LxM
Write a user defined function named Upper-half() which takes a two
dimensional array A, with size N rows and N columns as argument and
prints the upper half of the array.

e.g.,
23150 23150
9
71531 1531
2 5 7 8 1 Output will be: 178
01501 01
34915 5

Write a program which inputs a 2D array of size M x N and display original matrix
10
and its transpose.

Lab#9 String Handling / Array of characters / Built-in string functions


Sr# Programs
Write a program that takes a string as input from user then converts its capital letters
into small letters and small letters into capital letters.
Enter a string
1
Learn from Yesterday, Live for Today, Hope for Tomorrow
Output
lEARN FROM yESTERDAY, lIVE FOR tODAY, hOPE FOR tOMORROW
Write a program that takes a string as input from user then prints whether it is
palindrome or not. (Palindrome is a type of word play in which a word, phrase, or
sentence reads the same backward or forward)
2 Enter a string
no evil shahs live on
Output
The given string is a Palindrome string
Write a program that takes a string as input from user then capitalize its each word.
Output
3 Enter a string
We must accept finite disappointment, but never lose infinite hope
We Must Accept Finite Disappointment, But Never Lose Infinite Hope
Write a program that inputs a string, a sub-string and location (where user wants to add
substring) and then inserts the substring to the location provided by the user.
Output
Enter a string:
4 Pleases this application and give me gratuity
Enter Substring: read
Location : 7
After inserting substring final output is
Pleases read this application and give me gratuity
Write a program that takes two strings as input from user then prints whether both
strings are equal or not without using built-in function of strcmp()
Output
5 Enter first string:
He that lives upon hope will die fasting Enter second string:
He that lives upon hope will die fasting
The given strings are equal
Write a program that takes a string and a character from user as input then remove that
character from the string and print the final string.
Output
6 Enter the string:
He that lives upon hope will die fasting Enter the character you want to remove: i
Final string after removing i is:
He that lves upon hope wll de fastng
7
8
9
10

Lab#10 Advance Array / Sorting / Searching


Sr# Programs
write a program that inputs 20 numbers from user in an array and display the count
1
of each number on the screen.
2 Write a program to remove characters in string except alphabets
3 Write a program to find duplicate number in an array.
Write a program that declares a three-dimensional array to store the temperature of
a month. The temperature is entered for morning, noon and evening of each day.
The first dimension should be used to for three timings of a day, second dimension
4
should be used for seven days of a week and third dimension should be used for
four weeks of a month. The program should input the temperatures and then
display the maximum, minimum and average temperature of the whole month.
write a program that initialize an array. It inputs a value from the user and searches
5
the number in the array.
Write a program that initialized an array of ten integers. It inputs an integer from
6
the uses and searches the value in the array using binary search.
Write a program that gets input five input from user and sort them in ascending
7
order using bubble sort.
Write a program that gets input five input from user and sort them in ascending
8
order using selection sort.
Write a program that gets input five input from user and sort them in ascending
9
order using quick sort.
Lab#11 Structures / IO Structures / Array of Structures
Sr# Programs
Write a program that declares a structure to store Roll No, Marks, Average and
1 Grade of a student. The program should define a structure variable, input the values
and then displays these values.
Write program that declare a structure to store day, month and year of birth date. It
2
inputs three values and displays date of birth in dd/mm/yy format.
Write a program that declares a structure to store BookID, price and pages of book.
3 It defines two structure variables and input values. It displays the record of most
costly book.
Write a program that declares a structure to store employee id and salary of an
4
employee. It defines and initialize a structure variable and display it.
Write a program that uses a structure to store three parts of phone number. i.e
national code, area code, number separately. Create two variables of structure
5
phone. Initialize one variable get and input from user in the second variables and
then display both numbers.

Lab#12 Intro to Functions / Intro to Pointers


Sr# Programs
Write a program that displays a message ‘Programming makes life interesting’ on
1
screen using functions.
Write a program that inputs a number in main function and passes the number to a
2
function. The function displays table of that number.
Write a program that inputs two numbers and one arithmetic operator in main
function and passes them to a function. The function applies arithmetic operation
3
on two numbers on the basis of the operator entered by user using switch
statement.
Write a program that inputs a number in main function and passes the number to a
4
function. The function displays the factorial of that number.
Write a program that inputs two integers. It passes first integer to a function that
calculates and returns its square. It passes second integer to another function that
5
calculates and returns its cube. The main function adds both returned values and
displays the result.
Write a program the inputs base and height of a triangle in the main function and
6 passes them to a function. The function finds the area of triangle and return it to
main function where it is displayed on screen.
Write a program that inputs a number in global variable. The program calls a
7 function that multiplies the value of global variable by 2. The main function then
displays the value of global variable.
Write a program that calls a function for five times using loop. The function uses a
8
static variable initialized to 0.
Write a program that inputs a number in an integer variable. It stores the address of
9
the variable in a pointer and then displays the values and address of the variable.
10 Write a program to input five integers in an array and display them using a pointer.
Write a program that inputs five floating point values in an array and displays the
11
values in reverse order.
Write a program that inputs a string values from the user and displays it using
12
pointer.
Write a program that declares and initialize a string. It inputs a character from the
13
user and searches the character in the array.
Write a program that uses new operator to create an integer, inputs values in it and
14
then displays that values.
Write a program that inputs length from the user and declares an array of integers
15 of length specified by the user. The program then inputs values in array and
displays them.
Lab#13 Functions / Pass by value & reference
Sr# Programs
1
2
3
4
5
6
7
8
9
10

Lab#14 Array of Pointers


Sr# Programs
Write a program that declare three integer variables and an array of pointers. It
1 takes input from the user and passes them to array of pointer then display these
numbers using array of pointers.
Write a program that declare three integer variables and an array of pointers. It
2 takes input from the user and passes them to array of pointer then display
maximum number using array of pointers.
Write a program that declares array of pointers of size 5. It input marks from user
3
in array of pointers and find their sum.
Write a program that inputs ten integers in an array of pointers and sort them in
4
ascending order using selection sort algorithm.
Lab#15 File Handling
Sr# Programs
1 Write a program to read and write data in file.
2 Write a program that inputs names of five cities and stores them in a file city.txt
3 Write a program that reads the content of the file test.txt and displays on screen.
Write a program that displays all record from city.txt prepared in previous
4
examples.
Write a program that stored five lines of strings in a file and then displays them on
5
the screen by reading these lines.
6 Write a program that input five character from the user and stores them in a file.
Write a program that reads the characters from a text file and display them on
7
screen.
Write a program that reads the characters from text file. It counts the total number
8
of characters and total numbers of vowels in the file.

You might also like