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

Ex - 4 - Programs Using Functions & Recursions

Uploaded by

shangeerthana07
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)
14 views6 pages

Ex - 4 - Programs Using Functions & Recursions

Uploaded by

shangeerthana07
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

Exercise No.

Programs using Functions & Recursions


Problem 4A

Program to find the sum of digits of a given number using recursive function.

Aim

To write a C program to find the sum of digits of a given number using recursive
function

Algorithm & Flowchart

Algorithm for finding Sum of Digits

Step 1: Start
Step 2: Get the input number, n
Step 3: Calculate the sum of digits as:
sum  findSumOfDigits(n)
Step 4: Print the value of sum
Step 5: Stop

Algorithm for Recursive Function: findSumOfDigits(n)

Step 1: Start
Step 2: If n=0 Then (Check for the base case)
Step 2.1: Return the value 0
Step 3: Calculate the reminder (last digit), r  n%10
Step 4: Calculate the quotient, q  n/10
Step 5: Calculate the sum of digit as:
s  r + findSumOfDigits(q)
Step 6: Return the value of s
Step 7: Stop

CSE101R01 – Problem Solving & Programming in C Laboratory 18


Sample Input / Output
Test Case 1:
Input
1252
Output
10
Test Case 2:
Input
5964
Output
24

Result

Thus, the program to find the sum of digits of a given number is written, entered,
executed and the results are verified.

CSE101R01 – Problem Solving & Programming in C Laboratory 19


Problem 4B
Menu-driven Program to perform following base conversions using functions
o Decimal to Binary
o Binary to Decimal
o Decimal to Octal
o Octal to Decimal

Sample Input / Output

Input
Decimal input: 12
Binary input: 1011
Octal Input: 17

Output
Binary equivalent of 12 is 1100
Octal equivalent of 12 is 14
Decimal equivalent of 1011 is 11
Decimal equivalent of 17 is 15

Problem 4C

Program to generate prime numbers within a range using function.

Sample Input / Output

Input
Range: 5 to 20

Output
5, 7, 11, 13, 17, 19

CSE101R01 – Problem Solving & Programming in C Laboratory 20


Problem 4D

Program to find the value of sin(x) using functions. The sin(x) series is given below
𝑥1 𝑥 3 𝑥 5 𝑥 7
sin(𝑥) = − + − ………
1! 3! 5! 7!

Aim

To write a C program to find the sin(x) value using functions.

Algorithm & Flowchart

Algorithm for finding 𝐬𝐢𝐧(𝒙)

Step 1: Start
Step 2: Assign: 𝒔𝒊𝒏𝒙 ← 0, 𝒔𝒊𝒈𝒏 ← 1, 𝒊 ← 1
Step 3: Get the input x (x is in radians), and n (no. of terms)
Step 4: Repeat the following steps until i<2n
𝑝𝑜𝑤𝑒𝑟 (𝑥,𝑖)
Step 4.1: 𝑠𝑖𝑛𝑥 ← 𝑠𝑖𝑛𝑥 + 𝑓𝑎𝑐𝑡𝑜𝑟𝑖𝑎𝑙(𝑖) ∗ 𝑠𝑖𝑔𝑛
Step 4.2: 𝑠𝑖𝑔𝑛 ← 𝑠𝑖𝑔𝑛 ∗ (−1)
Step 4.3: 𝑖 ← 𝑖 + 2
Step 5: Print the value of 𝑠𝑖𝑛𝑥
Step 6: Stop

CSE101R01 – Problem Solving & Programming in C Laboratory 21


Algorithm for 𝒇𝒂𝒄𝒕𝒐𝒓𝒊𝒂𝒍(𝒊)

Step 1: Start
Step 2: Initialize 𝒇𝒂𝒄𝒕 ← 1, 𝒋 ← 1
Step 3: Repeat the steps until 𝑗 ≤ 𝑖
Step 3.1: 𝑓𝑎𝑐𝑡 ← 𝑓𝑎𝑐𝑡 ∗ 𝑗
Step 3.2: 𝑗 ← 𝑗 + 1
Step 4: Return the value of 𝑓𝑎𝑐𝑡
Step 5: Stop

Algorithm for finding 𝒑𝒐𝒘𝒆𝒓(𝒙, 𝒊)

Step 1: Start the process


Step 2: Initialize 𝒓𝒆𝒔𝒖𝒍𝒕 ← 1, 𝒋 ← 1
Step 3: Repeat the steps until 𝑗 ≤ 𝑖
Step 3.1. 𝑟𝑒𝑠𝑢𝑙𝑡 ← 𝑟𝑒𝑠𝑢𝑙𝑡 ∗ 𝒙
Step 3.2. 𝑗 ← 𝑗 + 1
Step 4: Return the value of 𝑟𝑒𝑠𝑢𝑙𝑡
Step 5: Stop

Sample Input / Output

Input
x=1.57, n=3

Output
sin(1.57)=0.997 (i.e. sin(90o)=1)

Result

Thus, the program to find the value of sin(x) using functions is written, entered,
executed and the results are verified.

CSE101R01 – Problem Solving & Programming in C Laboratory 22


Problem 4E

Program to perform the following task using functions


o Given the coordinates of the points check whether a triangle can be formed
o Given a point check whether it is inside or outside the triangle

Problem Description

Triangle formation
 Using the coordinate input P(x1,y1), Q(x2,y2), R(x3,y3) find the length of the
sides.
𝑎+𝑏+𝑐
 Calculate semi perimeter using the formula 𝑠 = 2 , where a,b,c are the
length of the sides of triangle.
 Calculate 𝑠 − 𝑎, 𝑠 − 𝑏, 𝑠 − 𝑐. If any one of the values is negative then triangle
can’t be formed.

Inside outside test


 Get a fourth coordinate S(x4,y4). Calculate the area for triangles PQR, PQS,
PSR and QSR.
 Find the sum of area of triangles PQS, PSR, QSR. If the sum is equal to PQR
then the point S lies inside the triangle.
 Area of triangle =√𝑠(𝑠 − 𝑎)(𝑠 − 𝑏)(𝑠 − 𝑐)

Sample Input / Output

Input
P(2,3) Q(8,3), R(5,7)
S(9,9)

Output
Triangle PQR can be formed
Point S lies outside the triangle

CSE101R01 – Problem Solving & Programming in C Laboratory 23

You might also like