Program to calculate product of digits of a number
Last Updated :
13 Mar, 2023
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>
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
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));
}
}
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 = 4513
print(getProduct(n))
# This code is contributed
# by mohit kumar
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
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
?>
JavaScript
<script>
// JavaScript program to compute
// product of digits in the number.
// Function to get product of digits
function getProduct(n)
{
let product = 1;
while (n != 0)
{
product = product * (n % 10);
n = Math.floor(n / 10);
}
return product;
}
// Driver code
let n = 4513;
document.write(getProduct(n));
// This code is contributed by Manoj.
</script>
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 10^{19} , 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>
using namespace std;
int getProduct(string str)
{
int product = 1;
// Traversing through the string
for (int i = 0; i < str.length(); i++) {
// Since ascii value of
// numbers starts from 48
// so we subtract it from sum
product = product * (str[i] - 48);
}
return product;
}
// Driver Code
int main()
{
string st = "4513";
cout << getProduct(st);
return 0;
}
Java
import java.io.*;
class GFG {
static int getProduct(String str)
{
int product = 1;
// Traversing through the string
for (int i = 0; i < str.length(); i++)
{
// Since ascii value of
// numbers starts from 48
// so we subtract it from sum
product *= str.charAt(i) - '0';
}
return product;
}
// Driver Code
public static void main(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
def getProduct(n):
product = 1
# Converting integer to string
num = str(n)
# Traversing the string
for i in num:
product = product * int(i)
return product
# Driver Code
n = 4513
print(getProduct(n))
# This code is contributed by vikkycirus
C#
using System;
using System.Collections;
class GFG
{
static int getProduct(String str)
{
int product = 1;
// Traversing through the string
for (int i = 0; i < str.Length; i++)
{
// Since ascii value of
// numbers starts from 48
// so we subtract it from sum
product = product * (str[i] - 48);
}
return product;
}
// Driver Code
public static void Main(String[] args)
{
String st = "4513";
Console.Write(getProduct(st));
}
}
//This code is contributed by shivanisinghss2110
JavaScript
<script>
function getProduct(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]));
}
return product;
}
// Driver Code
let st = "4513";
document.write(getProduct(st));
// This code is contributed by unknown2108
</script>
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>
using namespace std;
int getProduct(int n){
// Base Case
if(n == 0){
return 1 ;
}
// get the last digit and multiply it with remaining digits
return (n%10) * getProduct(n/10) ;
}
int main() {
// call the function
cout<<getProduct(125) ;
return 0;
}
Java
// Recursive function to get product of the digits
import java.util.*;
class GFG
{
static int getProduct(int n)
{
// Base Case
if(n == 0){
return 1 ;
}
// get the last digit and multiply it with remaining digits
return (n%10) * getProduct(n/10) ;
}
public static void main(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
def getProduct(n):
# Base Case
if(n == 0):
return 1
# 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
using System;
using System.Collections.Generic;
class GFG
{
static int getProduct(int n)
{
// Base Case
if(n == 0){
return 1 ;
}
// get the last digit and multiply it with remaining digits
return (n%10) * getProduct(n/10) ;
}
public static void Main(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
function getProduct(n){
// Base Case
if(n == 0){
return 1 ;
}
// 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
Time Complexity: O(log10N)
Auxiliary Space: O(1)
Similar Reads
Cumulative product of digits of all numbers in the given range Given two integers L and R, the task is to find the cumulative product of digits (i.e. product of the product of digits) of all Natural numbers in the range L to R. Examples: Input: L = 2, R = 5 Output: 14 Explanation: 2 * 3 * 4 * 5 = 120Input: L = 11, R = 15 Output: 120 Explanation: (1*1) * (1*2) *
5 min read
Spy Number (Sum and Products of Digits are same) A number is said to be a Spy number if the sum of all the digits is equal to the product of all digits. Examples : Input : 1412Output : Spy NumberExplanation : sum = (1 + 4 + 1 + 2) = 8product = (1 * 4 * 1 * 2) = 8since, sum == product == 8 Input : 132Output : Spy NumberExplanation : sum = (1 + 3 +
4 min read
First digit in product of an array of numbers Given an array of 'n' numbers. We need to find the first digit of product of these 'n' numbers Examples : Input : arr[] = {5, 8, 3, 7} Output : 8 Product of 5, 8, 3, 7 is 840 and its first digit is 8 Input : arr[] = {6, 7, 9} Output : 3 Recommended PracticeFirst DigitTry It! Background : First we st
7 min read
Last digit of Product of two Large or Small numbers (a * b) Given two large or small numbers, the task is to find the last digit of the product of these two numbers.Examples: Input: a = 1234567891233789, b = 567891233156156 Output: 4 Input: a = 123, b = 456 Output: 8 Approach: In general, the last digit of multiplication of 2 numbers a and b is the last digi
3 min read
Sort the numbers according to their product of digits Given an array arr[] of N non-negative integers, the task is to sort these integers according to the product of their digits.Examples: Input: arr[] = {12, 10, 102, 31, 15} Output: 10 102 12 31 15 10 -> 1 * 0 = 0 102 -> 1 * 0 * 2 = 0 12 -> 1 * 2 = 2 31 -> 3 * 1 = 3 15 -> 1 * 5 = 5Input
6 min read