346 Programming and Data Structures
pointer 'q' contains the base address of u [ ]. Hence, pointers p and q are global pointers. They can
be accessed through any function.
10.10 RECURSION
The C language supports recursive feature, that is, a function is called repeatedly by itself. Recursion
can be used directly or indirectly. The direct recursion function calls to itself till the condition is
true. In indirect recursion a function calls to another function, then the called function calls to the
calling function.
10.31 Write a program to call the mainO function recursively and perform a sum of 1 to 5
numbers.
# include <8tdio.&>
# include <procaah>
in t x # s;
void m a in (in t);
void main(x)
s=s+x;
p rin tf ( m\n x ® %d s - % d~,x,e);
i f (x*®5)
e x i t (0 );
tnaitt(++x);
>
OUTPUT:
x = 1 s = 1
x = 2 s = 3
x = 3 s = 6
x = 4 s= 10
x = 5 s= 15
Explanation In the above program variables x and y are declared outside main () function.
Initially their values are zero. Followed by it the prototype of function main () is defined. The
variable x is passed through the main () function. The variable x is added to variable s till value of
x reaches 5. Every time function main () is called repeatedly, x is incremented. The result of the
program is displayed at the output. The value of x in main () is 1 because it is a command line
argument.
Functions 347
Table 10.7 Steps of a recursive function
Function call Value of x Value of s (sum)
main(l)
x = 1
s= l ( 0 + 1 ) = 1
main(2)
x = 2 s=3 ( 2 + 1 + 0 ) = 3
main(3) x = 3
s=6 (3 + 2 + l + 0 ) = 6
main(4)
x = 4 s=10 (.4 + 3 + 2 + 1 + 0 ) = 10
main(5)
x = 5
s=15 (5 + 4 + 3 + 2 + l + 0 ) = 15
The recursive function main () is called as in Table 10.7. The analysis of each step is given for
understanding.
1032 Write a program to calculate triangular number of an entered number with recursion
function method.
# include <stdio.h>
# include <conio.h>
void mainO
{
int n , t , t r i num(int) ;
c lr s c r () ;
printf ("\n Enter a Number
scanf ("%d",&n);
t=trijnum(n);
printf ("\n Triangular number of %d is %d'r,n,t);
tri_num( m)
I int f=0;
if (m ~ 0 )
retumif);
else
f=f+m+trijnum(m -l);
return (f);
I
OUTPUT;
Enter a Number: 5
Triangular number 5 is 15
Explanation In the above program a number is entered whose triangular number is to be
calculated. The number is passed to the function tri_num (). The value of variable n is copied to
348 Programming and Data Structures
variable m. In the tri_n u m () function the entered number is added to variable f. The entered
number is decremented and the tri_n u m () function is called repeatedly (recursively) till the
entered number becomes zero.
10.33 Write a program to display the given string using recursive call of a user-defined
function.
# include <stdio.h>
# include <conio.h>
# include <process.h>
char t r [ ] * <rHave a Good Day*;
in t x*0;
void m ain(vo id);
void main ()
{
$2vitch(str[x])
{
case 'H':
clrscrO;
default:
printf ("%c",str[x]);
break;
case '\0':
exit(l);
break;
I
x++;
mainO;
OUTPUT;
Have a Good Day
Explanation In the above program a string "Have a Good Day" is initialized with character
array str[]. The integer variable x is initially zero. The mainO function is called recursively. In
each call variable x is incremented. The switch () case structure controls display of string. If the
first character is H, screen is cleared. The default case displays each character where x is the element
number. If the 0 character is encountered, the program terminates. In the first case break statement
is not taken to execute successive case statements, so that the screen is cleared and the complete
string is displayed

Get Programming and Data Structures now with O’Reilly online learning.

O’Reilly members experience live online training, plus books, videos, and digital content from 200+ publishers.