Get Even More Visitors To Your Blog, Upgrade To A Business Listing >>

C program to print table of 2 using for loop


#include

int main() {

    int i;

The first line includes the standard input/output header file, which is needed for the printf Function used later in the program.

The main function is the entry point for the program. It is the first function that is executed when the program runs.

The next line declares a variable i of type int. This variable will be used as the Loop counter in the for loop.

    for (i = 1; i

        printf("2 * %d = %d\n", i, 2 * i);

    }

This is the for loop. It starts with the for keyword, followed by parentheses that contain three statements separated by semicolons:

The initialization statement i = 1 sets the value of i to 1 before the loop starts.

The loop condition i

The iteration statement i++ increments the value of i by 1 after each iteration of the loop.

Inside the loop, the printf function is used to print a string to the console. The string contains two %d format specifiers, which are placeholders for two integer values that will be printed. The first %d is replaced by the value of i, and the second %d is replaced by the result of 2 * i. The \n at the end of the string is a newline character that causes the output to be printed on a new line.

Finally, the return 0; statement at the end of the main function indicates that the program has completed successfully.



This post first appeared on Java Tutorial - InstanceOfJava, please read the originial post: here

Share the post

C program to print table of 2 using for loop

×

Subscribe to Java Tutorial - Instanceofjava

Get updates delivered right to your inbox!

Thank you for your subscription

×