C++ 程序用于将两个数字相乘
两个数 a 和 b 的乘积就是它们的乘数。a 的值会以与 b 的值相同的方式添加以获得 a 和 b 的乘积。
例如。
5 * 4 = 20 7 * 8 = 56 9 * 9 = 81
使用 * 运算符将两个数字相乘的程序
使用 * 运算符将两个数字相乘的程序如下所示 −
示例
#include <iostream>
using namespace std;
int main() {
int a = 6, b = 8;
cout<<"Product of "<<a<<" and "<<b<<" is "<<a*b<<endl;
return 0;
}输出
Product of 6 and 8 is 48
在上面的程序中,a 和 b 的乘积只是使用 * 运算符显示的。以下代码段对此进行了演示。
cout<<"Product of "<<a<<" and "<<b<<" is "<<a*b<<endl;
不使用 * 运算符将两个数字相乘的程序
不使用 * 运算符将两个数字相乘的程序如下所示 −
示例
#include<iostream>
using namespace std;
int main() {
int a=7, b=8, product=0;
for(int i=1; i<=b; i++)
product = product + a;
cout<<"The product of "<<a<<" and "<<b<<" is "<<product<<endl;
return 0;
}输出
The product of 7 and 8 is 56
在上面的程序中,一个 for 循环用于向总计中添加 a 的值,总计是 b 次。这得到了 a 和 b 的乘积。
以下代码段对此进行了演示。
for(int i=1; i<=b; i++) product = product + a;
在此之后,将显示 a 和 b 的乘积。如下所示 −
cout<<"The product of "<<a<<" and "<<b<<" is "<<product<<endl;
广告
数据结构
网络
RDBMS
操作系统
Java
iOS
HTML
CSS
Android
Python
C 编程
C++
C#
MongoDB
MySQL
Javascript
PHP