C Program to Find and Print the Product of All Digits of Any Number

Here we will learn about how to create a program in C that will ask the user to enter any number (at run-time) as input and then calculate and print the product of all the digits of that given number. This program will only calculate the non-zero digit's product:

#include<stdio.h>
#include<conio.h>
int main()
{
    int num, prod=1, rem;
    printf("Enter any number: ");
    scanf("%d", &num);
    while(num>0)
    {
        rem = num%10;
        if(rem!=0)
            prod = prod*rem;
        num = num/10;
    }
    printf("\nProduct of Digit = %d", prod);
    getch();
    return 0;
}

As the program was written in the Code::Blocks IDE, here is the output you will see on your screen after a successful build and run. Let's suppose that the user has supplied the input number as 2345 and pressed the ENTER key:

c program product of all digit

Let's take another sample run:

print product of digits of number c

As you can see from the above sample run, 0 is skipped while multiplying the digits of the given number, as if we take 0 along with a non-zero digit, then the product will be 0.

Here are some of the main steps used in the above program:

C Quiz


« Previous Program Next Program »