การคูณจำนวนสองตัว 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
ในโปรแกรมข้างต้น ใช้ a for loop เพื่อเพิ่มมูลค่าทั้งหมด 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;