0% found this document useful (0 votes)
6 views3 pages

Lab VI - COSC 215

This document outlines a lab session for COSC 215 focusing on recursion in programming. It includes four exercises: calculating powers, checking for prime numbers, finding the maximum element in an array, and determining if a string is a palindrome. Each exercise provides sample input and output to guide students in developing their recursive functions.

Uploaded by

mmezher50
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)
6 views3 pages

Lab VI - COSC 215

This document outlines a lab session for COSC 215 focusing on recursion in programming. It includes four exercises: calculating powers, checking for prime numbers, finding the maximum element in an array, and determining if a string is a palindrome. Each exercise provides sample input and output to guide students in developing their recursive functions.

Uploaded by

mmezher50
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/ 3

Affordable Excellence in Education

COSC 215 – Advanced Programming and Data Structures

Lab Session VI
Recursion

Dr. Ali Rammal (Assistant Professor – CIS Department)


Exercise 1: Power
Write a recursive function that calculates ab. Develop a main function to test your function.

Sample input/output 1

Enter a: 2

Enter b: 3

2 to the power 3 = 8

Sample input/output 2

Enter a: 2

Enter b: -3
2 to the power -3 = 0.125

Exercise 2: Prime Number


Write a recursive function that calculates checks whether an integer is prime. Develop a main
function to test your function.

Sample input/output 1

Enter value: 29

29 is prime

Sample input/output 2

Enter a: 49

49 isn’t prime

Exercise 3: Max Element of an Array


Write a recursive function that returns the greatest element in an array of numbers. Develop a main
function to test your function on the following two sample arrays:
[5, 2, 8, 9, 6, 4 ] and [-7, -2, -6, -8, -3].

Sample input/output:
Max of the first array: 9
Max of the second array: -2
2
Exercise 4: Palindrome
A palindrome is a word, number, phrase, or other sequence of symbols that reads the same
backwards as forwards, such as the words madam or Racecar, the date and time 12/21/33
12:21, and the sentence: "A man, a plan, a canal – Panama"

Define a recursive function isPalindrome that checks whether a string is palindrome.

Develop a main function to test your function as showed in the following Input/Output sample.
Recall that, to read a string s with blanks, you need to use the function getline(cin, s).

To ignore the case of a letter (uppercase or lowercase) you can convert all uppercase letters to
lowercase letters by adding to them the value 32. For example, ‘A’ + 32 gives ‘a’.

Sample input/output

Enter a string: madam

madam is a palindrome

Enter a string: Racecar

Racecar is a palindrome

Enter a string: 12/21/33 12:21

12/21/33 12:21 is a palindrome

Enter a string: A man, a plan, a canal - Panama

A man, a plan, a canal - Panama is a palindrome

Enter a string: palindrome

palindrome isn't a palindrome

Enter a string: stop

Thank you!

You might also like