Recursion in C

The Recursion refers to a situation when a function calls itself directly or indirectly.In direct recursion,Function calls itself.And in indirect recursion,Function calls other function and its calls back first function.C supports Recursion and it is useful in many programs like a program to find a factorial of any number by using recursion.Following is a program created with a recursive function from which we can print the factorial of any number.
Factorial: Suppose we want to find a factorial of any number N.Then factorial of number N can be given as n*(n-1)*(n-1)*(n-1)*(n-1)......[n-(n-1)].For example we want to find a factorial of 5 then it will be 5*4*3*2*1=120.

Program:

#include<stdio.h>
#include<conio.h>
int fact(int);
main()
{
    int n,ans;
    clrscr();
    printf("Enter number: ");
    scanf("%d",&n);
    ans=fact(n);
    printf("\nFactorial of %d is %d",n,ans);
    getch();
}
int fact(int x)
{
    if(x==1)
    {
        return 1;
    }
    else
    {
        x*=fact(x-1);
        return x;
    }
}

Recommened Books for C

1.The c programming language - by Dennis M.Ritchie and Brian Kernighan.This book is very effective to learn c programming language.Complete exercises for more understanding.This book is Highly recommened to those who want to become c programmer.





2.C for dummies - by Dan Gookin.there are two volumes of this book.First volume covers basics of c programming language and after completing 1st volume, second volume contains more difficult topics related to c programming language.


3.Head first C - by David Griffiths and Dawn Griffiths is also the best book to learn c programming language.In this book you have to concentrate more than others.By this book also you can become a c programmer.


opening file in C

the fopen() function in c programming language is used to open any file to view or edit or append according to given mode.Following is how to declare fopen() function and how to declare it.

Declaration:
FILE *fopen(const char *filename,const char *mode);

there are two types of parameters in fopen function they are filename and in which mode we have to open it.Following are some modes to open file by fopen() function in c programming language.

Mode

Description


r
This mode is used to read the file

w
this mode is used to write the file and if it is already exist then it will delete old contents and create new file.

a
this mode is used to append the data to the end of the file and if it doesn't exist then it will create new file.

r+
Open a file for update both reading and writing but The file must exist
w+create an empty file for both reading and writing.
a+open a file for reading and appendng.