Priniting tables using Goto Statement in C++


This program prints out the multiplication table of a given number up to a certain limit using a goto statement. Here's how the program works:

  • It prompts the user to enter the limit and the number for the multiplication table.
  • It reads the values into the variables n and a.
  • It initializes the variable i to 1.
  • It uses a goto statement to create a loop that prints out the multiplication table for the given number up to the limit.
  • It prints out the result of the multiplication.
  • It increments i by 1.
  • It checks if i is less than or equal to n. If it is, the loop goes back to the start label and repeats. If it is not, the loop exits.
  • The program ends.

While this program works correctly, using a goto statement is generally not considered good programming practice because it can make the code difficult to read and maintain. A better alternative would be to use a for loop to print out the multiplication table

Source Code

/*
    i=1
    n=10
    table=2

    1*2=2
    2*2=4
    3*2=6

*/
#include<iostream>
using namespace std;
int main()
{
    int i=1,n,m,a;
    cout<<"Enter the limit :";
    cin>>n;
    cout<<"Enter the table's number :";
    cin>>a;
    start:
        cout<<"\n"<<i<<"*"<<a<<"="<<i*a;
        i++;
        if(i<=n)
        {
            goto start;
        }
    return 0;
}
To download raw file Click Here

Output

Enter the limit :10
Enter the table's number :9
1*9=9
2*9=18
3*9=27
4*9=36
5*9=45
6*9=54
7*9=63
8*9=72
9*9=81
10*9=90

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