Function Lab 06
Function Lab 06
1. Write a C++ function int sum(int a, int b) that takes two integers as parameters
and returns their sum. Implement the function and demonstrate its use in the
main() function.
2. Create a function int factorial(int n) that calculates and returns the factorial of
a given non-negative integer n. Use the function in main() to print the factorial
of a number provided by the user.
3. Write a function bool isPrime(int n) that checks whether a given integer n is a
prime number. The function should return true if n is prime, and false
otherwise. Test the function with various inputs in main().
4. Write a function int maxOfThree(int a, int b, int c) that takes three integers as
parameters and returns the largest of the three. Demonstrate the function in
main().
5. Create a function int gcd(int a, int b) that calculates and returns the greatest
common divisor (GCD) of two integers using the Euclidean algorithm. Use
the function in main() to find the GCD of two num
6. Write a function int power(int base, int exp) that calculates and returns the
result of raising base to the power of exp (i.e., base^exp). Do not use any built-
in power functions. Implement the function in main() to calculate powers of
various numbers.
7. Implement a function bool isPalindrome(int n) that checks if an integer n is a
palindrome. A number is a palindrome if it reads the same forward and
backward. Use this function in main() to test several numbers.
8. Write a function int fibonacci(int n) that returns the n-th Fibonacci number.
The Fibonacci sequence starts with 0 and 1, and each subsequent number is
the sum of the previous two. Use this function in main() to print the first n
Fibonacci numbers.