In this article, we have discussed various methods to multiply two numbers in C++. These are simple program, you can learn them easily.
Method-1 : With Hardcoded Inputs
#include <iostream> using namespace std; // driver int main() { // first number is 5 int x = 5; // second number is 6 int y = 6; // resultant number int multiply = x * y; cout << multiply; return 0; }
Output:
30
Method-2 : With user Inputs
#include <iostream> using namespace std; // driver int main() { // for inputs int x, y; // for output int multiply; cout << "first number: "; cin >> x; cout << "second number: "; cin >> y; // multiplication of both numbers multiply = x * y; cout << " multiply is: " << multiply; return 0; }
Output:
Output depends on user inputs, that will be multiplication of both numbers.
Method-3 : With class
#include <iostream> using namespace std; class multiplication { // for inputs int x, y; public: // input numbers void input() { cout << "Input two integers\n"; cin >> x >> y; } void multiply() { // multiplication of both numbers cout << "Result: " << x * y; } }; // driver int main() { // object of class multiply m; m.input(); m.multiply(); return 0; }
Output:
Output depends on user inputs, that will be multiplication of both numbers.
Method-4 : With array elements
#include <iostream> using namespace std; //driver int main() { // create array int a[3]; // initialize array a[0]=15; a[1]=10; // multiplication of first two elements a[2]=a[0] * a[1]; cout<<" multiplication of "<<a[0]<<" and "<<a[1]<<" is "<<a[2]; return 0; }
Output:
multiplication of 15 and 10 is 150
Please write comments if you find anything incorrect. A gentle request to share this topic on your social media profile.