Lab3 Function
Lab3 Function
C/C++ Programming
Lab 3
Functions
_____________________________________________________________________________________
1
INTERNATIONAL UNIVERSITY
I. Objectives
Students are required to review the theory about the topics before the lab time.
Exercise 1
Write a C program that reads several numbers and rounds each of these numbers to the
nearest integer. Print both the original number and the rounded number.
Hint: Include the <math.h> and Use the C standard library function: floor.
Output:
Exercise 2
Write a function that takes an integer value and returns the number with its digits
reversed. Write a C program to test the function.
Output:
_____________________________________________________________________________________
2
INTERNATIONAL UNIVERSITY
Exercise 3
Write a C program using recursion function that solve the Towers of Hanoi (TOH) puzzle
where we have three rods and n disks. The objective of the puzzle is to move the entire
stack from rod A to rod C, as shown in the below diagram.
Your program must print the series of moves that needed to solve the puzzle.
Hint:
Moving n disks can be viewed in terms of moving only n – 1 disks (and hence the
recursion) as follows:
a) Move n – 1 disks from rod A to rod B, using rod C as a temporary holding area.
b) Move the last disk (the largest) from rod A to rod C.
c) Move the n – 1 disks from rod B to rod C, using rod A as a temporary holding area.
_____________________________________________________________________________________
3
INTERNATIONAL UNIVERSITY
Output:
Exercise 5
Write a C program that aid a young student learns multiplication. Use srand function to generate
randomly two positive 1-digit integers.
The student then entered the solution. The program should verify whether the message in the answer and
response is correct or incorrect. Another question involving multiplication follows. Until the student
enters a -1 to end the program, the procedure should be repeated. The program concludes by showing the
number of questions asked and accurate answers.
Output:
_____________________________________________________________________________________
4
INTERNATIONAL UNIVERSITY
THE END
_____________________________________________________________________________________