Appendix

C PROGRAMMING FOR SOME ALGORITHMS

  1. 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

     

  2. 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, 1st Edition by Pearson now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.