Printing Multiplication Table in Descending Order using Goto Condition in C
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 'n' to 1 in descending order.
- 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, two integers n and a are declared.
- 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 n>0 is false.
- The first time the code is executed, the multiplication table of 'a' from 'n' to 1 is printed using printf("\n%d*%d=%d",n,a,n*a) . Then the value of n is decremented by 1. Then the condition n>0 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.
Source Code
#include <stdio.h>
int main()
{
int i,n,m,a;
printf("\nEnter the limit :");
scanf("%d",&n);
printf("\nEnter the table's number:");
scanf("%d",&a);
start:
printf("\n%d*%d=%d",n,a,n*a);
n--;
if(n>0)
{
goto start;
}
return 0;
}
To download raw file
Click Here
Output
Enter the limit:5
Enter the table's number:7
5*7=35
4*7=28
3*7=21
2*7=14
1*7=7