{"id":8010,"date":"2023-01-18T15:56:26","date_gmt":"2023-01-18T13:56:26","guid":{"rendered":"https:\/\/blue.pri.ee\/ttu\/?page_id=8010"},"modified":"2025-09-17T11:36:41","modified_gmt":"2025-09-17T09:36:41","slug":"loops-multiplication-table","status":"publish","type":"page","link":"https:\/\/blue.pri.ee\/ttu\/programming-i\/samples\/loops-multiplication-table\/","title":{"rendered":"Loops &#8211; multiplication table"},"content":{"rendered":"<p>We will create two example programs to demonstrate use of loops to generate multiplication tables.<\/p>\n<div id=\"ez-toc-container\" class=\"ez-toc-v2_0_82_2 counter-hierarchy ez-toc-counter ez-toc-grey ez-toc-container-direction\">\n<div class=\"ez-toc-title-container\">\n<span class=\"ez-toc-title-toggle\"><a href=\"#\" class=\"ez-toc-pull-right ez-toc-btn ez-toc-btn-xs ez-toc-btn-default ez-toc-toggle\" aria-label=\"Toggle Table of Content\"><span class=\"ez-toc-js-icon-con\"><span class=\"\"><span class=\"eztoc-hide\" style=\"display:none;\">Toggle<\/span><span class=\"ez-toc-icon-toggle-span\"><svg style=\"fill: #999;color:#999\" xmlns=\"http:\/\/www.w3.org\/2000\/svg\" class=\"list-377408\" width=\"20px\" height=\"20px\" viewBox=\"0 0 24 24\" fill=\"none\"><path d=\"M6 6H4v2h2V6zm14 0H8v2h12V6zM4 11h2v2H4v-2zm16 0H8v2h12v-2zM4 16h2v2H4v-2zm16 0H8v2h12v-2z\" fill=\"currentColor\"><\/path><\/svg><svg style=\"fill: #999;color:#999\" class=\"arrow-unsorted-368013\" xmlns=\"http:\/\/www.w3.org\/2000\/svg\" width=\"10px\" height=\"10px\" viewBox=\"0 0 24 24\" version=\"1.2\" baseProfile=\"tiny\"><path d=\"M18.2 9.3l-6.2-6.3-6.2 6.3c-.2.2-.3.4-.3.7s.1.5.3.7c.2.2.4.3.7.3h11c.3 0 .5-.1.7-.3.2-.2.3-.5.3-.7s-.1-.5-.3-.7zM5.8 14.7l6.2 6.3 6.2-6.3c.2-.2.3-.5.3-.7s-.1-.5-.3-.7c-.2-.2-.4-.3-.7-.3h-11c-.3 0-.5.1-.7.3-.2.2-.3.5-.3.7s.1.5.3.7z\"\/><\/svg><\/span><\/span><\/span><\/a><\/span><\/div>\n<nav><ul class='ez-toc-list ez-toc-list-level-1 ' ><li class='ez-toc-page-1 ez-toc-heading-level-3'><a class=\"ez-toc-link ez-toc-heading-1\" href=\"https:\/\/blue.pri.ee\/ttu\/programming-i\/samples\/loops-multiplication-table\/#Example_1_Multiplication_table_with_used-specified_multiplier\" >Example 1: Multiplication table with used-specified multiplier<\/a><ul class='ez-toc-list-level-4' ><li class='ez-toc-heading-level-4'><a class=\"ez-toc-link ez-toc-heading-2\" href=\"https:\/\/blue.pri.ee\/ttu\/programming-i\/samples\/loops-multiplication-table\/#Algorithm_for_the_task\" >Algorithm for the task<\/a><\/li><\/ul><\/li><li class='ez-toc-page-1 ez-toc-heading-level-3'><a class=\"ez-toc-link ez-toc-heading-3\" href=\"https:\/\/blue.pri.ee\/ttu\/programming-i\/samples\/loops-multiplication-table\/#Example_2_Complete_multiplication_table\" >Example 2: Complete multiplication table<\/a><\/li><\/ul><\/nav><\/div>\n<h3><span class=\"ez-toc-section\" id=\"Example_1_Multiplication_table_with_used-specified_multiplier\"><\/span>Example 1: Multiplication table with used-specified multiplier<span class=\"ez-toc-section-end\"><\/span><\/h3>\n<p>In the first program, we will ask the user for the multiplier and generate the multiplication table for that value from 0 to 10, which is defined as a macro <span class=\"lang:default highlight:0 decode:true crayon-inline\">MULT_LIMIT<\/span> . Notice, that the loop condition uses\u00a0 <span class=\"lang:default highlight:0 decode:true crayon-inline\">&lt;=<\/span>\u00a0 &#8211; this means that the ending value is inclusive, which is typically not what we would want, but in this case we do.<\/p>\n<pre class=\"toolbar:2 lang:default decode:true\">\/**\r\n * File:        mult1.c\r\n * Author:      Risto Heinsar\r\n * Created:     18.01.2023\r\n * Edited:      20.06.2025\r\n *\r\n * Description: Sample code for looping, demonstrating how to generate a\r\n *              multiplication table for a single number.\r\n *\/\r\n\r\n#include &lt;stdio.h&gt;\r\n\r\n#define MULT_LIMIT 10\r\n\r\nint main(void)\r\n{\r\n    int multiplier;\r\n    \r\n    printf(\"Enter the number you would like to multiply: \");\r\n    scanf(\"%d\", &amp;multiplier);\r\n    \r\n    printf(\"Generating multiplication table of %d from 0 to %d\\n\",\r\n           multiplier, MULT_LIMIT);\r\n    \r\n    \/\/ Loop for multiplication, end value is inclusive\r\n    for (int i = 0; i &lt;= MULT_LIMIT; i++)\r\n    {\r\n        \/\/ Calculate answer\r\n        int product = multiplier * i;\r\n        \r\n        \/\/ Print the operation and the answer\r\n        printf(\"%d * %d = %d\\n\", multiplier, i, product);\r\n    }\r\n    return 0;\r\n}\r\n<\/pre>\n<h4><span class=\"ez-toc-section\" id=\"Algorithm_for_the_task\"><\/span>Algorithm for the task<span class=\"ez-toc-section-end\"><\/span><\/h4>\n<p>In the diagram, notice the control flows &#8211; when using loops, the control flow returns to an earlier point. It&#8217;s also important to have the condition correctly formed and located. In this case, we are using an entry-controlled loop.<\/p>\n<p><a href=\"https:\/\/blue.pri.ee\/ttu\/wp-content\/uploads\/2025\/09\/ex_mult_single_en.png\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-10628\" src=\"https:\/\/blue.pri.ee\/ttu\/wp-content\/uploads\/2025\/09\/ex_mult_single_en.png\" alt=\"\" width=\"599\" height=\"753\" srcset=\"https:\/\/blue.pri.ee\/ttu\/wp-content\/uploads\/2025\/09\/ex_mult_single_en.png 599w, https:\/\/blue.pri.ee\/ttu\/wp-content\/uploads\/2025\/09\/ex_mult_single_en-239x300.png 239w\" sizes=\"auto, (max-width: 599px) 100vw, 599px\" \/><\/a><\/p>\n<h3><span class=\"ez-toc-section\" id=\"Example_2_Complete_multiplication_table\"><\/span>Example 2: Complete multiplication table<span class=\"ez-toc-section-end\"><\/span><\/h3>\n<p>In the second example, we will take a look at nested loops and produce a full multiplication table from 1 to 10.<\/p>\n<p>Notice, that contrary to usual, the starting value of the counters is 1 and the ending value is again inclusive. Also notice the location of the line change and padding the answers with spaces! Think how and when the\u00a0 <span class=\"lang:default highlight:0 decode:true crayon-inline\">printf()<\/span>\u00a0 statements are being used!<\/p>\n<pre class=\"toolbar:2 lang:default decode:true\">\/**\r\n * File:        mult2.c\r\n * Author:      Risto Heinsar\r\n * Created:     18.01.2023\r\n * Edited:      20.06.2023\r\n *\r\n * Description: Sample code for nested loops, demonstrating how to generate\r\n *              a full multiplication table.\r\n *\/\r\n\r\n#include &lt;stdio.h&gt;\r\n\r\n#define MULT_LIMIT 10\r\n\r\nint main(void)\r\n{\r\n    \/\/ Picking out the value for multiplication\r\n    for (int i = 1; i &lt;= MULT_LIMIT; i++)\r\n    {\r\n        \/\/ Picking out the column value for multiplication\r\n        for (int j = 1; j &lt;= MULT_LIMIT; j++)\r\n        {\r\n            \/\/ Calculate the product\r\n            int product = i * j;\r\n            \r\n            \/\/ Display the product with 3 characters of spacing for alignment\r\n            \/\/ All numbers are printed in the same     \r\n            printf(\"%3d \", product);\r\n        }\r\n        \r\n        \/\/ Reached the end of a line (1 x 10, 2 x 10, etc), print a newline\r\n        printf(\"\\n\");\r\n    }\r\n    return 0;\r\n}\r\n<\/pre>\n<p>&nbsp;<\/p>\n","protected":false},"excerpt":{"rendered":"<p>We will create two example programs to demonstrate use of loops to generate multiplication tables. Example 1: Multiplication table with used-specified multiplier In the first program, we will ask the user for the multiplier and generate the multiplication table for that value from 0 to 10, which is defined as a macro MULT_LIMIT . Notice, &hellip; <a href=\"https:\/\/blue.pri.ee\/ttu\/programming-i\/samples\/loops-multiplication-table\/\" class=\"more-link\">Loe edasi <span class=\"screen-reader-text\">Loops &#8211; multiplication table<\/span> <span class=\"meta-nav\">&rarr;<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"parent":1521,"menu_order":4,"comment_status":"closed","ping_status":"closed","template":"page-templates\/code-width-wide.php","meta":{"footnotes":""},"class_list":["post-8010","page","type-page","status-publish","hentry"],"_links":{"self":[{"href":"https:\/\/blue.pri.ee\/ttu\/wp-json\/wp\/v2\/pages\/8010","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/blue.pri.ee\/ttu\/wp-json\/wp\/v2\/pages"}],"about":[{"href":"https:\/\/blue.pri.ee\/ttu\/wp-json\/wp\/v2\/types\/page"}],"author":[{"embeddable":true,"href":"https:\/\/blue.pri.ee\/ttu\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/blue.pri.ee\/ttu\/wp-json\/wp\/v2\/comments?post=8010"}],"version-history":[{"count":5,"href":"https:\/\/blue.pri.ee\/ttu\/wp-json\/wp\/v2\/pages\/8010\/revisions"}],"predecessor-version":[{"id":10638,"href":"https:\/\/blue.pri.ee\/ttu\/wp-json\/wp\/v2\/pages\/8010\/revisions\/10638"}],"up":[{"embeddable":true,"href":"https:\/\/blue.pri.ee\/ttu\/wp-json\/wp\/v2\/pages\/1521"}],"wp:attachment":[{"href":"https:\/\/blue.pri.ee\/ttu\/wp-json\/wp\/v2\/media?parent=8010"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}