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++
// C++ program to compute
// product of digits in the number.
#include<bits/stdc++.h>
usingnamespacestd;
/* Function to get product of digits */
intgetProduct(intn)
{
intproduct = 1;
while(n != 0)
{
product = product * (n % 10);
n = n / 10;
}
returnproduct;
}
// Driver program
intmain()
{
intn = 4513;
cout << (getProduct(n));
}
// This code is contributed by
// Surendra_Gangwar
Java
// Java program to compute
// product of digits in the number.
importjava.io.*;
classGFG {
/* Function to get product of digits */
staticintgetProduct(intn)
{
intproduct = 1;
while(n != 0) {
product = product * (n % 10);
n = n / 10;
}
returnproduct;
}
// Driver program
publicstaticvoidmain(String[] args)
{
intn = 4513;
System.out.println(getProduct(n));
}
}
Python3
# Python3 program to compute
# product of digits in the number.
# Function to get product of digits
defgetProduct(n):
product =1
while(n !=0):
product =product *(n %10)
n =n //10
returnproduct
# Driver Code
n =4513
print(getProduct(n))
# This code is contributed
# by mohit kumar
C#
// C# program to compute
// product of digits in the number.
usingSystem;
classGFG
{
/* Function to get product of digits */
staticintgetProduct(intn)
{
intproduct = 1;
while(n != 0)
{
product = product * (n % 10);
n = n / 10;
}
returnproduct;
}
// Driver program
publicstaticvoidMain()
{
intn = 4513;
Console.WriteLine(getProduct(n));
}
}
// This code is contributed by Ryuga
PHP
<?php
<?php
// PHP program to compute
// $product of digits in the number.
/* Function to get $product of digits */
functiongetProduct($n)
{
$product= 1;
while($n!= 0)
{
$product= $product* ( $n% 10);
$n= intdiv($n, 10);
}
return$product;
}
// Driver code
$n= 4513;
echogetProduct($n);
// This code is contributed by
// ihritik
?>
Javascript
<script>
// JavaScript program to compute
// product of digits in the number.
// Function to get product of digits
functiongetProduct(n)
{
let product = 1;
while(n != 0)
{
product = product * (n % 10);
n = Math.floor(n / 10);
}
returnproduct;
}
// Driver code
let n = 4513;
document.write(getProduct(n));
// This code is contributed by Manoj.
</script>
Output
60
Time Complexity: O(log10N) Auxiliary Space: O(1)
Method #2:Using string() method:
Convert the integer to string
Traverse the string and multiply the characters by converting them to integer
When this method can be used?: When the number of digits of a number exceeds , we can’t take that number as an integer since the range of long long int doesn’t satisfy the given number. So take input as a string, run a loop from start to the length of the string and increase the sum with that character(in this case it is numeric)
Below is the implementation:
C++
#include <iostream>
usingnamespacestd;
intgetProduct(string str)
{
intproduct = 1;
// Traversing through the string
for(inti = 0; i < str.length(); i++) {
// Since ascii value of
// numbers starts from 48
// so we subtract it from sum
product = product * (str[i] - 48);
}
returnproduct;
}
// Driver Code
intmain()
{
string st = "4513";
cout << getProduct(st);
return0;
}
Java
importjava.io.*;
classGFG {
staticintgetProduct(String str)
{
intproduct = 1;
// Traversing through the string
for(inti = 0; i < str.length(); i++)
{
// Since ascii value of
// numbers starts from 48
// so we subtract it from sum
product *= str.charAt(i) - '0';
}
returnproduct;
}
// Driver Code
publicstaticvoidmain(String[] args)
{
String st = "4513";
System.out.println(getProduct(st));
}
}
//this code is contributed by shivanisinghss2110
Python3
# Python3 program to compute
# product of digits in the number.
# Function to get product of digits
defgetProduct(n):
product =1
# Converting integer to string
num =str(n)
# Traversing the string
fori innum:
product =product *int(i)
returnproduct
# Driver Code
n =4513
print(getProduct(n))
# This code is contributed by vikkycirus
C#
usingSystem;
usingSystem.Collections;
classGFG
{
staticintgetProduct(String str)
{
intproduct = 1;
// Traversing through the string
for(inti = 0; i < str.Length; i++)
{
// Since ascii value of
// numbers starts from 48
// so we subtract it from sum
product = product * (str[i] - 48);
}
returnproduct;
}
// Driver Code
publicstaticvoidMain(String[] args)
{
String st = "4513";
Console.Write(getProduct(st));
}
}
//This code is contributed by shivanisinghss2110
Javascript
<script>
functiongetProduct(str)
{
let product = 1;
// Traversing through the string
for(let i = 0; i < str.length; i++)
{
// Since ascii value of
// numbers starts from 48
// so we subtract it from sum
product = product * (parseInt(str[i]));
}
returnproduct;
}
// Driver Code
let st = "4513";
document.write(getProduct(st));
// This code is contributed by unknown2108
</script>
Output
60
Time Complexity: O(N) Auxiliary Space: O(1)
Method #3: Recursion
Get the number
Get the remainder and pass the next remaining digits
Get the rightmost digit of the number with help of the remainder ‘%’ operator by dividing it by 10 and multiply it to the product.
Divide the number by 10 with help of ‘/’ operator to remove the rightmost digit
Check the base case with n = 0
Print or return the product
C++
//Recursive function to get product of the digits
#include <iostream>
usingnamespacestd;
intgetProduct(intn){
// Base Case
if(n == 0){
return1 ;
}
// get the last digit and multiply it with remaining digits
return(n%10) * getProduct(n/10) ;
}
intmain() {
// call the function
cout<<getProduct(125) ;
return0;
}
Java
// Recursive function to get product of the digits
importjava.util.*;
classGFG
{
staticintgetProduct(intn)
{
// Base Case
if(n == 0){
return1;
}
// get the last digit and multiply it with remaining digits
return(n%10) * getProduct(n/10) ;
}
publicstaticvoidmain(String[] args)
{
// call the function
System.out.println(getProduct(125));
}
}
// This code is contributed by phasing17
Python3
# Python3 program to implement the approach
# Recursive function to get product of the digits
defgetProduct(n):
# Base Case
if(n ==0):
return1
# get the last digit and multiply it with remaining digits
return(n%10) *getProduct(n//10) ;
# Driver Code
# call the function
print(getProduct(125));
# This code is contributed by phasing17
C#
// Recursive function to get product of the digits
usingSystem;
usingSystem.Collections.Generic;
classGFG
{
staticintgetProduct(intn)
{
// Base Case
if(n == 0){
return1 ;
}
// get the last digit and multiply it with remaining digits
return(n%10) * getProduct(n/10) ;
}
publicstaticvoidMain(string[] args)
{
// call the function
Console.WriteLine(getProduct(125));
}
}
// This code is contributed by phasing17
Javascript
// JS program to implement the approach
// Recursive function to get product of the digits
functiongetProduct(n){
// Base Case
if(n == 0){
return1 ;
}
// get the last digit and multiply it with remaining digits
return(n%10) * getProduct(Math.floor(n/10)) ;
}
// Driver Code
// call the function
console.log(getProduct(125));
// This code is contributed by phasing17
Output
10
Time Complexity: O(log10N) Auxiliary Space: O(1)
Feeling lost in the world of random DSA topics, wasting time without progress? It’s time for a change! Join our DSA course, where we’ll guide you on an exciting journey to master DSA efficiently and on schedule.
Ready to dive in? Explore our Free Demo Content and join our DSA course, trusted by over 100,000 neveropen!