Lab 8: User-Defined Functions in C

Lab Report Information
Lab No.: 8
Title: User-Defined Functions in C
Course: Computer Programming (CT 101), I Year I Part
Credit: Important Notes
C Programming Lab Report: Theory for Lab 8
User-Defined Functions in C
This C Programming Lab Report focuses on User-Defined Functions in C. A function is a self-contained block of code that performs a specific task. They are the cornerstone of modular programming, allowing developers to write reusable, readable, and easily debuggable code.
Key Aspects of a User-Defined Function:
- Function Declaration (Prototype): Informs the compiler about the function’s name, return type, and parameters before it is used. Syntax:
return_type function_name(parameter_list);
- Function Definition: Contains the actual code that executes when the function is called.
- Function Call: Invokes the function to perform its task.
Recursion
Recursion is a technique where a function calls itself. A recursive function must have a **base case** to stop the recursion and a **recursive step** that moves it closer to the base case. It is ideal for problems that can be broken down into smaller, self-similar subproblems.
C Programming Lab Report: Programs for Lab 8
1. Multiplication Table Using a Function
Question:
Write a program to display the multiplication table of a number using a function with no arguments and no return type.
Program Code:
#include <stdio.h> #include <conio.h> void multiplication(); void main() { multiplication(); getch(); } void multiplication() { int n, i; printf("\nEnter number: "); scanf("%d", &n); printf("\nThe multiplication table is:\n"); for (i = 1; i <= 10; i++) { printf("%d x %d = %d\n", n, i, n * i); } }
Output:
The multiplication table is:
5 x 1 = 5
... (and so on) ...
5 x 10 = 50
Algorithm:
- Start
- Declare the function `multiplication()`.
- Call `multiplication()` from `main()`.
- In `multiplication()`, read a number `n`.
- Use a `for` loop from 1 to 10 to calculate and print each line of the multiplication table.
- Stop.
2. Factorial using Recursion
Question:
Write a program to find the factorial of a number using a recursive function.
Program Code:
#include <stdio.h> #include <conio.h> int fact(int); int main() { int n, a; printf("\nEnter number: "); scanf("%d", &n); a = fact(n); printf("\nThe factorial is: %d", a); return 0; } int fact(int x) { if (x == 1 || x == 0) { return 1; // Base case } else { return x * fact(x - 1); // Recursive step } }
Output:
The factorial is: 720
Algorithm:
- Start
- Get a number `n` from the user.
- Call the recursive function `fact(n)`.
- In `fact(x)`, if `x` is 0 or 1 (base case), return 1.
- Otherwise (recursive step), return `x * fact(x - 1)`.
- Print the final result returned to `main()`.
- Stop.
3. Prime or Composite Check Using a Function
Question:
Write a program to check if a number is prime or composite using a function.
Program Code:
#include <stdio.h> #include <conio.h> int prime(int); int main() { int n, result; printf("\nEnter number: "); scanf("%d", &n); result = prime(n); if (result == 1) { printf("\nThe number is prime."); } else { printf("\nThe number is composite."); } return 0; } int prime(int x) { if (x <= 1) return 0; for (int i = 2; i <= x / 2; i++) { if (x % i == 0) { return 0; // Composite } } return 1; // Prime }
Output:
The number is prime.
Algorithm:
- Start
- Read an integer `n`.
- Call function `prime(n)`.
- In `prime(x)`, if `x <= 1`, return 0.
- Loop from 2 to `x/2`. If `x` is divisible by any number in this range, return 0.
- If the loop finishes, return 1.
- In `main`, if the return value is 1, print "Prime", otherwise print "Composite".
- Stop.
4. Perfect Numbers in an Interval
Question:
Write a program to print all perfect numbers between a given interval using a function.
Program Code:
#include <stdio.h> #include <conio.h> void perfect(); void main() { perfect(); getch(); } void perfect() { int i, j, l, u, sum; printf("Enter lower and upper limit: "); scanf("%d %d", &l, &u); printf("Perfect numbers in range are: "); for (i = l; i <= u; i++) { sum = 0; if (i == 0) continue; for (j = 1; j <= i / 2; j++) { if (i % j == 0) { sum += j; } } if (sum == i) { printf("%d\t", i); } } }
Output:
Perfect numbers in range are: 6 28 496
Algorithm:
- Start
- Read a lower and upper limit.
- Loop through each number `i` in the range.
- For each `i`, find the sum of its proper divisors.
- If the sum equals `i`, print `i`.
- Stop.
5. Nth Fibonacci Term using Recursion
Question:
Write a program to generate the nth Fibonacci term using recursion.
Program Code:
#include <stdio.h> #include <conio.h> int fibo(int); void main() { int n, i; printf("\nEnter number of terms: "); scanf("%d", &n); printf("\nThe Fibonacci series is:\n"); for (i = 0; i < n; i++) { printf("%d\t", fibo(i)); } getch(); } int fibo(int x) { if (x == 0) return 0; if (x == 1) return 1; return (fibo(x - 1) + fibo(x - 2)); }
Output:
The Fibonacci series is:
0 1 1 2 3
Algorithm:
- Start
- Read the number of terms `n`.
- Loop from `i=0` to `n-1`, calling `fibo(i)` in each iteration.
- In `fibo(x)`, if `x` is 0, return 0. If `x` is 1, return 1 (base cases).
- Otherwise, return `fibo(x-1) + fibo(x-2)` (recursive step).
- Print the returned value in each loop iteration.
- Stop.
6. Find Maximum and Minimum Elements in an Array
Question:
Write a program to find the maximum and minimum elements in an array using a function.
Program Code:
#include <stdio.h> #include <conio.h> void findMaxMin(int a[], int n); int main() { int n, i, a[100]; printf("Enter the number of elements: "); scanf("%d", &n); printf("Enter %d numbers: ", n); for (i = 0; i < n; i++) { scanf("%d", &a[i]); } findMaxMin(a, n); return 0; } void findMaxMin(int a[], int n) { int max = a[0]; int min = a[0]; for (int i = 1; i < n; i++) { if (a[i] > max) max = a[i]; if (a[i] < min) min = a[i]; } printf("Maximum number is %d\n", max); printf("Minimum number is %d\n", min); }
Output:
Enter 5 numbers: 1 5 8 7 6
Maximum number is 8
Minimum number is 1
Algorithm:
- Start
- Read the size and elements of an array.
- Pass the array to the `findMaxMin` function.
- In the function, initialize `max` and `min` to the first element.
- Loop through the rest of the array, updating `max` and `min` as necessary.
- Print `max` and `min`.
- Stop.
7. HCF and LCM of Two Numbers
Question:
Write a program to find the HCF and LCM of two numbers using functions.
Program Code:
#include <stdio.h> #include <conio.h> int hcf(int, int); int lcm(int, int); int main() { int a, b; printf("Enter two numbers: "); scanf("%d %d", &a, &b); printf("HCF = %d\n", hcf(a, b)); printf("LCM = %d\n", lcm(a, b)); return 0; } int hcf(int a, int b) { while (b != 0) { int temp = b; b = a % b; a = temp; } return a; } int lcm(int a, int b) { return (a * b) / hcf(a, b); }
Output:
HCF = 25
LCM = 50
Algorithm:
- Start
- Read two numbers, `a` and `b`.
- Call the `hcf` function, which uses the Euclidean algorithm to find the HCF.
- Call the `lcm` function, which calculates the LCM using the formula `(a*b)/hcf(a,b)`.
- Print the results from both functions.
- Stop.
Discussion and Conclusion
This C Programming Lab Report on User-Defined Functions in C explored the implementation of modular programming. The exercises covered various function types and demonstrated how to break down problems into reusable modules, improving code organization and readability. The concept of recursion was introduced through factorial and Fibonacci problems, highlighting how a function can call itself to solve complex problems. Overall, this lab provided a solid practical understanding of creating and utilizing functions to write more efficient and modular C programs.