# program to calculate the Product of Digits of number in C Language

## Program Description:

Write a program to calculate the Product of Digits in C language. The program should accept a positive number from the user and calculate the product of all the individual digits. We are going to use the Loops ( while, for, etc), goto statement, and division and modulus operators to calculate the product of digits.

The program should show the error message if the user enters invalid input. Here is examples of inputs and outputs of the program.

### Expected Input and Output:

Example 1:

Input:

Enter a Positive Number: 1234

Output:

Sum of Digits of 1234 is: 24

Example 2:

Input:

Enter a Positive Number: -45

Output:

ERROR: Please enter a valid number, Try Again.

## Prerequisites:

It is recommended to know the basics of the C loops and goto statement and arithmetic operators. Please go through the following links to learn more about the above concepts

## Product of digits in C Program Algorithm:

1. Start by taking the user input and Store the input value in a variable num.
2. Check if the num is a positive number, If not display the error message (ERROR: Please enter a valid number, Try Again.) and Prompt the user to try again, Using the goto statement. We can use the goto statement to jump from one statement to another statement in a function.
3. If the num is a positive number, We proceed further. We initialize prod a variable with 1 to store the Product of digits. we also use the temp to hold the num value.
4. Then, We will use the modulus operator and division operators to get each Digit of number.
• The modulus operator is useful to get the last digit of any number. If we do the modulus of any number with 10, We will get the last digit.
• For example 123 % 10 = 3
• Similarly, We can remove the last digit from the number by dividing the given number by 10.
• For example:  123 / 10  = 12
• So we are going to use the above two operators to get the last digit and remove the last digit.
5. The loop will start from the last digit of the number and continue till we reach the first digit.
6. At each iteration,
• Get the last digit using the modulus operator and then calculate product of the last digit to the prod variable.
• Remove the last digit from the number ( num or temp) by using the division operator.
• Repeat the above steps until our number( temp) becomes less than 0. Which is the stopping condition for our loop. i.e temp &gt; 0
7. Once all iterations are complete, The prodvariable contains the Product of all digits in a given number.

## Product of digits in C Program using while loop:

Couple of things to note here, The initial value of the prod variable must be 1. If we use the , Anything which multiplied by will become zero. So make sure to use the one ( 1).

📢 Initialize the prod variable with 1.

We are also using the Goto Statement if user enters the Invalid Input. The Goto statement will take the program control back to the start of the program ( i.e to INPUT label)

### Program Output:

Compile and run the program.

What if user enters Invalid Input

As you can see from the above output, If the user enters Invalid Input ( -45), The program displayed an Error message ( ERROR: Please enter a valid number, Try Again.) and presented the option to enter the number again.

## Product of digits in C Program Using For loop:

We can use the for loop Initilaztion step and Update step to update the temp variable.

for(temp = num; temp &gt; 0; temp = temp/10)

So let’s rewrite above program with for loop. (Note, The program logic will remain same)

## Tutorial Index:

Venkatesh

Hi Guys, I am Venkatesh. I am a programmer and an Open Source enthusiast. I write about programming and technology on this blog.

### 7 Responses

1. […] Program to calculate Product of Digits of a Number in C […]

2. […] C Program to Calculate Product of Digits of a Number […]

3. […] C Program to Calculate Product of Digits of a Number […]

4. […] C Program to Calculate Product of Digits of a Number […]

5. […] C Program to Calculate Product of Digits of a Number […]

6. […] ▶️Related Program: Product of Digits of a Number using Iterative Method […]

7. […] C Program to Calculate Product of Digits of a Number […]