0% found this document useful (0 votes)
130 views2 pages

LAB 9-In Lab Tasks PDF

This document provides instructions for 4 tasks to practice key C++ concepts in a lab: 1. Writing overloaded functions with different prototypes. 2. Passing arguments to functions by reference and value and swapping variable values. 3. Defining recursive functions to calculate powers and factorials, and using them to compute mathematical expressions. 4. Defining a recursive function to separate digits of a number and display the reverse.

Uploaded by

Raja Hamdan
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)
130 views2 pages

LAB 9-In Lab Tasks PDF

This document provides instructions for 4 tasks to practice key C++ concepts in a lab: 1. Writing overloaded functions with different prototypes. 2. Passing arguments to functions by reference and value and swapping variable values. 3. Defining recursive functions to calculate powers and factorials, and using them to compute mathematical expressions. 4. Defining a recursive function to separate digits of a number and display the reverse.

Uploaded by

Raja Hamdan
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/ 2

LAB 9 – C++ LANGUAGE AND FUNCTIONS-II

Learning Objectives
In today’s lab, you will practice;
1. Writing overloaded functions.
2. Passing arguments by reference and value.
3. Writing recursive functions.

Practice Questions
You can practice the following problems in this lab.
Task 1.1 Function Overloading
Write a C++ program using function overloading. The name of the overloaded function is
‘square’. The different prototypes of this overloaded function are;
• void square(void); // Used to print a solid square of *s with side length of 5
• void square (char); // Used to print a solid square of the character passed as argument
with side length of 6
• void square(char, int); // Used to print a solid square of the character and the side length
passed as arguments to this function
Make calls to these functions one by one in your program.
Task 1.2 Passing by Reference
Write a function ‘swap’ in C++ language that swaps the values of two variables passed as
arguments to this function. Write a C++ program which takes values of two integer variables
from the user and swaps their values using function ‘swap’. Your program should have the
following interface.
Enter the first value : 6
Enter the second value : 9
The two numbers have been swapped.
The first number is now 9.
The second number is now 6.

Task 1.3 Using Recursive Functions


Define a recursive function named ‘power’ which takes two parameters, base and exponent,
and returns the value equivalent to base multiplied by itself, exponent times. Define another
recursive function ‘factorial’ which takes an integer as argument and returns the value
equivalent to this number’s factorial. Use these recursive functions in a C++ program which
takes the values of x,y and N from user, calculates the following functions and displays the
results on screen.

F(x, y) = x/y – x2/y2 + x3/y3 – x4/y4 + ………… + xN/yN

G(x) = x/1! – x2/2! + x3/3! – x4/4! + ………… + xN/N!


Your program should have the following interface.
Enter the values of x,y and N.
3
2
3
F(3,2)= 2
G(3) = 3

Task 1.4 Using Recursive Functions


Write a recursive function named ‘separator’ which takes two integer parameters and finds
the quotient and remainder when the first integer is divided by the second integer. Write a
C++ program which takes an integer input from the user, uses the function ‘separator’ to
separate the digits of this number and displays the reverse of this number. Your program
should have the following interface.

Enter a number : 3453


The number in reverse is : 3543

You might also like