Program to Generate Multiplication Table in C Language

generate-mutliplication-table-program-in-c-using-loops

Multiplication Table in C Program Description:

We will write a program to generate the multiplication table in C Programming Language. The program will ask for a number and generates the multiplication table for the given number up to 10. Then we will customize the program to take the range from the user and finally we look at a program where we generate a multiplication table for all numbers from 1 to 20. So let’s get started.

Example Input and Output of the Program:

Input:

Enter number to generate the table: 15

Output:

Program Pre-Requisites:

It will be good to know the C Loops and the Arithmetic Operators. So please go through the following articles to learn more about them

Multiplication Table in C Program Algorithm:

  1. The program starts by asking for the input number and we store it in  num variable.
  2. Then we use the a C loop (for, while, etc) to print the multiplication of the num.
  3. We use variable i to control the Loop. Initialize the counter i with 1
  4. Start the Loop. Our Loop condition is i <= 10, So the loop will continue until i value reaches 10. At Each Iteration
    • Print the value of num * i.
    • Then update the counter variable i by 1
  5. So the above loop iterates  10 times and prints one line (by multiplying the variable i and variable num) onto the console at each iteration. Thus generating the multiplication table up to 10.

We will explore several variations of the Multiplication table program. All of these methods are based on the above algorithm.

Let’s start with the most basic one. Printing Multiplication Table of given number up to 10.

Generate Multiplication Table in C using for loop ( Up to 10):

We are using the for loop to create the multiplication table in this program.

Program Output:

Save the above program using the .c extension. Compile and run the program using your favorite compiler. We are using the GCC compiler here.

$ gcc multiplicatioTable.c

Run the program

mutliplication-table-program-in-c-using-for-loop-output

What if the user enters a negative number, Here is an example of a negative number multiplication table.

📢 If you don’t want to accept the Negative numbers, Then you can add the condition to display an error message based on user input.

Multiplication Table in C up to given range (Times):

How many times do you want to print the table?

In the above example, We are printing the table up to the number 10 (times). Let’s customize it and ask for the user input for it as well ( Number of times or up to the given range – times)

As you can see from the above program, We also added a new variable times and asking for the user input and storing the given range value in the variable times

Program Output:

Let’s compile and run the program.

Here the program asked for a second number, which defines the range. and We entered the range as 5. So the program displayed the 8 table up to 5 times.

As the range is not going to be a negative number. So our program won’t accept the negative number for times the variable. Let’s check it.

As you can see from the above output, The program is displaying the error message if the user provides a negative number for times variables ( range variables)

Here is one more example.

Using Do while or Goto statement to ask for user input again in case of an Invalid range number:

In the above example program, we are simply terminating the program in case of a negative number( Invalid number). So instead of stopping the program. Let’s ask the user input again using the goto statement or do-while loop.

Let’s start with the goto statement.

Request input until the user provides a valid number using the goto statement:

In this program, We are going to use the goto statement to jump from one statement in the program to another statement.

Whenever a user enters a negative number, The goto START; ( at line 26) will be executed and This will make the program go back to the goto label which is START ( at line 18). So The program displays the How many times you want to print (upto range): message again to accept the new input.

Program Output:

Let’s compile and run the program.

As you can see from the above output, the Program is working as expected in normal cases, But when the user enters a Negative number like -4, It displayed the error message – How many times you want to print (up to range): -4. and asked for input again. This process continues until the user enters a valid number.

Request input until the user provides a valid number using the Do-While Loop:

In this program, We are going to use the do while loop instead of the goto statement to ask for the user input again.

As you can see from the above code, We wrapped the second printf function using the do while loop.

So the do while loop continues executes until the condition times<= 0, becomes False. Which will become False when the user enters a positive number.

Program Output:

Save the program. Then compile and run the program.

Normal Case:

multiplication-table-in-c-using-do-while

When the user enters a negative number:

As you can see from the above output When the user enters a negative value -6 for the range(times variable), The do-while loop condition times<= 0 returned True, So we asked for the input again, Then user entered the another negative number -12, Still, the condition is True. It will become False only if the user enters a positive number. So the do-while loop won’t terminate until the user enters a valid number.

Generate Multiplication Table up to Range using while loop in C:

So far we have used the for loop to generate the table, Let’s use the while loop to generate the multiplication table. The program algorithm is not going to change.

If you observe, When a user enters a negative number, we are using the goto statement method described above to ask the user again for the input.

Program Output:

We can see from the above output, We are getting the expected results.

Generate Multiplication Table of a Number up to Range using while loop in C:

Here is the program to generate the multiplication table using the do while loop in C programming language.

Program Output:

Compile and run the program using your favorite compiler, We are using the GCC compiler to Compile and Run the C Program under Linux operating system

Test Case 1: Positive numbers for num and times

When the user enters a negative number for range( times variable)

program-generate-math-tables-in-c-language

Math (Multiplication) Tables in C language using the Goto Statement:

We can also create the loop-like behavior using the goto statement. This means we can use the Goto statement to iterate over a block of code. So let’s use the goto statement to print the multiplication table in C language.

Here is the main block of code, which creates the loop using the goto statement.

Program Output:

Compile and run the program.

Program to Print All Multiplication Tables from 1 to 20 numbers in C:

Now let’s generate all multiplication tables from the number 1 to number 20 using the for loop.

We are going to use the two loops,

  • The Outer-Loop, Which will loop start from 1 to 20. We use cnt variable to control the outer loop.
  • The Inner Loop Creates the table. Which starts from 1 to up to the given range times. (the range must be a positive number).

Here is the program to print math tables from 1 to 20 numbers.

Program Output:

Let’s compile and run the program. This output is going to generate all tables from 1 to 20. So the output will be very large.

As you can see from the above output, The program printed all multiplication tables.

Generate Multiplication Tables with Custom number of Tables and Custom Range:

In the above program, We generated 20 tables. So let’s customize it.

Now the program will ask the user how many tables you want to print. (Number of tables from 1). and what is the range of each table.

For Example,

If the user enters the number( num) as 5, and range/times as 5, Then We generate the tables from the number 1 to number 5 and Each table will have a range up to 5.

Note: This program doesn’t allow negative numbers for num and range/ times variables.

Program Output:

  1. C Program to Convert Decimal Number to Binary Number
  2. C Program to Convert Binary Number to Decimal Number
  3. C Program to Convert Octal Number to Decimal Number
  4. C Program to Calculate the Factorial of Number
  5. C Program to Find all Factors of a Number
  6. C Program to Calculate the GCD or HCF of Two Number
  7. C Program to Calculate the LCM of two Numbers
  8. C Program to Check 3-Digit and N-Digit Armstrong Number
  9. Collection of All Loops Programs

Venkatesh

Hi Guys, I am Venkatesh. I am a programmer and an Open Source enthusiast. I write about programming and technology on this blog.

You may also like...

1 Response

  1. […] C Program to Generate Multiplication Tables […]

Leave a Reply