Assignment 8
Assignment 8
The objective of this assignment is to learn how to use user define function in the program
Assignment:
1. Write a C program to find the factorial of a number and also find the value of nCr using
this function.
Factorial of a number using function:
STEP 1: Declare a function ‘fact’ with input parameter (n) before main function
STEP 2: In main, take the number(n) as user input
STEP 3: Call the function and pass the input parameter (n)
[ factorial = fact (n) ]
STEP 4: In function definition initialize a variable ‘f’ with 1
STEP 5: Start a for loop from i=1 to the number(n)
STEP 6: Multiply the value of ‘f’ with ‘i’ in the loop
STEP 7: Return the final value of ‘f’ to the main function
STEP 8: Print the value of ‘factorial’ in the main function
STEP 2: In main, define 3 matrices : matrix1, matrix2, matrix3 and the required variables
[ int matrix1[10][10], matrix2[10][10], matrix3[10][10], row, col, i, j;]
STEP 6: In function definition add the 2D arrays: matrix1 and matrix2 using 2 nested
for loops i and j.
5. Write a C program to sort all elements of an array in ascending order using marge
sort technique.
[Merge sort first divides the array into equal halves and then combines them in a sorted
manner.]
STEP 1:
6. Write a C program to find the GCD of two numbers using a recursive function, and also
find the GCD of three numbers using this function.
7. Write a C program to find the Fibonacci series up to n term using recursive function
Practice:
8. Write a C program to find maximum and minimum elements in an array using a recursive
function.
9. Write a C program to sort all elements of an array in ascending order using quick sort
technique.
10. Write a C Program to count the frequency of array elements in a 1-D array
11. Write a C program to solve Tower of Hanoi problem
[Objective: Move n disks from 1 rod to another rod with the help of an intermediate rod.