0% found this document useful (0 votes)
132 views6 pages

Ue24cs151b 20250317231859

This document contains lecture notes for a course on Problem Solving with C, focusing on recursion. It outlines course objectives and outcomes, and provides a series of programming challenges that students must solve using recursive techniques. The problems range from basic arithmetic operations to more complex tasks like generating Pascal's Triangle and creating a numeric password generator.
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)
132 views6 pages

Ue24cs151b 20250317231859

This document contains lecture notes for a course on Problem Solving with C, focusing on recursion. It outlines course objectives and outcomes, and provides a series of programming challenges that students must solve using recursive techniques. The problems range from basic arithmetic operations to more complex tasks like generating Pascal's Triangle and creating a numeric password generator.
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/ 6

Department of Computer Science and Engineering,

PES University, Bangalore, India

Lecture Notes
Problem Solving With C
UE24CS151B

Lecture #13
Problem Solving using Recursion

By,
Prof. Sindhu R Pai,
Theory Anchor, Feb-May, 2025
Assistant Professor
Dept. of CSE, PESU

Many Thanks to
Dr. Shylaja S S (Director, CCBD and CDSAML Research Center, PES University)
Prof. Nitin V Pujari (Dean, Internal Quality Assurance Cell, PES University)
Feb – May, 2025

Unit #: 2
Unit Name: Counting, Searching and Sorting
Topic: Problem Solving using Recursion

Course objectives: The objective(s) of this course is to make students


 Acquire knowledge on how to solve relevant and logical problems using computing
Machine.
 Map algorithmic solutions to relevant features of C programming language constructs.
 Gain knowledge about C constructs and its associated ecosystem.
 Appreciate and gain knowledge about the issues with C Standards and it’s respective
behaviors.

Course outcomes: At the end of the course, the student will be able to:
 Understand and Apply algorithmic solutions to counting problems using appropriate C
Constructs.
 Understand, Analyze and Apply sorting and Searching techniques.
 Understand, Analyze and Apply text processing and string manipulation methods using
Arrays, Pointers and functions.
 Understand user defined type creation and implement the same using C structures,
unions and other ways by reading and storing the data in secondary systems which
are portable.

Sindhu R Pai
Theory Anchor, Feb - May, 2025
Dept. of CSE,
PES University

Department of CSE, PESU 2


Feb – May, 2025

Solve the below programs using Recursion

Link for Solution: https://drive.google.com/file/d/1CZKirLd2E1j7leLG_aEPsPLca5jIjXV-


/view?usp=drive_link

Level-1:Banana
1. A young programmer is learning recursion. His mentor gives him a challenge: Can you
add two integers without using the + operator directly? Instead of normal addition,
he must use recursion by incrementing one number while decrementing the other until
one of them becomes zero.
Input: 4 3 Expected Output: 7

2. Fibonacci numbers appear in nature, such as the arrangement of leaves, flowers, and
shells. Develop an application for biologists to analyze these patterns using a recursive
function Fibonacci(n) to generate the nth Fibonacci number. Display appropriate
message for the negative value of n and 0.
Input: n = 6 Expected Output: 5
Explanation:
0,1,1,2,3,5,8,13,21,34... Fibonacci sequence is a sequence in which each number is the
sum of the two preceding numbers. First two numbers are fixed in the series.

3. A bank's security team discovered that fraudsters were tampering with account
numbers. To prevent this, they introduced a checksum verification system. Each
account number must pass a test where the sum of its digits is calculated to ensure
authenticity. Can you help the bank by writing a recursive function to compute the sum
of digits in an account number?
Input: 1234 Expected Output: Sum of digits: 10
Input: -1234 Expected Output: Sum of digits: 10

4. In a secret agency, messages including positive integers are encrypted using a special
formula: ab. The spies need to quickly compute large powers to send secure codes.
Using recursion, they can break down the calculation into smaller steps, making it
faster and more efficient. Implement a recursive function to help them encrypt their
messages?
Input: a = 2, b = 5
Expected Output: 2^5 = 32
Input: a = 0, b = 0
Expected Output: 0^0 = 1
Input: a = 0, b = 1
Expected Output: 0^1 = 0
Input: a = 1, b = 0
Expected Output: 1^0 = 1

