Cpp program to multiply two numbers using function
- Home
- Calculations
- Cpp program to multiply two numbers using function
- On
- By
- 0 Comment
- Categories: Calculations, multiply
Cpp program to multiply two numbers using function
Cpp program to multiply two numbers using function
In this tutorial, we will discuss the Cpp program to multiply two numbers using the function
In this topic, we will learn a simple concept of how to multiply two integers using the function in the C++ programming language
already we will know the same concept using the operator in a simple way.
If you want to know, click here C++ program to multiply two numbers
Cpp program to multiply two integer numbers
This program can have important following steps to completion
- Function declaration or prototype
- essential variable declaration
- read the number from the user to store in the variable
- Function definition with the return value
- calling the function
- Display result on the screen
Program 1
#include <iostream>
#include <conio.h>
using namespace std;
int multiply(int x, int y);//function declaration or prototype
int main()
{
int num1; //variable for store first numbers
int num2; //variable for store second numbers
int product; //variable for store result of product
//read numbers
cout << "Enter the first numbers" << endl;
cin>>num1; //stored first number- get input from user
cout << "Enter the second numbers" << endl;
cin>>num2; //stored second number- get input from user
product=multiply(num1, num2);//calling function and stored returning the result by function;
//print product of numbers
cout<<"Product of two numbers "<<product<<endl;
getch();
return 0;
}
//function definition
int multiply(int x, int y)
{
return (x*y); //return the product result to main
}
When the above code is compiled and executed, it produces the following results
Enter the first numbers 45 Enter the second numbers 34 product of two numbers 1530
In this program, the user is asked to enter two numbers(integer numbers). then it calculating product of those numbers and display on the screen
Here, when the user enters the numbers, the first number stored in variable num1 and the second number stored the variable num2. Then, the product of the numbers is stored in the variable product.
Cpp program to multiply two floating point numbers
Program 1
#include <iostream>
#include <conio.h>
using namespace std;
float multiply(float x, float y);//function declaration or prototype
int main()
{
float num1; //variable for store first numbers
float num2; //variable for store second numbers
float product; //variable for store result of product
//read numbers enterd from user
cout << "Enter the first numbers" << endl;
cin>>num1; //stored first number- get input from user
cout << "Enter the second numbers" << endl;
cin>>num2; //stored second number- get input from user
product=multiply(num1, num2);//calling function and stored returning the result by function;
//print product of numbers
cout<<"Product of two numbers "<<product<<endl;
getch();
return 0;
}
//function definition
float multiply(float x, float y)
{
return (x*y); //return the multiplication result to main
}
When the above code is compiled and executed, it produces the following results
Enter the first number 12.34 Enter thes second number 21.43 the product of two numbers 264.446
In this program, the user is asked to enter two numbers(floating point numbers). then it calculating product of those numbers and display on the screen
Here, when the user enters the numbers, the first number stored in variable num1 and the second number stored the variable num2. Then, the product of the numbers is stored in the variable product.
Suggested for you
Data type in C++ language