ADVERTISEMENT
Generate Table of Given Number
This program takes user input for a number and the range for the multiplication table, validates the input, and then prints the multiplication table within the specified range if the input is valid. If the input is not valid, it provides an error message.
#include<stdio.h>
main()
{
int i,Tbl_Num,Tbl_Start, Tbl_End;
printf("Enter a number for which you want table: ");
scanf("%d",&Tbl_Num);
printf("Enter Table Start No: ");
scanf("%d",&Tbl_Start);
printf("Enter Table End No: ");
scanf("%d",&Tbl_End);
if(Tbl_End<Tbl_Start)
{
printf("Table Ending No Must be Greater than Start.\n");
}
else
{
printf("Multiplication table of %d is\n",Tbl_Num);
for(i=Tbl_Start;i<=Tbl_End;i++)
{
printf("%d x %d = %d\n",Tbl_Num,i,Tbl_Num*i);
}
}
}C Programming ExamplesEnter a number for which you want table: 3
Enter Table Start No: 7
Enter Table End No: 15
Multiplication table of 3 is
3 x 7 = 21
3 x 8 = 24
3 x 9 = 27
3 x 10 = 30
3 x 11 = 33
3 x 12 = 36
3 x 13 = 39
3 x 14 = 42
3 x 15 = 45
C Programming ExamplesNow, let’s break down each part of the code:
- Header Inclusion:
#include <stdio.h>
This line includes the standard input-output library, which provides functions like printf and scanf.
- Main Function:
main()
{
// Code goes here
}
This is the main function where the execution of the program begins.
- Variable Declarations:
int i, Tbl_Num, Tbl_Start, Tbl_End;
Declares four integer variables: i for the loop counter, Tbl_Num for the number for which the table is to be generated, and Tbl_Start and Tbl_End for the starting and ending numbers of the table range.
- User Input:
printf("Enter a number for which you want the table: ");
scanf("%d", &Tbl_Num);
printf("Enter the Table Start No: ");
scanf("%d", &Tbl_Start);
printf("Enter the Table End No: ");
scanf("%d", &Tbl_End);
Prompts the user to enter values for Tbl_Num, Tbl_Start, and Tbl_End using printf and reads the input using scanf.
- Input Validation:
if (Tbl_End < Tbl_Start)
{
printf("Table Ending Number Must be Greater than Start.\n");
}
Checks if the ending number of the table is less than the starting number. If true, it prints an error message.
- Table Generation:
else
{
printf("Multiplication table of %d is\n", Tbl_Num);
for (i = Tbl_Start; i <= Tbl_End; i++)
{
printf("%d x %d = %d\n", Tbl_Num, i, Tbl_Num * i);
}
}
If the input is valid, it prints the multiplication table of Tbl_Num within the specified range (Tbl_Start to Tbl_End) using a for loop.