# C Program to Find Product of Digits Of a Number

How to write a C Program to Find Product of Digits Of a Number using For Loop, While Loop, Functions, and Recursion

## C Program to Find Product of Digits Of a Number using For Loop

This program allows the user to enter any positive integer. Next, it will divide the given number into individual digits, and finds the product of those individual digits using For Loop.

```/* C Program to Find Product of Digits Of a Number */

#include <stdio.h>

int main()
{
int Number, Reminder, Product;

printf("\n Please Enter any Number that you wish  : ");
scanf("%d", & Number);

for(Product = 1; Number > 0; Number = Number / 10)
{
Reminder = Number % 10;
Product = Product * Reminder;
}

printf(" \n The Product of Digits of a Given Number =  %d", Product);

return 0;
}```

Within this C Program to Find Product of Digits Of a Number, User Entered value: Number = 234 and Product = 1

First Iteration
Reminder = Number  % 10
Reminder = 234 % 10 = 4
Product = Product * Reminder = 1 * 4 = 4
Number = Number / 10 = 234 / 10 = 23

Second Iteration

From the C Programming first Iteration, the values of both Number and Product has changed as Number = 23 and Product = 4

Reminder = 23 % 10 = 3
Product = Product * Reminder =  4 * 3 = 12
Number = Number / 10 = 23 / 10 = 2

Third Iteration

From the Second Iteration, the values of both Number and Product has changed as Number = 2 and Product = 12

Reminder = Number  % 10 = 2 % 10 = 2
Product = Product * Reminder =  12 * 2 = 24
Number = Number / 10 = 2 / 10 = 0

Here Number = 0 so, the condition inside the For Loop will fail

## C Program to Find Product of Digits Of a Number using While Loop

This program to calculate product of digits in a number is same as above but, we just replaced the For loop with While Loop.

```/* C Program to Find Product of Digits Of a Number using While Loop */

#include <stdio.h>

int main()
{
int Number, Reminder, Product = 1;

printf("\n Please Enter any Number that you wish  : ");
scanf("%d", & Number);

while(Number != 0)
{
Reminder = Number % 10;
Product = Product * Reminder;
Number = Number / 10;
}

printf(" \n The Product of Digit of a Given Number =  %d", Product);

return 0;
}```
`````` Please Enter any Number that you wish  : 4568

The Product of Digit of a Given Number =  960``````

## Program to Find Product of Digits Of a Number Using Functions

This program is the same as the first example. But, we separated the code using Functions.

```/* C Program to Find Product of Digits of a Number Using Functions */

#include <stdio.h>

int Product_Of_Digits (int);

int main()
{
int Number, Product = 1;

printf("\n Please Enter any number  :  ");
scanf("%d", &Number);

Product = Product_Of_Digits (Number);

printf("\n The Product of all the digits of Given Number = %d", Product);
return 0;
}

int Product_Of_Digits (int Number)
{
int Reminder, Product;

for (Product = 1; Number > 0; Number = Number / 10)
{
Reminder = Number % 10;
Product = Product * Reminder;
}

return Product;
}```
`````` Please Enter any number  :  7825

The Product of all the digits of Given Number = 560``````

## C Program to Find Product of Digits Of a Number using Recursion

This program will find the product of individual digits in a given number by calling the function recursively. Please refer Recursion in C for further reference

```/* C Program to Find Product of Digits of a Number using Recursion */

#include <stdio.h>

int Product_Of_Digits (int);

int main()
{
int Number, Product = 1;

printf("\n Please Enter any number :  ");
scanf("%d", &Number);

Product = Product_Of_Digits (Number);

printf("\n The Product of all the digits of Given Number = %d", Product);
return 0;
}

int Product_Of_Digits (int Number)
{
static int Reminder, Product = 1;

if(Number > 0)
{
Reminder = Number % 10;
Product = Product * Reminder;
Product_Of_Digits (Number / 10);
return Product;
}
else
return 0;
}```
`````` Please Enter any number :  456

The Product of all the digits of Given Number = 120``````

Within this Program to Find Product of Digits Of a Number, Inside the function, the Static variable will initialize the value only when the function called the first time.

Product_Of_Digits (Number / 10) statement will call the function Recursively with the updated value. If you miss this statement, after completing the first line, it will terminate. For example, Number = 4567 and the output will be 7