Program to find multiplication using recursion.
mul(a,b) = a b=1 // Base case
= a + mul(a,b-1) b>1 // Recursive case
// recursion program on multiplication
#include<stdio.h>
int a,b,c; /* global variable */
int mul(int,int); /* function declaration */
void main()
{
int c;
printf("\n enter any two numbers:\n");
scanf("%d%d",&a,&b);
c=mul(a,b);
printf("multiplication of %d and %d = %d",a,b,c);
}
int mul(int a,int b)
{
if(b==1)
return a;
else
c=a+mul(a,b-1);
return (c);
}
output:
mul(a,b) = a b=1 // Base case
= a + mul(a,b-1) b>1 // Recursive case
// recursion program on multiplication
#include<stdio.h>
int a,b,c; /* global variable */
int mul(int,int); /* function declaration */
void main()
{
int c;
printf("\n enter any two numbers:\n");
scanf("%d%d",&a,&b);
c=mul(a,b);
printf("multiplication of %d and %d = %d",a,b,c);
}
int mul(int a,int b)
{
if(b==1)
return a;
else
c=a+mul(a,b-1);
return (c);
}
output:
No comments:
Post a Comment