C Program to Find Product of Digits of a Number

In this post, we will learn how to find the product of digits of a number using the C Programming language.

The below program asks the user to enter a number, then it calculates the product of its digit. For example: If the user enters a number 2436, then the product of its digits will be 2 x 4 x 3 x 6 = 144.

We will find the product of digits using the following approaches:

  1. Using While Loop
  2. Using For Loop
  3. Using Functions

So, without further ado, let’s begin this tutorial.

C Program to find Product of Digits in a Number

// C Program to Find Product of Digits of a Number
#include <stdio.h>
int main(){
    int n, rem, prod = 1;
    
    // Asking for input
    printf("Enter a number: ");
    scanf("%d", &n);
    
    // Calculating product using while loop
    while (n != 0){
        rem = n % 10;
        prod = prod * rem;
        n = n / 10;
    }
    
    // Displaying output
    printf("Product of digits is: %d", prod);
    return 0;
}

Output

Enter a number: 5634
Product of digits is: 360

How Does This Program Work ?

    int n, rem, prod = 1;

In this program, we have declared three integer data type variables named n, rem and prod. The prod variable has assigned a value of 1.

    // Asking for input
    printf("Enter a number: ");
    scanf("%d", &n);

The user is asked to enter a number. The entered value gets stored in the n named variable.

    // Calculating product using while loop
    while (n != 0){
        rem = n % 10;
        prod = prod * rem;
        n = n / 10;
    }

The product of digits of the entered number is calculated using the while loop.

Suppose, the user enters a number 5634, then:

  • 1st While Loop Iteration: while (5634 != 0), the condition is True.
    • rem = n % 10 = 5634 % 10 = 4
    • prod = prod * rem = 1 * 4 = 4
    • n = n / 10 = 5634 / 10 = 563
  • 2nd While Loop Iteration: while (563 != 0), the condition is True.
    • rem = n % 10 = 563 % 10 = 3
    • prod = prod * rem = 4 * 3 = 12
    • n = n / 10 = 563 / 10 = 56
  • 3rd While Loop Iteration: while (56 != 0), this condition is also True.
    • rem = n % 10 = 56 % 10 = 6
    • prod = prod * rem = 12 * 6 = 72
    • n = n / 10 = 56 / 10 = 5
  • 4th While Loop Iteration: while (5 != 0), this condition is True.
    • rem = n % 10 = 5 % 10 = 5
    • prod = prod * rem = 72 * 5 = 360
    • n = n / 10 = 5 / 10 = 0
  • 5th While Loop Iteration: while (0 != 0), here the condition is False, so the while loop terminates and we get prod = 360.
    // Displaying output
    printf("Product of digits is: %d", prod);

The product of digits of the entered number is displayed on the screen using printf() function.

C Program to Find Product of Digits of a Number Using For Loop

// C Program to Find Product of Digits of a Number
#include <stdio.h>
int main(){
    int number, i, rem;
    
    // Asking for input
    printf("Enter a number: ");
    scanf("%d", &number);
    
    for (i = 1; number > 0; number /= 10){
        rem = number % 10;
        i = i * rem;
    }
    
    // Displaying output
    printf("The product of digits is: %d", i);
    return 0;
}

Output

Enter a number: 2436
The product of digits is: 144

C Program to Find Product of Digits of a Number Using Function

// C Program to Find Product of Digits of a Number Using Function
#include <stdio.h>

int digitsProduct(int n){
    int rem, prod = 1;
    
    while (n != 0){
        rem = n % 10;
        prod = prod * rem;
        n = n / 10;
    }
    return prod;
}

int main(){
    int number, result;
    
    // Asking for input
    printf("Enter a number: ");
    scanf("%d", &number);
    
    // Calling out custom function
    result = digitsProduct(number);
    
    // Displaying output
    printf("Product of digits: %d", result);
    return 0;
}

Output

Enter a number: 12345
Product of digits: 120

Conclusion

I hope after going through this post, you understand how to find the product of digits of a number using the C Programming language.

If you have any doubt regarding the program, then contact us in the comment section. We will be delighted to assist you.

Also Read:

Leave a Comment

Your email address will not be published. Required fields are marked *