Multiplication of two numbers in C++

Last Updated :

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.

Comment
Next Article
RKplus Published 27 May, 2020 · 2 min read

Similar Reads

  • Find the Nth Triacontakaihenagonal Number

    Given a number N, the task is to find Nth triacontakaihenagonal number. A triacontakaihenagonal number is class of figurate number. It has 31 – sided polygon called triacontakaihenagon.…

    1 min read
  • Check if a Number is a Tetracontagon Number

    Given an integer N, the task is to check if it is a Tetracontagon number or not. Tetracontagon number is class of figurate number. It has 40 –…

    2 min read
  • Find max rational number in given Array

    Given the Numerator and Denominator of N Rational Numbers, the task is to find the maximum Rational Number from the array. Examples: Input: N = 2 num[] =…

    2 min read
  • Check if a Number is Pentacontagon

    Given an integer N, the task is to check if it is a pentacontagon number or not. Pentacontagon number is class of figurate number. It has 50 –…

    2 min read
  • Check if a Number is Triacontakaidigon

    Given an integer N, the task is to check if it is a Triacontakaidigon number or not. Triacontakaidigon number is class of figurate number. It has 32 –…

    2 min read
  • Check if a Number is Hexacontagon

    Given an integer N, the task is to check if it is a hexacontagon number or not. Hexacontagon number is class of figurate number. It has 60 –…

    2 min read