ex 7_ functions in c
TRANSCRIPT
-
7/28/2019 Ex 7_ Functions in c
1/4
FUNCTIONS
Ex.No. 7a
SEQUENTIAL AND BINARY SEARCH USING FUNCTIONSAim
Towrite a program in C to perform sequential and binary search using functions
Algorithm
1. Start.2. Accept the limit ,n
3. Read all the n elements in the array a[n]
4. Accept the element, s, to search.
5. call the function, seq(a,s,n) for sequential search6. call the function, bin(a,s,n) for binary search
7. Stop
Function seq(a,s,n)
1. Start
2. Initialize variables big and small to a[0] and posn to 1 respectively.3. For i = 0 to n-1 do step 4
4. if (s==a(i)) then posn = i
5. if (posn>-1) then print The given element is found at position : , posn6. if (posn==-1) then print The given element is not found in the array
7. Stop
Function bin(a,s,n)
1. Start
2. Initialize left=0 right =n3. while left a[mid] ,left=mid +1
6. else if value < a[mid], right=mid-17. else return mid
8. else return not found
9. Stop
Sample output:
Enter No of elements: 3
Enter Array elements: 2,6,9
Enter Searching element: 6
The given element is found at position :2.
-
7/28/2019 Ex 7_ Functions in c
2/4
Ex.No. 7b
Finding nCr value
Aim
Towrite a program in C to find the nCr value of the given numbers
Algorithm
1. Start.
2. Accept the numbers, n and r.
3. Find the factorial of n, r and n-r value using the function fact().
4. Find nCr using the formula nCr=n!/ (n-r)!r!5. Print the nCr value
6. Stop
Function fact(n)
1. Start
2. Initialise f = 13. For i = 1 to n do step 4
4. Multiply f and i and store it in f.
5. Return f
Sample output:
Enter the value of n: 3
Enter the value of r: 2
nCr value is :3.
-
7/28/2019 Ex 7_ Functions in c
3/4
Ex.No. 7c
FACTORIAL USING RECURSION
Aim
Towrite a program in C to calculate factorial of the given number using functions
Algorithm
1. Start2. Enter the number,n for which the factorial has to be calculated
3. Call the function, fact(n)
4. Print the factorial of the number
5. Stop
Function fact (n)
1. if n==1 return 1
2. else f=n*fact(n-1)3. return f
Sample Output
Enter the number: 4Factorial : 24
.
-
7/28/2019 Ex 7_ Functions in c
4/4
Ex.No. 7d
FIBONACCI SERIES USING RECURSIONAim
Towrite a program in C to generate Fibonacci series using recursion functions.
Algorithm
1. Start the program.2. Enter the number.
3. Check whether the number is 0 or not
4. If 0 print 0 value and not 0 go further.
5. set loop up to the given number.6. Fib=Fib + a; a=b;b=c;
7. Every increment in the loop prints the value of fib.8. After the execution of the loop stop the program
Sample Output:
Input: 6
Output: 0 1 1 2 3 5