📜  计算数字的数字乘积的程序

📅  最后修改于: 2022-05-13 01:57:58.845000             🧑  作者: Mango

计算数字的数字乘积的程序

给定一个数字,任务是找到一个数字的数字的乘积。

例子:

Input: n = 4513
Output: 60

Input: n = 5249
Output: 360

给定数字中数字乘积的通用算法:

  1. 获取号码
  2. 声明一个变量来存储产品并将其设置为 1
  3. 重复接下来的两个步骤,直到数字不为 0
  4. 在余数 '%'运算符的帮助下,通过将其除以 10 并将其乘以乘积,得到最右边的数字。
  5. 在“/”运算符的帮助下将数字除以 10
  6. 打印或退回产品。

以下是获得数字乘积的解决方案:

C++
// C++ program to compute
// product of digits in the number.
#include
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


Javascript


C++
#include 
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


C++
//Recursive function to get product of the digits
 
#include 
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<


输出:
60

方法#2:使用字符串()方法:

  • 将整数转换为字符串
  • 遍历字符串并通过将字符转换为整数来相乘

什么时候可以使用这个方法?:当一个数字的位数超过10^{19}         ,我们不能将该数字作为整数,因为 long long int 的范围不满足给定的数字。所以将输入作为一个字符串,从开始到字符串的长度运行一个循环,并用那个字符增加总和(在这种情况下它是数字)

下面是实现:

C++

#include 
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


输出:

60

方法#3:递归

  1. 获取号码
  2. 获取余数并传递下一个剩余数字
  3. 在余数 '%'运算符的帮助下,将数字除以 10 并乘以乘积,得到最右边的数字。
  4. 在“/”运算符的帮助下将数字除以 10 以删除最右边的数字
  5. 检查 n = 0 的基本情况
  6. 打印或退回产品

C++

//Recursive function to get product of the digits
 
#include 
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<