C++ 程序查找数字中各位数字的乘积

编写一个 C++ 程序,用一个例子来查找数字中各位数字的乘积。在此 C++ 数字各位数字乘积的示例中,while 循环检查数字是否大于 0。

  • reminder = number % 10 – 它给出数字的最后一位。
  • digitProduct = digitProduct * reminder – 它将最后一位数字乘以 digitProduct。
  • number = number / 10 – 它从数字中删除最后一位数字。
#include<iostream>

using namespace std;

int main()
{
	int number, reminder, digitProduct = 1;
	
	cout << "\nPlease Enter the Number to find the Digits Product =  ";
	cin >> number;
	
	while (number > 0)
	{
    	reminder = number % 10;
    	digitProduct = digitProduct * reminder;
    	number = number / 10;
    	
    	cout << "\nDigit = " << reminder << " and the Digit Product = " << digitProduct;
	}
	cout << "\n\nThe Product of all Digits in a given Number = " << digitProduct;
		
 	return 0;
}
C++ Program to find Product of Digits in a Number

C++ 程序使用 For 循环查找数字中各位数字的乘积

#include<iostream>

using namespace std;

int main()
{
	int number, reminder, digitProduct;
	
	cout << "\nPlease Enter the Number to find the Digits Product =  ";
	cin >> number;
	
	for (digitProduct = 1; number > 0; number = number / 10)
	{
    	reminder = number % 10;
    	digitProduct = digitProduct * reminder;
	}
	cout << "\nThe Product of all Digits in a given Number = " << digitProduct;
		
 	return 0;
}
Please Enter the Number to find the Digits Product =  47865

The Product of all Digits in a given Number = 6720

C++ 程序使用函数计算数字中各位数字的乘积

#include<iostream>

using namespace std;

int productOfDigits(int number)
{
	int reminder, digitProduct;
	
	for (digitProduct = 1; number > 0; number = number / 10)
	{
    	reminder = number % 10;
    	digitProduct = digitProduct * reminder;
	cout << "\nDigit = " << reminder << " and the Digit Product = " << digitProduct;
	}
	return digitProduct;
}

int main()
{
	int number, digitProduct;
	
	cout << "\nPlease Enter the Number to find the Digits Product =  ";
	cin >> number;
	
	digitProduct = productOfDigits(number);
	
	cout << "\n\nThe Product of all Digits in a given Number = " << digitProduct;
		
 	return 0;
}
Please Enter the Number to find the Digits Product =  789356

Digit = 6 and the Digit Product = 6
Digit = 5 and the Digit Product = 30
Digit = 3 and the Digit Product = 90
Digit = 9 and the Digit Product = 810
Digit = 8 and the Digit Product = 6480
Digit = 7 and the Digit Product = 45360

The Product of all Digits in a given Number = 45360

C++ 程序使用递归计算数字中各位数字的乘积

#include<iostream>

using namespace std;

int productOfDigits(int number)
{	
	static int reminder, digitProduct = 1;
 
  	if(number > 0)
  	{
    	reminder = number % 10;
    	digitProduct = digitProduct * reminder;
    	productOfDigits( number / 10);
    	return digitProduct;
 	}
 	else
   		return 0;
}

int main()
{
	int number, digitProduct;
	
	cout << "\nPlease Enter the Number to find the Digits Product =  ";
	cin >> number;
	
	digitProduct = productOfDigits(number);
	
	cout << "\nThe Product of all Digits in a given Number = " << digitProduct;
		
 	return 0;
}
Please Enter the Number to find the Digits Product =  3456

The Product of all Digits in a given Number = 360