Department of CSE, PESU 3


Feb – May, 2025

5. In an ancient civilization, architects used a special formula on positive integers to


calculate the correct proportions of their buildings. They believed that using the
Greatest Common Divisor (GCD) of two measurements ensured structural
stability. As an engineer studying these historical techniques, you need to write a
recursive function to compute the GCD of two given numbers. Display appropriate
message for negative integers.
Input: a = 56, b = 98
Expected Output: GCD of 56 and 98 is 14

Level-2:Orange
6. NASA scientists are working on decoding signals from an alien spacecraft. The signals
are sent in binary format, and they need to be converted to decimal numbers to
understand the coordinates of the spacecraft. Since binary numbers can be of any
length, recursion is a natural choice for conversion.
Input: 1011
Expected Output: Decimal equivalent: 11

7. One day, Rahul went to an ATM to deposit money. Due to system error, the ATM
processed his transaction in reverse order. Now, the bank's software needs to reverse
the digits of the transaction ID to verify the correct transaction. Since numbers can
have varying lengths, recursion is an efficient way to solve this problem without using
strings.
Input: 1234
Expected Output: Reversed number: 4321

8. In an ancient temple, scholars discovered a mysterious set of numbers carved into a


stone. They believed these numbers were magical because they read the same
forward and backward. To decode their meaning, they requested the team to build an
application to check if a number is a palindrome or not. Since numbers can be of
varying lengths, recursion is the best way to solve this problem.
Input: 1221
Expected Output: 1221 is a palindrome

9. A digital wallet app needs a fraud detection system that checks if a positive transaction
ID is too large. To simplify the ID, they sum up all its digits recursively until they
get a single-digit number. This is similar to how some banking checksum algorithms
work. Handle negative inputs with a proper message.
Input: 9875
Expected Output: Single digit id is 2
Explanation:
9 + 8 + 7 + 5 = 29
2 + 9 = 11
1 + 1 = 2 (Final output)

Department of CSE, PESU 4


Feb – May, 2025

Level-3:Jackfruit
10. A mathematician named Aryan discovered an ancient manuscript containing a
numerical pattern known as Pascal’s Triangle. The legend says that a hidden treasure
can be unlocked if he deciphers the number at a specific position in the triangle.

According to the manuscript, the 0th row starts with 1, and each number in the next
row is formed by adding the two numbers directly above it. Aryan needs to find the
element at a given row N and column M to move forward in his quest. However, the
calculations become tricky for large values, and he decides to use recursion to solve the
problem efficiently.

Can you help Aryan find the treasure by writing a recursive function that determines
the number at the Nth row and Mth column of Pascal’s Triangle? Handle negative
values accordingly with a proper message.
Pascal’s Triangle

Input: 4 2
Expected Output: Pascal's Triangle value at (4, 2) is 6
Explanation: The 4th row of Pascal’s Triangle is [1, 4, 6, 4, 1].
The element at the 2nd column (0-based index) in the 4th row is 6.

11. A monk in a temple follows an ancient tradition where he moves a stack of 3 disks
from one rod to another using a helper rod. The rule is that only one disk can be
moved at a time, and no larger disk can be placed on a smaller one. This is a
perfect example of recursion because solving it for n disks depends on solving it for n-
1 disks. Implement the function hanoi to do the requirement.
Input: 3
Client code: int n = 3;
hanoi( n, 'A', 'C', 'B' );
Function declaration: void hanoi (int n, char from, char to, char aux);
Expected Output:
Move disk 1 from A to C
Move disk 2 from A to B
Move disk 1 from C to B
Move disk 3 from A to C
Move disk 1 from B to A
Move disk 2 from B to C
Move disk 1 from A to C

Department of CSE, PESU 5


Feb – May, 2025

12. A mobile app developer is working on a numeric password generator. The challenge is
to generate all possible 3-digit numbers using the digits available on a mobile
keypad (0-9). Since there are 10 digits (0-9), the system should recursively form all
unique 3-digit combinations where each digit can be used multiple times.
Expected output:
000
001
002
...
997
998
999

Happy Coding using Recursion!!

Department of CSE, PESU 6

You might also like