C Programming Code Examples C > For Loops and While Loops Code Examples C program to find product of digits of a number C program to find product of digits of a number Write a C program to input a number from user and calculate product of its digits. How to find product of digits of a number using loop in C programming. I have divided the logic to calculate product of digits in three steps. Extract last digit of the given number. Multiply the extracted last digit with product. Remove the last digit by dividing number by 10. Input a number from user. Store it in some variable say j. Initialize another variable to store product i.e. product = 1. Now, you may think why I have initialized product with 1 why not 0? This is because we are performing multiplication operation not summation. Multiplying a number with 1 returns same, so as summation with 0 returns same. Hence, I have initialized product with 1. Find last digit of number by performing modulo division by 10 i.e. lastDigit = j % 10. Multiply last digit found above with product i.e. product = product * lastDigit. Remove last digit by dividing the number by 10 i.e. j = j / 10. Repeat step 3-5 till number becomes 0. Finally you will be left with product of digits in product variable. #include <stdio.h> int main() { int n; long long product=1ll; /* Input number from user */ printf("Enter any number to calculate product of digit: "); scanf("%d", &n); /* Repeat the steps till n becomes 0 */ while(n != 0) { /* Get the last digit from n and multiplies to product */ product = product * (n % 10); /* Remove the last digit from n */ n = n / 10; } printf("Product of digits = %lld", product); return 0; }