3. Write a script to perform the product
of 3 numbers entered by the user. *
(Dointal
Answers
search
Sign In
Home
Courses
Algorithmskeyboard_arrow_down
Data Structureskeyboard_arrow_down
Languageskeyboard_arrow_down
Interview Cornerkeyboard_arrow_down
GATEkeyboard_arrow_down
CS Subjectskeyboard_arrow_down
Studentkeyboard_arrow_down
Jobskeyboard_arrow_down
GBlog
Puzzles
What's New ?
▲
Program to calculate product of digits of a number
Last Updated: 17-05-2019
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
Recommended: Please try your approach on {IDE} first, before moving on to the solution.
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
Java
Python3
C#
PHP
Output:60
Recommended Posts:
Check whether product of digits at even places is divisible by sum of digits at odd place of a number
Minimum digits to be removed to make either all digits or alternating digits same
Find smallest number with given number of digits and sum of digits
Find the Largest number with given number of digits and sum of digits
Number of digits in the nth number made of given four digits
Count of integers in a range which have even number of odd digits and odd number of even digits
Find smallest number with given number of digits and sum of digits under given constraints
Number formed by deleting digits such that sum of the digits becomes even and the number odd
Sum and Product of digits in a number that divide the number
Smallest number with given sum of digits and sum of square of digits
Minimum number of digits to be removed so that no two consecutive digits are same
Check if the sum of digits of number is divisible by all of its digits
Sum of the digits of square of the given number which has only 1's as its digits