Appendix
C PROGRAMMING FOR SOME ALGORITHMS
- To write a recursive function to print the factorial of a number and execute it to find the factorial of 6.
#include <stdio.h>
#include <conio.h>
unsigned long fact(int);
void main()
{
unsigned long f;
int n;
clrscr();
printf(“\nEnter a number: “);
scanf(“%d”,&n);
f=fact(n);
printf(“\nFactorial of %d is %ld”,n,f);
getch();
}
unsigned long fact(int n)
{
unsigned long m;
if(n==1)
return(1);
else
m=n*fact(n−1);
return(m);
}
The Execution of the program to find the factorial of 6 is yields:-
Enter a Number:
6
Factorial of 6 is 720
- To write a recursive function to print the Fibonacci series up to a given number and write the series up to 89.
#include <stdio.h>
#include <conio.h>
unsigned long ...
Get Discrete Mathematics now with the O’Reilly learning platform.
O’Reilly members experience live online training, plus books, videos, and digital content from nearly 200 publishers.