Name DEVANK SWAMIPRASAD KOLPE
UID no. 2024300118
Experiment No. 3
AIM: Apply the concept of function to incorporate modularity.
Program 1
PROBLEM
STATEMENT :
Write a program to Calculate Factorial of a given number
ALGORITHM:
1. Start
2. Declare Function: int accept_n(int n);
3. Main Function:
4. Declare variables int n, ans
5. Ask User to enter value of n.
6. Read the value of n
7. Call function ans = accept_n(n);
8. Display ans.
1. Define function int accept_n(int n)
2. Initialize ans to 1
3. For Loop for i from 1 to n:
4. Multiply ans by i
5. Update ans=ans*i
6. Return ans
7. End
FLOWCH
ART:
PROGRA
M:
a
RESUL
T:
Program 2
Write a program to calculate x raised to y without using library functions.
PROBLEM
STATEMENT:
ALGORITHM: 1. Start
2. Define Function: double power(double x, int y)
Power Function:
Initialize double result = 1.0
While loop y is not equal to 0:
If y > 0:
Multiply result by x
Decrease y by 1
Else (when y < 0):
Divide result by x
Increase y by 1
Return result
3. Main Function:
4. Declare variables double x and int y
5. Ask user to enter base and power.
6. Read x and y.
7. Call the function double result = power(x, y);
8. Print result.
9. End
FLOWCHART:
PROGRAM:
RESULT:
Program 3
WAP to check if the given number ODD_or_EVEN using a
PROBLEM
function
STATEMENT:
ALGORITHM:
1. Start
2. Function Definition: Define the function void even_odd(int m):
3. Ask User to enter Number.
4. Read m
5. If m % 2 == 0, then:
Print "even"
6. Else:
Print "odd"
7. Main Function:
8. Declare Variable: int a
9. Function Call: Call the function even_odd(a);
10. End.
FLOWCHART
:
PROGRAM:
aa
FLOWCHART:
PROGRAM:
RESULT:
Program 5
Write a function that prints this pattern below :
PROBLEM 1
STATEMENT: 1 23
ALGORITHM
:
FLOWCHART
:
PROGRAM:
RESULT:
PROBLEM Write a function to find out whether given
STATEMENT: numbers are relatively prime or not.
PROGRAM 6
1. Start
2. Declare Function: void prime(int x, int y);
ALGORITHM: 3. Main Function:
4. Declare variables int a, b
5. Call prime(a, b);
Prime Function:
6. Define function void prime(int m, int n)
7. Declare variables int lcm, gcd, remainder, denominator, numerator
8. Ask users to enter two numbers
9. Read m and n.
10. Set numerator = (m > n) ? m : n
11. Set denominator = (m < n) ? m : n
12. Compute remainder = numerator % denominator
13. While remainder != 0:
Set numerator = denominator
Set denominator = remainder
Compute remainder = numerator % denominator
14. Set gcd = denominator
15. If gcd == 1:
16. Print "The numbers are relatively prime"
17. Else:
18. Print "The numbers are not relatively prime"
19. End
FLOWCHART:
PROGRAM:
RESULT:
CONCLUSION: Throughout this experiment, I gained insights into the importance of
functions within a program. In C programming, functions are
employed to divide intricate tasks into smaller, reusable code
segments. The utilization of functions can enhance the organization,
comprehensibility, and reusability of code, thereby simplifying
program management.
Here are some benefits of using functions in C:
1) Reusability: You can call functions many times and give them
different inputs.
2) Modularity: Functions can be employed to allocate distinct tasks to
each function, resulting in modular and easily debuggable code.
3) Abstraction: Functions provide a layer of abstraction to simplify
things.
4) Flexibility and optimization: Leveraging parameters and return
types can significantly improve flexibility and optimization.