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

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…

9 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…

10 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…

10 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…

10 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…

10 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…

10 months ago

This website uses cookies.