Saturday 31 August 2013

Recursion | C Programming Language Tutorial pdf



Recursion :

A function calling itself again and again to compute a value is known as recursive function or recursion function or recursion. Normally a function is called by the main program or by some other function but in recursion the same function is called by itself repeatedly.
Use of recursion function :
1. Recursion functions are written less number of statements.
2. Recursion is effective where terms are generated successively to compute value.
3. Recursion is useful for branching process. Recursion helps to create short code that would otherwise be impossible .
Program: Write a recursive function to find the factorized of a given integer. Use it to find ncr = n1 r1 (n-r);

PROGRAMS :
1.Write a function to add two matrices of order m x n. also write main program which can road the values of the matrices and print the resultant matrix.
# include ,stdio?h>
# include <con:o.n>
# include <stdl b.h>
int a(10) (10), b(10)(10), c(10)(10), m,n,I,j;, /* global declarationjs */
main ( )
{
void matadd( );
clrscr();
printf(“ in how many rows and (columns?”)?
scanf(“ %d % d; & m, & n);
printf(” /n Enter A matrix values in”)
for (i=0; i<n ; i**)
for (j=0; j<n; j**)
scanf (“% d”, & a(i) (j));
matadd(); /* call function *
printf(“in resultant matrix is /n “);
for (i=d; i<m; i+ +)
{
for ( j =0; j<n : j++)
printf(“:%d”. c(i)(j));
printf (;,n”);
}
getch ( );
}
void matadd ( ) // * function def */
{
for (i=0; i<m; i++)
for (j=0; j<n; j++)
c[i][j] = a[i][j] + b[i][j];
}
When this program is executed, the user has to enter the order of matrix m and n and the values of the matrices. A sample input and out put of the program is
How many rows and columns ? 2 2
Enter A matrix values
2 -2
0 4
Enter B matrix values
6 2
3 -5
Resultant matrix is
7 0
4 -1

2.Write a function in C to find the GCD of two integers. 13 Using the function, write a program to find the GCD of 3 integers
# include <stdio.h>
# include <coni0.h>
main()
{
int a,b,c,d1, d2,d3;
int gcd (int, int);
clrscr();
printf(“ in Enter three integers : “);
scanf (“%d %d ; & a, & b, & c);
d1 = gcd(a,b);
d2 = gcd (a,c)
d3 = gcd(b,c);
if(d1= = d3)
printf (“ in greatest common divisior is %d”, d1);
else
printf (“in greatest common divisior is % d”, gcd (d1,d3));
else
printf („in greatest common divisior is %d”, gcd (d1,d3));
printf (“ in in press any key to continue… “);
}
int gcd(int x, int y)
{
int nr, dr, r;
if(x>=y)
{
nr= x;
dr = y;
}
r = nr.% dr;
return (dr);
}
When the program is executed, the user has to enter the three integers. The function is called to find the common divisor in (a,c), (A,C) and (b,c). if the GCD obtained in these three calls are equal then the GCD will be displayed. Otherwise the function is called to find the common divisor in (d1,d3) or (d1,d2) to print the greatest common divisor
Output:
Enter three integers : 45 27 81
Greatest common divisor is 9
Press any key to continue.

3.Write a C program which calls a function reverse ( ) which excepts a string and displays its reverse.
The string is read in the main program and the function called with this string as an argument to print the reverse of the string.
# include < stdio?h>
# include < conio.h>
# include < string.h>
main ()
{
char reverse (char s( j);
char s((20);
clrscr();
printf (“ in Enter a string:”)?
gets(st); /* call the function to print the reverse string */
printf (“ in %s is reversed as %s”, st, reverse (st));
getch ( );
}
char reverse (char str ( ))
{
int i,j;
char rst (30);
j=0;
i=strlen (st)-1;
while(i > = 0)
{
rst[j] = st[i]
i---;
j++;
}
rst[j] = „\0?;
return (rst);
}
When this program is executed, the user has to enter a string and the reverse of string will be printed .
OutPut:
enter a string : WELCOME
WELCOME is reversed as EMOCLEW

0 comments:

Post a Comment

 

C Programming Language Interview Questions and Answers Tutorial for beginners. Copyright 2013 All Rights Reserved