The following program prints the multiplication table of numbers from 1 to 10:
#include <stdio.h>
/* Print multiplication table from 1 to 10 */
main()
{
int num, times, prod;
num = 1;
while (num <= 10) {
times = 1;
printf("Table of %d\n", num);
while (times <= 10) {
prod = num * times;
printf("%2d x %2d = %3d\n", num, times, prod);
times++;
}
num++;
}
}
This program consists of a single function “main”, including a comment, declarations, variables, arithmetic expressions, loops and formatted output.
The line:
/* Print multiplication table from 1 to 10 */
is a comment, which in this case explains what the program does. Any characters between /* and */ are ignored by the compiler; they may be used freely to make the program easier to understand. Comments can appear anywhere a blank or tab or newline can.
In C, all variables must be declared before they are used, usually at the beginning of the function before any executable statements. A declaration announces the properties of variables; it consists of a type name and a list of variables, such as
int num, times, prod;
The type int means that the variables listed are integers.
Declaration is followed by an assignment statement, which sets a variable to their initial values.
num = 1;
Individual statements are terminated by semicolons.
Every multiplication table and each line of the multiplication table is computed the same way, so we can use loops that repeat for numbers from 1 to 10 and once per output line; this is the purpose of the while loop. Here we use two while loops(nested loops).
while (num <= 10) { ... while (times <= 10) { ... } }
The while loop operates like this: The condition in the parentheses is tested. If it is true (num is less than or equal to 10), the body (statements enclosed in the curly braces) of the loop is executed. Then, the condition is re-tested, and if true, the body is executed again. When the condition becomes false, the loop ends, and execution continues at the statement that follows the loop.
The body of the while loop can be one or more statements enclosed in curly braces, or a single statement without braces, as in:
while (value < 100) value++;
“++” is called increment operator and increases the value of the operand by one.
value++
is same as
value = value + 1
Although C compilers don’t care about how the program looks, proper indentation and spacing are critical in making programs easy for people to read. It’s recommended to write only one statement per line, and using blanks around operators to clarify grouping.
We compute the product inside the inner while loop using the statement:
prod = num * times;
Then we print the product using printf:
printf("%2d x %2d = %3d\n", num, times, prod);
printf is a general-purpose output formatting function. Its first argument is a string of characters to be printed, with each % indicating where one of the other (second, third, …) arguments is to be substituted, and in what form it is to be printed. For instance, %d specifies an integer argument. Specifying a digit between % and d tells how wide the field is, making the output prettier.
So, in the above printf, first %2d maps to num variable which is two digits wide. Second %2d maps to times variable which is again two digits wide. %3d maps to prod variable which is 3 digits wide.