Program to calculate product of digits of a number
Given a number, the task is to find the product of the digits of a number.
Examples:
Input: n = 4513 Output: 60 Input: n = 5249 Output: 360
General Algorithm for product of digits in a given number:
- Get the number
- Declare a variable to store the product and set it to 1
- Repeat the next two steps till the number is not 0
- Get the rightmost digit of the number with help of remainder ‘%’ operator by dividing it with 10 and multiply it with product.
- Divide the number by 10 with help of ‘/’ operator
- Print or return the product
Below is the solution to get the product of the digits:
C++
// cpp program to compute // product of digits in the number. #include<bits/stdc++.h> using namespace std; /* Function to get product of digits */int getProduct(int n) { int product = 1; while (n != 0) { product = product * (n % 10); n = n / 10; } return product; } // Driver program int main() { int n = 4513; cout << (getProduct(n)); } // This code is contributed by // Surendra_Gangwar |
chevron_right
filter_none
Java
// Java program to compute // product of digits in the number. import java.io.*; class GFG { /* Function to get product of digits */ static int getProduct(int n) { int product = 1; while (n != 0) { product = product * (n % 10); n = n / 10; } return product; } // Driver program public static void main(String[] args) { int n = 4513; System.out.println(getProduct(n)); } } |
chevron_right
filter_none
Python3
# Python3 program to compute # product of digits in the number. # Function to get product of digits def getProduct(n): product = 1 while (n != 0): product = product * (n % 10) n = n // 10 return product # Driver Code n = 4513print(getProduct(n)) # This code is contributed # by mohit kumar |
chevron_right
filter_none
C#
// C# program to compute // product of digits in the number. using System; class GFG { /* Function to get product of digits */ static int getProduct(int n) { int product = 1; while (n != 0) { product = product * (n % 10); n = n / 10; } return product; } // Driver program public static void Main() { int n = 4513; Console.WriteLine(getProduct(n)); } } // This code is contributed by Ryuga |
chevron_right
filter_none
PHP
<?php <?php // PHP program to compute // $product of digits in the number. /* Function to get $product of digits */ function getProduct($n) { $product = 1; while ($n != 0) { $product = $product * ( $n % 10); $n = intdiv($n , 10); } return $product; } // Driver code $n = 4513; echo getProduct($n); // This code is contributed by // ihritik ?> |
chevron_right
filter_none
Output:
60
Recommended Posts:
- Check whether product of digits at even places is divisible by sum of digits at odd place of a number
- Number of digits in the product of two numbers
- Maximum sum and product of the M consecutive digits in a number
- Smallest number k such that the product of digits of k is equal to n
- Sum and Product of digits in a number that divide the number
- Check if product of digits of a number at even and odd places is equal
- Check whether product of digits at even places of a number is divisible by K
- Maximum of sum and product of digits until number is reduced to a single digit
- Program for Sum the digits of a given number
- Program to check if a given number is Lucky (all digits are different)
- Program to check if a number is divisible by any of its digits
- Write a program to reverse digits of a number
- Program to check if a number is divisible by sum of its digits
- Count of integers in a range which have even number of odd digits and odd number of even digits
- Find last k digits in product of an array numbers
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.



