codescracker


c

C Program to Find and Print Product of all Digit of any Number



« C Tutorial C Examples »

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

// Write a program in C to print the product of
// digit of any given Number by user at run-time
// In this program, if zero present as number's digit
// then skip it and continue to multiply only
// non-zero digits of the given number
// -----------codescracker.com-----------

#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 under Code::Blocks IDE, therefore after successful build and run, here is output you will see on your screen. Let's suppose that user has supplied the input number as 2345 and pressed ENTER key:

c program product of all digit

Let's take another sample run:

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

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


« C Tutorial C Examples »






Tools
Calculator

Quick Links
Signup - Login - Give Online Test