To print tables up to given limit using Goto statement
This is a C program that prompts the user to enter two integers, 'n' and 'a', and then uses the goto statement to print the multiplication table of 'a' from 1 to 'n'.
- The #include<stdio.h> is a preprocessor directive that includes the contents of the standard input-output library in the program. The int main() function is the starting point of the program execution. Inside the main function, three integers i, n and a are declared, where i is initialized to 1. printf() is used to display the prompts for the user to enter the limit and table number, and scanf() is used to get the input and store it in the variables 'n' and 'a' respectively.
- A label start is defined and a goto statement is used to jump to this label. The code between the label and the goto statement will execute repeatedly until the condition i<=n is false. The first time the code is executed, the multiplication table of 'a' from 1 to i is printed using printf("\n%d*%d=%d",i,a,i*a) . Then the value of i is incremented by 1. Then the condition i<=n is checked, if it's true the control jumps back to the label 'start' and the same process is repeated until the condition is false.
- It is worth noting that the use of the goto statement is generally discouraged in modern programming because it can lead to unstructured and hard-to-maintain code. Alternative control structures such as for loops, while loops or do-while loops can be used for the same purpose and are considered more readable, maintainable and less prone to errors.
Finally, the return 0 statement is used to indicate the successful execution of the program. The return value of 0 is a convention used to indicate that the program has executed correctly.
Source Code
#include<stdio.h>
int main()
{
int i=1,n,m,a;
printf("Enter the limit :");
scanf("%d",&n);
printf("Enter the table's number :");
scanf("%d",&a);
start:
printf("\n%d*%d=%d",i,a,i*a);
i++;
if(i<=n)
{
goto start;
}
return 0;
}
To download raw file
Click Here
Output
Enter the limit : 8
Enter the table's number : 5
1*5=5
2*5=10
3*5=15
4*5=20
5*5=25
6*5=30
7*5=35
8*5=40