Skip to content
Menu
Code for Java c
  • Home
  • Java
    • Java Examples
    • Java tutorials
  • C
    • C tutorials
    • C Examples
  • C++
    • C++ Tutorials
    • C++ Examples
  • Python
    • Python Tutorials
    • Python Examples
  • About
    • About me
    • contact us
    • disclaimer
    • Privacy Policy
Code for Java c
Python program to Multiply two integers|in 5ways

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

Posted on September 3, 2020September 6, 2020

Table of Contents

  • C++ program to Multiply two integers|in 6ways
    • Code to Multiply two integers
      • Code to Multiply two integers – standard method
      • Code to Multiply two integers – Using user input
      • Code to Multiply two integers – Using function
      • Code to Multiply two integers – Using recursion
      • Code to Multiply two integers – Using pointer
      • Code to Multiply two integers – with out multiplication operator

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

Related

Recent Posts

  • Subtract two numbers using method overriding
  • PHP Star triangle Pattern program
  • Using function or method to Write temperature conversion : Fahrenheit into Celsius
  • Function or method:temperature conversion from Fahrenheit into Celsius – Entered by user
  • Write temperature conversion program: Fahrenheit into Celsius
  • How to write a program to convert Fahrenheit into Celsius

tag

Addition (6) Array (38) C++ language (91) C language (98) c sharp (23) Division (6) Function (29) if else (32) Java language (102) JavaScript (5) loops (137) Multiply (7) Oop (2) patterns (65) PHP (13) Python Language (38) Subtraction (7) temperature (20)

Archives

Categories

Address

Global information technology

Puloly south, PointPedro

Jaffna

Srilanka

©2025 Code for Java c | Powered by SuperbThemes