C++

C++ program to Multiply two integers|in 6ways

C++ program to Multiply two integers|in 6ways

In this tutorial, we will discuss the C++ program to multiply of two numbers

In this post, we are going to learn how to find multiplication of two numbers via different 6 ways in C++ language

Code to Multiply two integers

Code to Multiply two integers – standard method

Program 1

#include <iostream>
#include <conio.h>
using namespace std;

int main()
{
    int num1=50,num2=20, product;
    product=num1*num2;
    cout << "Product of "<<num1<<" and "<<num2<<" is :"<< product<<endl;
    getch();
    return 0;
}

When the above code is executed, it produces the following result

Product of 50 and 20 is: 1000

 

In this program,

  • Integer variable num1 and num2 both are declared and initialized
  • The num1 and num2 both are multiplied together and the value assigned to the integer variable product
  • Finally the program is displayed the output using cout<<

 

Code to Multiply two integers – Using user input

Program 2

#include <iostream>
#include <conio.h>
using namespace std;

int main()
{
    int num1,num2;
    cout<<"Enter two numbers: ";
    //The program ask to enter two numbers
    cin>>num1>>num2;
    //store two integer numbers in variable num1 and num2 respectively

    //performs multiplication and stores the result in variable product
    int product=num1*num2;
    cout << "Product of "<<num1<<" and "<<num2<<" is :"<< product<<endl;
    //display the product of two numbers
    getch();
    return 0;
}

When the above code is executed, it produces the following result

Enter two numbers 24
12
product of 24 and 12 is: 288

 

in this program,

  • Integer variable num1 and num2 both are declared,
  • The program takes input from the user
  • Then the user enters the input value for num1 and num2
  • The program will read the input using cin>> and store the variables in num1, num2.
  • The num1 and num2 both are multiplied together and the value assigned to the integer variable product
  • Finally the program is displayed the output using cout<<

 

Code to Multiply two integers – Using function

Program 3

#include <iostream>
#include <conio.h>
using namespace std;
int multiply(int x, int y);//function prototype
int main()
{
    int num1,num2;
    cout<<"Enter two numbers: ";
    //The program ask to enter two numbers
    cin>>num1>>num2;
    //store two integer numbers in variable num1 and num2 respectively

    //performs multiplication and stores the result in variable product
    int product=multiply(num1,num2);
    cout << "Product of "<<num1<<" and "<<num2<<" is :"<< product<<endl;
    //display the product of two numbers
    getch();
    return 0;
}
int multiply(int x, int y)
{
    return(x*y);//return the result to main
}

When the above code is executed, it produces the following result

Enter two numbers 40
30
product of 40 and 30 is: 1200

 

Approach

  • Declare a function named as multiply() with two integer parameters
  • Declare num1,num2 and product.
  • The program takes input from the user
  • Then the user enters the input value for num1, num2
  • the program will read the input using cin>> and store the variables in num1, num2 respectively
  • define the function (multiply())for find product
  • Call the function to produce output
  • finally, the program displays the result of multiplication using cout<<

 

Code to Multiply two integers – Using recursion

Program 4

#include <iostream>
#include <conio.h>
using namespace std;
int multiply(int,int);//function prototype
int main()
{
    int num1,num2;
    cout<<"Enter two numbers: ";
    //The program ask to enter two numbers
    cin>>num1>>num2;
    //store two integer numbers in variable num1 and num2 respectively

    //performs multiplication and stores the result in variable product
    int product=multiply(num1,num2);
    cout << "Product of "<<num1<<" and "<<num2<<" is :"<< product<<endl;
    //display the product of two numbers
    getch();
    return 0;
}
int multiply(int x, int y)
{
    if(x<y){
        return multiply(y,x);
    }
    else if(y!=0){
        return(x+multiply(x,y-1));//return the result to main
}
else {
     return 0;
}
}

When the above code is executed, it produces the following result

Enter two numbers 23
34
product of 40 and 30 is: 782

 

  • Declare a function named as multiply() with two int parameters
  • Declare num1,num2 and product.
  • The program takes input from the user
  • Then the user enters the input value for num1, num2
  • The program will read the input using cin>> and store the variables in num1, num2 respectively
  • Define the function (mutiply())for find multiplication recursively
  • Call the function to produce output
  • Finally, the program displays the result of multiplication using cout<<

 

Code to Multiply two integers – Using pointer

Program 5

#include <iostream>
#include <conio.h>
using namespace std;

int main()
{
    int num1,num2,mul;
    int *ptr1,*ptr2;
    cout<<"Enter two numbers: ";
    //The program ask to enter two numbers
    cin>>num1>>num2;
    //store two integer numbers in variable num1 and num2 respectively

    //performs multiplication and stores the result in variable product
  ptr1=&num1;
  ptr2=&num2;

  mul=*ptr1 * *ptr2;
    cout << "Product of "<<num1<<" and "<<num2<<" is :"<< mul<<endl;
    //display the product of two numbers
    getch();
    return 0;
}

When the above code is executed, it produces the following result

Enter two numbers 54
43
product of 40 and 30 is: 2322

 

  • Declare variables num1,num2 and mul.
  • Declare pointer variables *ptr1, *ptr2.
  • The program takes input from the user
  • Then the user enters the input value for num1, num2
  • The program will read the input using cin>> and store the variables in num1, num2 respectively
  • Address of the variables num1 and num2 assigned to pointer variables ptr1 and ptr2 respectively
  • calculate multiplication of the numbers using pointer variable  and Assign the product value to mul variable
  • Finally, the program displays the result using cout<<

 

Code to Multiply two integers – with out multiplication operator

Program 6

#include <iostream>
#include <conio.h>
using namespace std;

int main()
{
    int num1,num2,product=0;
    cout << "Enter two numbers for multiply" << endl;
    cin>>num1>>num2;
    for(int i=1; i<=num2; i++){
        product=product+num1;
    }
    cout<<"The product of "<<num1<<" and "<<num2<<" is :"<<product<<endl;
    getch();
    return 0;
}

When the above code is executed, it produces the following result

Enter two numbers for multiply
25
35
The product of 25 and 35 is: 875

 

Suggested for you

Data type in C++ language

Variable in C++ language

Operator in C++ language

While loop in C++ language

 

Similar post

Find product of two numbers in Java language|5ways

Find product of two numbers in C language|6ways

Find product of two numbers in Python language|4ways

Karmehavannan

Recent Posts

Multiply two numbers in Java using scanner| 5 different ways

Multiply two numbers in Java using scanner| 5 different ways In this article, we will…

3 months ago

5 different ways to Divide two numbers in Java using scanner

5 Different ways to Divide two numbers in Java using scanner In this article, we…

3 months ago

Learn 8 Ways to Subtract Two Numbers Using Methods in Java

Learn 8 Ways to Subtract Two Numbers Using Methods in Java In this article, we…

4 months ago

10 ways to subtract two numbers in Java

10 ways to subtract two numbers in Java In this article, we will discuss the…

4 months ago

Java Code Examples – Multiply Two Numbers in 5 Easy Ways

Java Code Examples – Multiply Two Numbers in 5 Easy Ways In this article, we…

4 months ago

How to Divide two numbers in Java| 5 different ways

How to Divide two numbers in Java| 5 different ways In this article, we will…

4 months ago

This website uses cookies.