C Program to find Product of 2 Numbers using Recursion


This C program using recursion, finds the product of 2 numbers without using the multiplication operator.
Here is the source code of the C program to display a linked list in reverse. The C program is successfully compiled and run on a Linux system. The program output is also shown below.
  1. /*
  2.  * C Program to find Product of 2 Numbers using Recursion
  3.  */
  4. #include <stdio.h>
  5.  
  6. int product(int, int);
  7.  
  8. int main()
  9. {
  10.     int a, b, result;
  11.  
  12.     printf("Enter two numbers to find their product: ");
  13.     scanf("%d%d", &a, &b);
  14.     result = product(a, b);
  15.     printf("Product of %d and %d is %d\n", a, b, result);
  16.     return 0;
  17. }
  18.  
  19. int product(int a, int b)
  20. {
  21.     if (a < b)
  22.     {
  23.         return product(b, a);
  24.     }
  25.     else if (b != 0)
  26.     {
  27.         return (a + product(a, b - 1));
  28.     }
  29.     else
  30.     {
  31.         return 0;
  32.     }
  33. }

$ cc pgm20.c
$ a.out
Enter two numbers to find their product: 176 340
Product of 176 and 340 is 59840

0 comments:

Post a Comment

 
Top