Print tables using function in C++


Example Program : 1

This program generates multiplication tables for a given number up to a specified limit. The tables function takes no arguments and returns no value. It prompts the user to enter the table number and the limit, then uses a for loop to iterate from 1 up to the limit and prints out each multiplication table entry. The main function simply calls the tables function and returns 0.

Source Code

#include<iostream>
using namespace std;
void tables();
int main()
{
    tables();
    return 0;
}
void tables()
{
    int i,j,n;
    cout<<"\nEnter the Table number:";
    cin>>j;
    cout<<"\nEnter the limit:";
    cin>>n;
    for(i=1;i<=n;i++)
    {
        cout<<"\n"<<i<<"*"<<j<<"="<<i*j;
    }
}
To download raw file Click Here

Output

Enter the Table number:10
Enter the limit:10
1*10=10
2*10=20
3*10=30
4*10=40
5*10=50
6*10=60
7*10=70
8*10=80
9*10=90
10*10=100

Example Program : 2

The program is an example of using a function with parameters. The main function asks the user for a table number (j) and a limit (n) and then calls the tables function with these values as arguments.

The tables function takes two parameters, j and n, and uses them to print out the multiplication table of j up to the limit n. The function uses a loop to iterate through the numbers 1 to n and print out each multiplication. Finally, the main function returns 0 to indicate successful program execution.

Source Code

#include<iostream>
using namespace std;
void tables(int,int);
int main()
{
    int i,j,n;
    cout<<"\nEnter the Table number:";
    cin>>j;
    cout<<"\nEnter the limit:";
    cin>>n;
    tables(j,n);
    return 0;
}
void tables(int j,int n)
{
    int i;
    for(i=1;i<=n;i++)
    {
        cout<<"\n"<<i<<"*"<<j<<"="<<i*j;
    }
}
To download raw file Click Here

Output

Enter the Table number:10
Enter the limit:10
1*10=10
2*10=20
3*10=30
4*10=40
5*10=50
6*10=60
7*10=70
8*10=80
9*10=90
10*10=100

Example Program : 3

This program allows the user to input a number (which represents a multiplication table) and a limit, and then prints out the multiplication table up to that limit. The program contains a function tables() which prompts the user to input a table number and limit, and then uses a for loop to print out the multiplication table up to that limit. The main() function calls the tables() function to run the program.

Source Code

#include<iostream>
using namespace std;
void tables();
int main()
{
    tables();
    return 0;
}
void tables()
{
    int i,j,n;
    cout<<"\nEnter the Table number:";
    cin>>j;
    cout<<"\nEnter the limit:";
    cin>>n;
    for(i=1;i<=n;i++)
    {
        cout<<"\n"<<i<<"*"<<j<<"="<<i*j;
    }
}
To download raw file Click Here

Output

Enter the Table number:10
Enter the limit:10
1*10=10
2*10=20
3*10=30
4*10=40
5*10=50
6*10=60
7*10=70
8*10=80
9*10=90
10*10=100

Program List


Flow Control

IF Statement Examples


Switch Case


Goto Statement


Break and Continue


While Loop


Do While Loop


For Loop


Friend Function in C++


String Examples


Array Examples


Structure Examples


Structure & Pointer Examples


Structure & Functions Examples


Enumeration Examples


Template Examples


Functions


Inheritance Examples

Hierarchical Inheritance


Hybrid Inheritance


Multilevel Inheritance


Multiple Inheritance


Single Level Inheritance


Class and Objects

Constructor Example


Destructor Example


Operator Overloading Example


Operator and Function Example


List of Programs


Pointer Examples


Memory Management Examples


Pointers and Arrays


Virtual Function Examples