In today's post, we will talk about how we can print the table, we will use loops to print the table, and will also show it by explaining how the table is printed. Read this post today, the best 3 ways to print table in c language, only then you will understand how the program works.
To print the table you need know about loops, because we will use loops to print the table. Although printing the table is not a difficult task, but still many people have trouble in understanding it.
best 3 ways to print table in c language
As you know we will use loops to print the table. And you will also know that there are mainly two types of loops. 1.) Entry control loop 2. ) Exit control loopEntry control loop
These are of two types.for loop: - Whenever you learn any programming language, the most commonly used loop is for loop, and with the help of this we can create many different types of programs. The most important thing about this loop is that if the condition is false, then this loop does not execute even once. And this is also called entry control loop.
Syntax = for(value initilization ; condition ; increement/decreement)
{
statement 1 ;
statement 2 ;
statement 3 ;
statement n;
}
while loop :- This loop is also very popular, a lot of people use this loop as well, according to the problem , we use loops. We also call it the entry control loop.
Syntax= while(condition)
{
statement 1 ;
statement 2 ;
statement 3 ;
statement n ;
}
Exit control loop
do while loop :- Very few people use this loop, if the given condition is false, then the loop executes at least once. This is also called exit control loop. Syntax= do
{
statement 1 ;
statement 2 ;
statement 3 ;
}
while(condition);
print table using for loop
If you have seen the syntax of for loop, then you have to take care of three things, which we will discuss in turn.Value initilization: - This is the part from where we initilize the value to start the loop, we know that the table always starts with a multiple of one (1 * 1), and then its value increases.
for example :- (2*1),(2*2),(2*3) etc .
To print the table we have to initilize the value 1, and for that we have to declare a variable, for example we have to declare the variable (i).
i = 1;
Condition: - This is the part where we tell when to end the loop, the control of the loop comes out as soon as our given condition becomes false. And you know that the table is only up to a multiple of 10, so we give a specific condition to print the table.
i <=10;
As soon as the value of i is equal to 10, the loop control will come out.
Increement/Decreement: - Now to change the value of i, we have to use this part, now you think that to print the table, the value of i is increase or decrease. Obviously, to print the table, we have to increase the value of i, else you will understand yourself further.
i++ ;
statement: - Now our loop will start doing its work, but now we will tell you what to do with the loop by our statement.
For example, if we want to print a table of 8, then statement are executed that this is the program, which we will cover step by step now.
When you see the statement inside the loop, you must be wondering how the table will be printed. So let's see.
When you see the statement inside the loop, you must be wondering how the table will be printed. So let's see.
Explain: We have declared three variable names named i, T, n, where T is the output of our table. Now i = 1, so when you look at the statement and apply the value in it, then you will know what is happening.
T = 8 * 1 Here we have written both the value of n and the value of i, so you will know what T will print. answer is = 8
Now the condition of the loop will be checked, which is still true because we have condition (i <= 10), meaning the value of i is currently smaller than 10, now the value of i will increase by one (i ++).
Now with the increase in value of i T = 8 * 2 , T will now print 16 and due to the presence of \ n, 16 will print from the new line.
Now the condition will be checked again, and when it is incremented, now the value of i will be 3, and T will print 24 in the new line, and so the loop will continue, and the value of i will increase, as long as the condition follows. It does not happen. when the condition is false , then the loop come out from the curly bracess.
So in this way we can print the table with the help of for loop and the same process applies to each loop.
It works exactly like a for loop, just in this we have already initialized i = 1, in this way we can also print the table using a while loop.
If you look carefully, we have put a semicolon after the while loop, so that the program can be repeated. Here the value is printed first, followed by the value of i, and then after the condition is checked, if the condition is true then the loop will repeat again, and it will continue until the condition becomes false.
print table using while loop
It works exactly like a for loop, just in this we have already initialized i = 1, in this way we can also print the table using a while loop.
print table using do while loop
You will know that the do while loop condition is executed at least once even if it is false, then we will print the table something like this with the help of do while.If you look carefully, we have put a semicolon after the while loop, so that the program can be repeated. Here the value is printed first, followed by the value of i, and then after the condition is checked, if the condition is true then the loop will repeat again, and it will continue until the condition becomes false.
We hope you liked our post the best 3 way to print table in C language. If you have any question, you can comment. If you think this post can be useful to someone else, then do not forget to share it. You must subscribe to our blog, as soon as we upload a new post, the notification reaches you. Don't forget to read another post on my blog .
1 Comments
Nice read
ReplyDeleteHey , Comment Your Query or Suggestion