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
- While Loop in C Language
- For Loop in C Language with Example Programs
- Goto Statement in C
- Division Operator
- Modulus Operator
- Precedence and Associativity of Arithmetic Operators
Product of digits in C Program Algorithm:
- Start by taking the user input and Store the input value in a variable num.
- 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.
- 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.
- 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.
- 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.
- The loop will start from the last digit of the number and continue till we reach the first digit.
- 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 > 0
- 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:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 |
/* Program: Product of digits of number using while loop */ #include<stdio.h> int main() { int num, temp; // goto statement label. If user enters invalid input, we will request for number again. INPUT: // User Input printf("Enter a Positive Number: "); scanf("%d", &num); // product will be one at the start // if it is 0, 0 * anything is always zero. int prod = 1; if(num > 0) { // Number is positive // take backup of 'num' temp = num; // Iterate over each digit using modulus and division operator. while(temp > 0) { // update 'prod' // make sure to warp the (temp%10) with parenthesis // Learn more at - https://sillycodes.com/precedence-of-arithmetic-operators/ prod = prod * (temp%10); // update 'temp' temp = temp / 10; } } else { // Negative Number, Display error message and return. printf("ERROR: Please enter valid number, Try Again.\n"); goto INPUT; } // print the product of digits on console. printf("Product of Digits of %d is: %d \n", num, prod); return 0; } |
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.
1 2 3 4 5 6 7 8 |
$ gcc product-of-digits.c $ ./a.out Enter a Positive Number: 567 Product of Digits of 567 is: 210 $ ./a.out Enter a Positive Number: 1234 Product of Digits of 1234 is: 24 $ |
What if user enters Invalid Input
1 2 3 4 5 6 7 8 |
$ ./a.out Enter a Positive Number: -45 ERROR: Please enter valid number Enter a Positive Number: 0 ERROR: Please enter a valid number, Try Again. Enter a Positive Number: 8975 Product of Digits of 8975 is: 2520 $ |
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 > 0; temp = temp/10)
So let’s rewrite above program with for loop. (Note, The program logic will remain same)
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 |
/* Program: Product of digits of number using for loop */ #include<stdio.h> int main() { int num, temp; // goto statement label. If user enters invalid input, we will request for number again. INPUT: // User Input printf("Enter a Positive Number: "); scanf("%d", &num); // product will be one at the start // if it is 0, 0 * anything is always zero. int prod = 1; if(num > 0) { // Number is positive // Iterate over each digit using modulus and division operator. // As it is for loop, We can use the for loops Init and Update // steps to initialize and update 'temp' variable for(temp = num; temp > 0; temp = temp/10) { // update 'prod' // make sure to warp the (temp%10) with parenthesis // Learn more at - https://sillycodes.com/precedence-of-arithmetic-operators/ prod = prod * (temp%10); } } else { // Negative Number, Display error message and return. printf("ERROR: Please enter valid number, Try Again.\n"); goto INPUT; } // print the product of digits on console. printf("Product of Digits of %d is: %d \n", num, prod); return 0; } |
Program Output:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 |
$ gcc product-of-digits.c $ ./a.out Enter a Positive Number: 876 Product of Digits of 876 is: 336 $ ./a.out Enter a Positive Number: 543 Product of Digits of 543 is: 60 $ ./a.out Enter a Positive Number: -45 ERROR: Please enter valid number, Try Again. Enter a Positive Number: -2123 ERROR: Please enter valid number, Try Again. Enter a Positive Number: -9 ERROR: Please enter valid number, Try Again. Enter a Positive Number: 345 Product of Digits of 345 is: 60 $ |
7 Responses
[…] Program to calculate Product of Digits of a Number in C […]
[…] C Program to Calculate Product of Digits of a Number […]
[…] C Program to Calculate Product of Digits of a Number […]
[…] C Program to Calculate Product of Digits of a Number […]
[…] C Program to Calculate Product of Digits of a Number […]
[…] ▶️Related Program: Product of Digits of a Number using Iterative Method […]
[…] C Program to Calculate Product of Digits of a Number […]