In: Computer Science
Create a c++ program to compute the product of two integers. call create the following functions:
1. getNum - to accept only positive numbers && will call computeProd.
2.computeProd - to compute the product of the numbers & will call the function displayProduct.
3. displayProduct - to display the product.
Call the function getNum in the main function.
Compute the product w/o using the multiplication operator(*).
using #include <iostream> only
Dear Student ,
As per the requirement submitted above , kindly find the below solution.
Here a new cpp program with name "main.cpp" is created, which contains following code.
main.cpp :
//header files
#include <iostream>
using namespace std;
//method to display product
void displayProduct(int product)
{
cout<<"Product of two numbers :
"<<product<<endl;
}
//method to compute product of two number
void computeProd(int num1,int num2)
{
//compute product of two numbers
int product=num1*num2;
//call method and pass product of two number
displayProduct(product);
}
//method to accept positive numbers
void getNum()
{
//declaring variables
int num1,num2;
//asking user to enter num1
cout<<"Enter num1 : ";
cin>>num1;//reading num1
//asking user to enter num2
cout<<"Enter num2 : ";
cin>>num2;//reading num2
//checking number
if(num1>0 && num2>0)
{//if both numbers are greater than 0 then
//calling method and passing both numbers
computeProd(num1,num2);
}
else
{
//when any of the number is not positive then
cout<<"Enter both numbers greater than 0"<<endl;
}
}
//main method
int main()
{
//calling method to get two numbers from user
getNum();
return 0;
}
======================================================
Output : Compile and Run main.cpp to get the screen as shown below
Screen 1 :Screen when any of the number is less than 0
Screen 2:Screen showing product of two numbers
NOTE : PLEASE FEEL FREE TO PROVIDE FEEDBACK ABOUT THE SOLUTION.