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