×

Multiplication table program in C using For loop

Before we move on the program of multiplication table of any natural number, first we have to know about the For loop statement.

The syntax of ‘for’ loop in C programming language is given below:

for (initialization statement; test expression; update statement)
{
    /* main body of the ‘for’ loop */
}

How does the ‘for’ loop work?

In for loop, the initialization command is implemented only once.

After that, it checks the test expression of the loop and finds whether the test expression is false or true. And, for loop is dismissed if the test expression is false.   
If the test expression of the 'F' loop becomes true, then the program line provided in the F loop’s body is executed, and the update expression is changed accordingly.
Again, the entire process of evaluation is to be done. The loop executes its body until and unless the value of the test expression in for loop becomes false. The loop automatically terminates as soon as the test expression result becomes false.

The following program shows the multiplication table of any number entered by the user with the help of For loop in ‘C’ programming language:

#include <stdio.h>
int main ()
{
   int a, b; // declaring various variables without initializing their values
   printf ("Enter the natural number whose table you want to see here: \n");
// taking the value for output by the user as an input
   scanf ("%d", &b); /* assigning the value inputted by the user to the variable ‘b’ */
   for (a=1; a<=10; a++) // for (initialization statement; test expression; increment or decrement statement (for further evaluation))
{
      printf ("%d * %d = %d\n", b, a, (b*a)); // printing the result as an output value
   }
   return 0;
}

Output:

Enter the natural number whose table you want to see here:
10
10 * 1 = 10
10 * 2 = 20
10 * 3 = 30
10 * 4 = 40
10 * 5 = 50
10 * 6 = 60
10 * 7 = 70
10 * 8 = 80
10 * 9 = 90
10 * 10 = 100

Explanation of entire above program:

First step is to declare the variables a and b.
One more thing to notice in above-mentioned example is that both the variables are not initialized at the time of declaration. So, we let the user to input any natural number whose multiplication table is desired to be printed. The value entered by the user as an input is assigned to the variable ‘b’.

After that the ‘for’ loop is initialized. As we all know that FOR loop generally have three basic elements initialization statement, test expression or termination condition, and increment or decrement statement that changes the value after the execution of each iteration of ‘for’ loop.

Here, we initialized the value of variable ‘a’ equal to 1 and the termination condition is set that the value of variable ‘a’ must be less than or equal to 10. The modifying statement defined as a++ (an increment statement) that means the value of variable ‘a’ will be incremented after each and every execution of the ‘for’ loop.

After this, there is a print command to print the result as an output on the user’s screen.

The value of variable ‘b’ is 10 and it will not change throughout the entire evaluation of the ‘for’ loop as no increment or decrement statement is defined for variable ‘b’.

In the body of loop, we provided an operation b * a in the printf statement which multiplies the value of both variable and print the final value at last.

In the above program we have taken the multiplication table of 10.

So, value of b = 10 and value of a = 1.

And, the printf statement has printed the value 10

Then the increment statement incremented the value of variable ‘a’ from 1 to 2.

Hence, this time the printf statement will print the 10 * 2 = 20 on the screen.

And this process will execute for around 10 times and you will get the desired output same as the output of above program.

And as the value of ‘a’ will become 11 it will terminate the loop because the termination condition was set as a<=10 therefore it will find this condition to be false and dismiss the ‘for’ loop.


Related Topics

Bank management system in C

This is a mini project that is constructed completely using the C language. The bank management system is a beginner friendly project. To construct this user only needs to know...

12 minutes read.

How to measure time taken by a function in C?

Measuring the time taken by a function is quite a complex task, because numerous methods are frequently not transferable to other platforms, measuring the execution time of a C programs...

5 minutes read.

Type Casting in C

Type casting is a way to convert a variable from one data type to another data type.Syntax: (type) expression;     Example: #include <stdio.h> int main() { int a,b; float sum; printf("Example of Type Casting\n"); printf("Enter any integer nos:"); scanf("%d",&a,&b); sum=a+b; printf("Sum: %f",sum); return 0; } Output Example...

1 minute read.

Math functions in C

Math functions in C: In the C programming language, the compiler allows the developer to perform mathematical operations through the header’s functions, represented by <math.h>. The <math.h> header defines various mathematical...

3 minutes read.

For loop in C Programming

In the C programming language, the for loop is used to repeatedly iterate over a set of instructions or a section of code. It is frequently used to traverse array-based...

3 minutes read.

Built-in functions in C

The function is a set of instructions and statements enclosed in the "{}" delimiter. In c, there are two types of functions. Pre-define functions/ Built-in functionsUser define function. Built-in functions in C:- These...

8 minutes read.

How to Find Square Free Numbers in C?

Introduction Square-free number program is a typical interview and academic question in C Programming. In this section, we will define square-free integers, construct algorithms and C program to print the nth...

11 minutes read.

Else If Ladder in C

What is Else If Ladder: When there are multiple options and a user has to decide from the options, we use Else If Ladder. Basically, it is an extension of if...

3 minutes read.

Stdio.h in C

Header files are used to make the programmer’s efforts a lot easier. In order to make the programming simple, there are a number of libraries which are included as predefined...

4 minutes read.

C program that does not Suspend when Ctrl+Z is Pressed

In Programming when a program breakdown and runs surprisingly in the terminal compiler the developer has the ability to prevent the program from running unequivocally. For expressly halting the program,...

3 minutes read.

getc() function in C

Getc is one of the file handling technique in C. The Getc() is a C library function gets the next character or new characters  from the specific stream and supports...

4 minutes read.

Associativity of Operators in C

What is an Associativity operator? Two operators with equal priority are connected by employing their association when they are present in an expression. There are two different types of associativity: left to rightright...

4 minutes read.

Pointer to pointer in C

A pointer to another pointer is another type of multiple indirections and a chain of many pointers. Generally, a pointer consists of the address of the variable. Once a pointer to...

4 minutes read.

Results of Comparison Operations in C and C++

In this tutorial, we will explore comparison operators and how the system should compare an incoming value supplied as a context parameter to a given value or range of values. A...

2 minutes read.

One Dimensional Array in C

One Dimensional Array in C: In the C programming language, an array is defined as a collection of one or more values of the same type. Every value present in...

4 minutes read.

Derived Data Types in C

A variable in a program occupies some space in the computer's memory where some value is stored. Each variable in C has an associated data type. A value to be...

4 minutes read.

Write() function in c

As the name suggests the write () function is used to write the file descriptor. In other words it is used to write any file name without specifying file name,...

3 minutes read.

Static function in C

The functions in the C programming language are by default global. This means the programmer can easily access the function which is outside from the file where it was initially...

3 minutes read.

Types of Array in C

What is an Array? One value can be stored in a variable at once. How many variables will you need if you have 100 values? Well, the answer isn't 100. It...

14 minutes read.

C Programming Tutorial

What is C Programming C is most popular and widely used computer programming language. It was developed by Dennis Ritchie in 1972 at the Bell Lab. It is used to develop system application software. There...

8 minutes read.