Learn about Addition of two numbers Using a recursive function in C in the below code example. Also refer the comments in the code snippet to get a detailed view about what’s actually happening.
Contents
Addition of two numbers Using a recursive function in C
Program:
#include<stdio.h>
int add(int x, int y)
{
if(y == 0)
return x;
else
return add( x^y, (x & y) << 1);
}
int main()
{
int a, b, sum;
printf("Enter two numbers: ");
scanf("%d %d", &a, &b);
sum = add(a,b);
printf("Sum = %d\n",sum);
return 0;
}
Output:
Enter two numbers: 6 2
Sum = 8
Hope above code works for you and Refer the below Related Codes to gain more insights. Happy coding and come back again.
Similar Codes :
Add Two Numbers Without Using the Addition Operator in C
C program to find LCM using recursion