Multiplication using recursion in c





C code to multiply two numbers by recursion:

#include<stdio.h>

int multiply(int,int);

int main(){

    int a,b,product;
    printf("Enter any two integers: ");
    scanf("%d%d",&a,&b);

    product = multiply(a,b);

    printf("Multiplication of two integers is %d",product);

    return 0;
}

int multiply(int a,int b){

    static int product=0,i=0;

    if(i < a){
         product = product + b;
         i++;
         multiply(a,b);
    }

    return product;
}

Sample output:

Enter any two integers: 5 8
Multiplication of two integers is 40

C code to multiply two numbers without using recursion:

#include<stdio.h>

int multiply(int,int);

int main(){

    int a,b,product;
    printf("Enter any two integers: ");
    scanf("%d%d",&a,&b);

    product = multiply(a,b);

    printf("Multiplication of two integers is %d",product);

    return 0;
}

int multiply(int a,int b){

    int product =0,i=0;

    while(i < a){
         product = product + b;
         i++;
    }

    return product;
}





Sum of n numbers using recursion in c
Matrix multiplication using recursion in c
Multiplication using recursion in c
Lcm using recursion in c
Using recursion in c find the largest element in an array
Prime number program in c using recursion
Decimal to binary conversion in c using recursion
C program for fibonacci series using recursion
Reverse a string using recursion
Write a program for palindrome using recursion
Find factorial of a number using recursion in c program
Find gcd of a number using recursion in c program
Find sum of digits of a number using recursion using cprogram
Find power of a number using recursion using c program
Binary search through recurssion using c program
Reverse a number using recursion in c program
Big list of c program examples

8 comments:

Anonymous said...

y como seria el mismo programa pero con numeros demasiados grandes, es decir, usando cadenas para poder imprimirlas
por cierto buen programa

Unknown said...

int multiply(int a,int b){
if (b == 0) return 0;
return a + multiply(a, b - 1);
}

Anonymous said...

please explain........ how it wrkss???????????????????

Unknown said...

keep it up... (y)

Anonymous said...

this is will be better when improved because when the first number entered is a negative number and when both numbers are negative it does not give the right answer.

LOVE STORIES said...

Awesome and easy logic from sudipto choudury

don said...

int multiply(int a,int b,int i)
{
if(i>a-1)
return(0);
else
return(b+multiply(a,b,i+1));


}

Somebody9 said...

Be aware! The author's original code for recursion does not work.