Tutorial 3

You might also like

Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 2

Tut -3

EEN-103
Autumn 2019
1. Write a program that uses the following functions:
int addition (int, int); // adds two integers and returns the sum
int subtraction (int, int); //subtracts one integer from other and returns the difference
int multiplication (int, int); //multiplies one integer to other and returns the product
int division (int, int); //divides one integer by other and returns quotient
void printLines(int, char);// prints lines with the some character e.g.
***************************************** or ###########################
Call the printLines function in a main so that a line is printed before display (of addition/subtraction
etc), and after each display and also at the end of program.
2. WAP with a function to find the power of integer, float (also compare the result with pow
function using Math library )
3. WAP with function (i) Input a positive number from the keyboard and (ii) find its prime
factors.
4. WAP with function to display the following depending on number of rows:
1
23
456
7 8 9 10
5. WAP with a function to display the following pattern depending on number of row.
void stars(int )
*
**
***
6. WAP with a function to circularly shift the values of three variables and display the shifted
values immediately after the method call. e.g x = 3, y = 4, z= 5 then after shift x= 4, y = 5, z = 3.
int shift(int &, int &, int & );

7. Write a function ‘sumDiff’ which either adds two integers and returns the sum or subtracts
the two integers and returns the difference. The function takes a bool parameter to decide the
action of summing or subtracting. Write main to test the function.
8. Write a program which calls the function ‘maximum’ that takes two integer parameters and
returns the larger integer to main. In the main we are interested in finding the largest integer from
three integers.
9. Write a function caseReversal which take one char type parameter and returns the same
with its case reversed i. e. lower case is returned in upper case and vice versa. Write a driver of
this function to test it.
10. Write a Boolean function called ‘isEven’ that takes an integer and returns false if the given
integer is not even otherwise returns true. Call this function in the main.
11. Write a Boolean function that tests whether the number passed to it is a factorion. A
factorion is a natural number that equals the sum of the factorials of its decimal digits. For
example, 145 is a factorion because 1! + 4! + 5! = 1 + 24 + 120 = 145
12. Write a function printVertically that takes a parameter of type string and displays the string
of characters vertically. The function does not return anything to main. Write main to test
this function. Use of array is not allowed.
13. A function ‘Box_Volume’ takes three default parameters: length, width and height which
are of type float and returns volume of the box, which is also of type float. Write main that calls
the function ‘Box_Volume’ (i) with one default parameter, (ii) with two default parameters, and (iii)
with three default parameters. Also call this function by giving length, width and height from the
user. Try to understand the implication of default values. Make the default values such that the
volume is unity.
14. Write a function: void Compute_Volume (float & volume, float radius, float height). The
task of this function is to compute the volume of the cone (Note that function is void type). Display
the volume of the cone in the main; don’t display volume in the function. Will the program work if
first parameter is not reference type?
15. Write two overloaded versions of a function ‘Addition’ – one that takes two int parameters
and returns the sum, and the other that takes two float parameters and returns the float sum.
Write main to test these functions.
16. Write a function power_ which takes two parameters: i) double for base and ii) int for
power. The function should compute the value of x+n and x-n, where x is the base and n is the
power. The function should be able to compute x+n and x-n depending whether the power is +ve
or –ve. Write the main to test this function.
17. Write a recursive function ‘Fibonacci’ that takes an integer parameter and returns an
integer value. The task of this function is to find nth Fibonacci number. The relationship between
the Fibonacci numbers is given by: F(n) = F(n-1) + F(n-2), where n is a positive integer. Further
F(0) = 1 and F(1) = 1. Write another function ‘Fibonacci_Itr’ that takes the integer parameter and
returns the integer value. This function uses an iterative technique to find Fibonacci number. Write
two separate main to test these function and understand the difference between these functions.
Try calculating F(150) using both versions of the function. Which takes longer and why?
18. Write a recursive function ‘int MultiplicationNumbers (int m, int n)’ where m is less than n. The
task of this function is to multiply numbers starting from m and ending at n and return the product.
As for example, if m = 3 and n = 6, the function should return the product ‘3 x 4 x 5 x 6’ = 360.
Write main to test this function.
19. A recursive function ‘int Factorial (unsigned int n)’ computes the factorial of a positive integer
inclusive of 0. The body of this function should have only one statement. Write main to test this
function.
20. Write a recursive function ‘int gcd(int m, int n) to compute the greatest common divisor of two
positive integer numbers. Write main to test the function.

You might also like