C Program to Find Product of Two Numbers without Recursion

This is a C program to find product of 2 numbers without using recursion.

Problem Description

This C Program Finds the Product of 2 Numbers without using Recursion.

Problem Solution

This C Program using iteration, finds the product of 2 numbers without using the multiplication operator.

Program/Source Code

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.

/*  
 * C Program to find Product of 2 Numbers without using Recursion
 */
 
#include <stdio.h>
 
int product(int, int);
 
int main()
{
    int a, b, result;
 
    printf("Enter two numbers to find their product: ");
    scanf("%d%d", &a, &b);
    result = product(a, b);
    printf("Product of %d and %d is %d\n", a, b, result);
    return 0;
}
 
int product(int a, int b)
{
    int temp = 0;
 
    while (b != 0)
    {
        temp += a;
        b--;
    }
    return temp;
}
Program Explanation

In this C program, we are reading the two numbers. The product() function is used to compute the product of two numbers. In the product function initially declare the value of ‘temp’ variable as 0.

advertisement

The while loop is used to check the value of ‘b’ variable is not equal to 0. If the condition is true, then execute the loop. Add the value of ‘temp’ and ‘a’ variables and decrement the value of ‘b’ variable. print the product of two numbers.

Runtime Test Cases
 
$ cc pgm19.c
$ a.out
Enter two numbers to find their product:  89  458
Product of 89 and 458 is 40762

Sanfoundry Global Education & Learning Series – 1000 C Programs.

Free 30-Day Python Certification Bootcamp is Live. Join Now!

Here’s the list of Best Books in C Programming, Data-Structures and Algorithms

If you wish to look at other example programs on Simple C Programs, go to Simple C Programs. If you wish to look at programming examples on all topics, go to C Programming Examples.

advertisement
advertisement
Subscribe to our Newsletters (Subject-wise). Participate in the Sanfoundry Certification contest to get free Certificate of Merit. Join our social networks below and stay updated with latest contests, videos, internships and jobs!

Youtube | Telegram | LinkedIn | Instagram | Facebook | Twitter | Pinterest
Manish Bhojasia - Founder & CTO at Sanfoundry
I’m Manish - Founder and CTO at Sanfoundry. I’ve been working in tech for over 25 years, with deep focus on Linux kernel, SAN technologies, Advanced C, Full Stack and Scalable website designs.

You can connect with me on LinkedIn, watch my Youtube Masterclasses, or join my Telegram tech discussions.

If you’re in your 40s–60s and exploring new directions in your career, I also offer mentoring. Learn more here.