Write a program to calculate the result of following with recursive calls of function.
CODING:
# include <stdio.h>
# include <conio.h>
# include <process.h>
void main ()
{
int s=0,n;
int num(int);
clrscr();
printf ("\n Enter a number : ");
scanf ("%d",&n);
s=num(n);
printf ("\n s=%d",s);
getch();
}
int num (int n)
{
static int s;
s=s+n;
printf (" %d ",n);
if (n>0)
num(--n);
return s;
}
OUTPUT:
Enter a number: 4
4 3 2 1 0
S=10
CODING:
# include <stdio.h>
# include <conio.h>
# include <process.h>
void main ()
{
int s=0,n;
int num(int);
clrscr();
printf ("\n Enter a number : ");
scanf ("%d",&n);
s=num(n);
printf ("\n s=%d",s);
getch();
}
int num (int n)
{
static int s;
s=s+n;
printf (" %d ",n);
if (n>0)
num(--n);
return s;
}
OUTPUT:
Enter a number: 4
4 3 2 1 0
S=10
Comments
Post a Comment