C++

C++ program to Generate multiplication table

C++ Program to Generate multiplication table

In this article, we will discuss the concept of C++ Program to generate multiplication table..

In this program, we are going to learn how to generate a multiplication table using 5 ways in C++ language.

This is done using for loop , while loop , do-while loop , method and recursion

Program to Generate multiplication table – using for loop

In this program, we will  display multiplication table of given number using for loop in C++ language

Program 1

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

int main()
{
    int num;
    cout << "Enter an integer!" << endl;
    cin>>num;
    for(int i=1; i<=10; i++){
        cout<<num<<" * "<<i<<" = "<<num*i << endl;
    }
    getch();
    return 0;
}

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

Enter an  integer

13
13 * 1 = 13
13 * 2 = 26
13 * 3 = 39
13 * 4 = 52
13 * 5 = 65
13 * 6 = 78
13 * 7 = 91
13 * 8 = 104
13 * 9 = 127
13 * 10 = 130

 

  • integer variable num and i are declared.
  • The program asks input from the user
  • Then the user enters the input values for num.
  • The program will read the input using cin>> and store to the variable num
  • Create a for loop of i from 1 to 10 and increase the value of i after every iteration by 1
  • finally, the program displays the multiplication table using cout<<.

 

Program to Generate multiplication table – using while loop

In this program, we will  display multiplication table of given number using while loop in C++ language

Program 2

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

int main()
{
    int num;
    cout << "Enter an integer!" << endl;
    cin>>num;
    int i=1;
    while(i<=10){
        cout<<num<<" * "<<i<<" = "<<num*i << endl;
        i++;
    }
    getch();
    return 0;
}

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

Enter an  integer

7
7 * 1 = 7
7 * 2 = 14
7 * 3 = 21
7 * 4 = 28
7 * 5 = 35
7 * 6 = 42
7 * 7 = 49
7 * 8 = 56
7 * 9 = 63
7 * 10 = 70

 

  • integer variable num and i are declared.
  • The program asks input from the user
  • Then the user enters the input values for num.
  • The program will read the input using cin>> and store to the variable num
  • Create a while loop of i from 1 to 10 and increase the value of i after every iteration by 1
  • finally, the program displays the multiplication table using cout<<.

 

Program to Generate multiplication table – using Do-while loop

In this program, we will  display multiplication table of given number using do-while loop in C++ language

Program 3

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

int main()
{
    int num;
    cout << "Enter an integer!" << endl;
    cin>>num;
    int i=1;
   do{
        cout<<num<<" * "<<i<<" = "<<num*i << endl;
        i++;
    } while(i<=10);
    getch();
    return 0;
}

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

Enter an  integer

11
11 * 1 = 11
11 * 2 = 22
11 * 3 = 33
11 * 4 = 44
11 * 5 = 55
11 * 6 = 66
11 * 7 = 77
11 * 8 = 88
11 * 9 = 99
11 * 10 =110

  • integer variable num and i are declared.
  • The program asks input from the user
  • Then the user enters the input values for num.
  • The program will read the input using cin>> and store to the variable num
  • Create a do-while loop of i from 1 to 10 and increase the value of i after every iteration by 1
  • finally, the program displays the multiplication table using cout<<.

 

 

Program to Generate multiplication table – using function

In this program, we will  display multiplication table of given number using function

Program 4

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

void mulTable(int);//function prototype
int main()
{
    int num;
    cout<<"Enter a positive number\n";
    cin>>num;
    cout<<"\nMutiplication table for "<<num<< "is\n";
    mulTable(num);//function call
    getch();
    return 0;
}
void mulTable(int num)//function definition
{
    int i;
    for(i=1; i<=12; i++ ){
         cout<<num<<" * "<<i<<" = "<<num*i<<"\n";
    }
}

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

Generate multiplication table
  • Declare a function named as mulTable(int); with parameter
  • integer variables num is declared.
  • The program asks input from the user
  • Then the user enters the input values for num.
  • The program will read the input using cin>> and store the variable num.
  • Define the function  mulTable(int); to print multiplication table
  • Call the function to produce output
  • finally, the program displays the multiplication table using cout<<.

 

Program to Generate multiplication table – using recursion

In this program, we will  display multiplication table of given number using recursive function

Program 5

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

int mulTable(int,int);//function prototype
int main()
{
    int num,i=1;
    cout<<"Enter a positive number\n";
    cin>>num;
    cout<<"\nMutiplication table for "<<num<< " is\n";
    mulTable(num,i);//function call
    getch();
    return 0;
}
int mulTable(int num,int i)//function definition
{
    if(i>10){
        return 0;
    }
    else
    {
        cout<<num<<" * "<<i<<" = "<<num*i << endl;
        return mulTable(num,i+1);
    }
}

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

 

Suggested post

For loop in C++ language

While loop in C++ language

Do-While loop in C++ language

Function in C++ language

Recursion in C++ language

 

Similar post

Program to Display multiplication table in Java

Program to Display multiplication table in C

Program to Display multiplication table in C++

Program to Display multiplication table in Python

 

 

Karmehavannan

Recent Posts

Subtract two numbers using method overriding

Subtract two numbers using method overriding   Program 1

3 months ago

PHP Star triangle Pattern program

PHP Star triangle Pattern program Here's a simple Java program that demonstrates how to print…

3 months ago

Using function or method to Write temperature conversion : Fahrenheit into Celsius

Using Function or Method to Write to temperature conversion: Fahrenheit into Celsius In this article,…

1 year ago

Function or method:temperature conversion from Fahrenheit into Celsius – Entered by user

Function or method of temperature conversion from Fahrenheit into Celsius In this article, we will…

1 year ago

Write temperature conversion program: Fahrenheit into Celsius

Write temperature conversion program: from Fahrenheit to Celsius In this article, we will discuss the…

1 year ago

How to write a program to convert Fahrenheit into Celsius

How to write a program to convert Fahrenheit into Celsius In this article, we will…

1 year ago

This website uses cookies.