{"id":12076,"date":"2022-10-03T04:11:46","date_gmt":"2022-10-03T04:11:46","guid":{"rendered":"http:\/\/fastbitlab.com\/?p=12076"},"modified":"2023-09-28T12:46:13","modified_gmt":"2023-09-28T07:16:13","slug":"microcontroller-embedded-c-programming-lecture-121-while-loop-exercise","status":"publish","type":"post","link":"https:\/\/fastbitlab.com\/blog\/microcontroller-embedded-c-programming-lecture-121-while-loop-exercise\/","title":{"rendered":"Microcontroller Embedded C Programming Lecture 121| while loop exercise"},"content":{"rendered":"<div class=\"boldgrid-section\" style=\"background-image: linear-gradient(to left, #eeeeee, #eeeeee);\" data-bg-color-1=\"#EEEEEE\" data-bg-color-2=\"#EEEEEE\" data-bg-direction=\"to left\">\n<div class=\"container\">\n<div class=\"row\" style=\"padding-top: 35px; padding-bottom: 0px; background-image: linear-gradient(to left, #eeeeee, #eeeeee);\" data-bg-color-1=\"#EEEEEE\" data-bg-color-2=\"#EEEEEE\" data-bg-direction=\"to left\">\n<div class=\"col-md-1 col-sm-12 col-xs-12 col-lg-1\">\n<div class=\"boldgrid-shortcode\" data-imhwpb-draggable=\"true\">\n\n<\/div>\n<p>&nbsp;<\/p>\n<\/div>\n<div class=\"col-md-10 col-sm-12 col-xs-12 col-lg-10\">\n<h1 class=\"\" style=\"text-align: center; font-size: 35px; border-width: 0px; line-height: 45px; font-family: 'Roboto Slab'; font-weight: 400;\" data-font-family=\"Roboto Slab\" data-font-weight=\"400\" data-font-style=\"\"><strong><span style=\"color: #000080;\">while loop exercise<\/span><\/strong><\/h1>\n<div class=\"row bg-editor-hr-wrap\" style=\"border-width: 0px; margin-top: -25px;\">\n<div class=\"col-lg-12 col-md-12 col-xs-12 col-sm-12\">\n<div>\n<p>&nbsp;<\/p>\n<div class=\"bg-hr bg-hr-10\" style=\"border-style: solid; border-width: 0px 0px 3px; width: 80%; margin-top: -25px;\"><\/div>\n<p>&nbsp;<\/p>\n<\/div>\n<\/div>\n<\/div>\n<p class=\"\" style=\"font-size: 25px; line-height: 40px; border-width: 0px; font-family: 'Roboto Slab'; font-weight: 400;\" data-font-family=\"Roboto Slab\" data-font-weight=\"400\" data-font-style=\"\"><span style=\"text-decoration: underline; color: #cc0c0c;\"><b>Exercise:<\/b><\/span><\/p>\n<ul class=\"\" style=\"font-size: 17px; line-height: 1.8em; border-width: 0px; font-family: 'Roboto Slab'; font-weight: 400;\" data-font-family=\"Roboto Slab\" data-font-weight=\"400\" data-font-style=\"\">\n<li style=\"font-weight: 400;\" aria-level=\"1\"><span style=\"font-weight: 400; color: #000000;\">Write a program to print all even numbers between 0 to 100 (including the boundary numbers).&nbsp;<\/span><\/li>\n<li style=\"font-weight: 400;\" aria-level=\"1\"><span style=\"font-weight: 400; color: #000000;\">Also, count and print how many even numbers you find.&nbsp;<\/span>\n<ul>\n<li style=\"font-weight: 400;\" aria-level=\"2\"><span style=\"font-weight: 400; color: #000000;\">Use &#8216;while&#8217; loop, or &#8216;for&#8217; loop, or &#8216;do while&#8217; loop.&nbsp;<\/span><\/li>\n<li style=\"font-weight: 400;\" aria-level=\"2\"><span style=\"font-weight: 400; color: #000000;\">Later change the program to accept boundary numbers from the user.<\/span><\/li>\n<\/ul>\n<\/li>\n<\/ul>\n<p class=\"\">&nbsp;<\/p>\n<p class=\"\" style=\"font-size: 20px;\"><span style=\"text-decoration: underline; color: #000080;\"><strong>Code:<\/strong><\/span><\/p>\n<pre class=\"color-5-text-contrast color5-background-color\" style=\"font-size: 12px; box-shadow: #cecece 0px 0px 0px 0px;\"><span style=\"color: #ff99cc;\">#include<\/span>&lt;stdio.h&gt;\r\n<span style=\"color: #ff99cc;\">#include<\/span>&lt;stdint.h&gt;\r\n\r\n<span style=\"color: #ff99cc;\">void<\/span> wait_for_user_input(<span style=\"color: #ff99cc;\">void<\/span>);\r\n\r\n<span style=\"color: #ff99cc;\">int<\/span> main(<span style=\"color: #ff99cc;\">void<\/span>)\r\n{\r\n    <span style=\"color: #008000;\">int32_t<\/span> start_num , end_num;\r\n    <span style=\"color: #008000;\">uint32_t<\/span> even;\r\n    <span style=\"color: #ff00ff;\">printf<\/span>(\"Enter starting and ending numbers(give space between 2 nos):\");\r\n    <span style=\"color: #ff00ff;\">scanf<\/span>(\"%d %d\",&amp;start_num,&amp;end_num);\r\n\r\n   <span style=\"color: #ff00ff;\"> printf<\/span>(\"Even numbers are :\\n\");\r\n    even=0;\r\n    <span style=\"color: #ff99cc;\">while<\/span>(start_num &lt;= end_num){\r\n         <span style=\"color: #ff99cc;\">if<\/span>(!(start_num % 2) ){\r\n              <span style=\"color: #ff00ff;\">printf<\/span>(\"%d\\t\",start_num);\r\n              even++;\r\n          }\r\n          start_num++;\r\n    }<span style=\"color: #008000;\">\/\/end of while loop<\/span>\r\n\r\n    <span style=\"color: #ff00ff;\">printf<\/span>(\"\\nTotal even numbers : %u\\n\",even);\r\n\r\n  wait_for_user_input();\r\n}\r\n\r\n\r\nvoid wait_for_user_input(void)\r\n{\r\n   <span style=\"color: #ff00ff;\"> printf<\/span>(\"Press enter key to exit this application\");\r\n\r\n  <span style=\"color: #ff99cc;\">while<\/span>(getchar() != '\\n')\r\n  {\r\n      <span style=\"color: #008000;\">\/\/just read the input buffer and do nothing<\/span>\r\n  }\r\n  getchar();\r\n\r\n}<\/pre>\n<p class=\"\">&nbsp;<\/p>\n<p class=\"\" style=\"font-size: 17px; line-height: 1.8em; border-width: 0px; font-family: 'Roboto Slab'; font-weight: 400;\" data-font-family=\"Roboto Slab\" data-font-weight=\"400\" data-font-style=\"\"><span style=\"font-weight: 400; color: #000000;\">First, accept two numbers starting and ending numbers.<\/span><\/p>\n<p class=\"\" style=\"font-size: 17px; line-height: 1.8em; border-width: 0px; font-family: 'Roboto Slab'; font-weight: 400;\" data-font-family=\"Roboto Slab\" data-font-weight=\"400\" data-font-style=\"\"><span style=\"color: #000000;\"><span style=\"font-weight: 400;\">I created the variables <\/span><b>start_num<\/b><span style=\"font-weight: 400;\"> and <\/span><b>end_num<\/b><span style=\"font-weight: 400;\">, and the variable is of type signed int. It is signed int because the user is allowed to enter the signed number.<\/span><\/span><\/p>\n<p class=\"\" style=\"font-size: 17px; line-height: 1.8em; border-width: 0px; font-family: 'Roboto Slab'; font-weight: 400;\" data-font-family=\"Roboto Slab\" data-font-weight=\"400\" data-font-style=\"\"><span style=\"color: #000000;\"><span style=\"font-weight: 400;\">And another variable is <\/span><b>even<\/b><span style=\"font-weight: 400;\">. This must be unsigned because the count cannot be negative.<\/span><\/span><\/p>\n<div><\/div>\n<p class=\"\" style=\"font-size: 17px; line-height: 1.8em; border-width: 0px; font-family: 'Roboto Slab'; font-weight: 400;\" data-font-family=\"Roboto Slab\" data-font-weight=\"400\" data-font-style=\"\"><span style=\"color: #000000;\"><span style=\"font-weight: 400;\">Then write a printf statement.<\/span><b>&nbsp;<\/b><\/span><\/p>\n<p class=\"\" style=\"font-size: 17px; line-height: 1.8em; border-width: 0px; font-family: 'Roboto Slab'; font-weight: 400;\" data-font-family=\"Roboto Slab\" data-font-weight=\"400\" data-font-style=\"\"><span style=\"color: #454575;\"><b>printf(\u201cEnter starting and ending numbers(give space between 2 numbers):\u201d);&nbsp;<\/b><\/span><\/p>\n<p class=\"\">&nbsp;<\/p>\n<p class=\"\" style=\"font-size: 17px; line-height: 1.8em; border-width: 0px; font-family: 'Roboto Slab'; font-weight: 400;\" data-font-family=\"Roboto Slab\" data-font-weight=\"400\" data-font-style=\"\"><span style=\"font-weight: 400; color: #000000;\">After that, let&#8217;s scan the numbers.&nbsp;<\/span><\/p>\n<p class=\"\" style=\"font-size: 17px; line-height: 1.8em; border-width: 0px; font-family: 'Roboto Slab'; font-weight: 400;\" data-font-family=\"Roboto Slab\" data-font-weight=\"400\" data-font-style=\"\"><span style=\"color: #454575;\"><b>scanf(\u201c%d %d\u201d, &amp;start_num, &amp;end_num);<\/b><\/span><\/p>\n<p class=\"\" style=\"font-size: 17px; line-height: 1.8em; border-width: 0px; font-family: 'Roboto Slab'; font-weight: 400;\" data-font-family=\"Roboto Slab\" data-font-weight=\"400\" data-font-style=\"\"><span style=\"font-weight: 400; color: #000000;\">Let&#8217;s read the input so scanf, I would use %d space %d and the first one is start_num and the second one is end_num.<\/span><\/p>\n<p class=\"\">&nbsp;<\/p>\n<p class=\"\" style=\"font-size: 17px; line-height: 1.8em; border-width: 0px; font-family: 'Roboto Slab'; font-weight: 400;\" data-font-family=\"Roboto Slab\" data-font-weight=\"400\" data-font-style=\"\"><span style=\"font-weight: 400; color: #000000;\">After that, you have to use a loop.&nbsp;<\/span><\/p>\n<p class=\"\" style=\"font-size: 17px; line-height: 1.8em; border-width: 0px; font-family: 'Roboto Slab'; font-weight: 400;\" data-font-family=\"Roboto Slab\" data-font-weight=\"400\" data-font-style=\"\"><span style=\"color: #454575;\"><b>while(start_num &lt;= end_num<\/b><span style=\"font-weight: 400;\">)<\/span><\/span><\/p>\n<p class=\"\" style=\"font-size: 17px; line-height: 1.8em; border-width: 0px; font-family: 'Roboto Slab'; font-weight: 400;\" data-font-family=\"Roboto Slab\" data-font-weight=\"400\" data-font-style=\"\"><span style=\"font-weight: 400; color: #000000;\">You should start from the start number, check whether it is even or odd, and then proceed to the next number until that number is less than or equal to the end number. That&#8217;s a logic.<\/span><\/p>\n<p class=\"\">&nbsp;<\/p>\n<p class=\"\" style=\"font-size: 17px; line-height: 1.8em; border-width: 0px; font-family: 'Roboto Slab'; font-weight: 400;\" data-font-family=\"Roboto Slab\" data-font-weight=\"400\" data-font-style=\"\"><span style=\"font-weight: 400; color: #000000;\">First, let&#8217;s check whether a start_num is even or odd. For that, we use the modulus(%) operator.&nbsp;<\/span><\/p>\n<p class=\"\" style=\"font-size: 17px; line-height: 1.8em; border-width: 0px; font-family: 'Roboto Slab'; font-weight: 400;\" data-font-family=\"Roboto Slab\" data-font-weight=\"400\" data-font-style=\"\"><span style=\"color: #454575;\"><b>if(!(start_num % 2))<\/b><span style=\"font-weight: 400;\">&nbsp;<\/span><\/span><\/p>\n<p class=\"\" style=\"font-size: 17px; line-height: 1.8em; border-width: 0px; font-family: 'Roboto Slab'; font-weight: 400;\" data-font-family=\"Roboto Slab\" data-font-weight=\"400\" data-font-style=\"\"><span style=\"font-weight: 400; color: #000000;\">If that expression is 0, then it&#8217;s an even number. I can use not operator(!) for this expression.&nbsp;<\/span><\/p>\n<p class=\"\" style=\"font-size: 17px; line-height: 1.8em; border-width: 0px; font-family: 'Roboto Slab'; font-weight: 400;\" data-font-family=\"Roboto Slab\" data-font-weight=\"400\" data-font-style=\"\"><span style=\"font-weight: 400; color: #000000;\">If start_num is really an even number, so (start_num % 2) expression will be 0. That is, the even number modulus of 2 is 0. It is always 0. That&#8217;s why 0 means false. So, not of(!) false is true. That means a number is an even number.&nbsp;<\/span><\/p>\n<p class=\"\" style=\"font-size: 17px; line-height: 1.8em; border-width: 0px; font-family: 'Roboto Slab'; font-weight: 400;\" data-font-family=\"Roboto Slab\" data-font-weight=\"400\" data-font-style=\"\"><span style=\"font-weight: 400; color: #000000;\">If it is an even number I will print that.&nbsp;<\/span><\/p>\n<p class=\"\" style=\"font-size: 17px; line-height: 1.8em; border-width: 0px; font-family: 'Roboto Slab'; font-weight: 400;\" data-font-family=\"Roboto Slab\" data-font-weight=\"400\" data-font-style=\"\"><span style=\"color: #454575;\"><b>printf(\u201c%d\\t\u201d, start_num);<\/b><\/span><\/p>\n<p class=\"\" style=\"font-size: 17px; line-height: 1.8em; border-width: 0px; font-family: 'Roboto Slab'; font-weight: 400;\" data-font-family=\"Roboto Slab\" data-font-weight=\"400\" data-font-style=\"\"><span style=\"font-weight: 400; color: #000000;\">I would give \\t instead of \\n because I want to print horizontally by giving a tab between two numbers. And here I would print start_num.&nbsp;<\/span><\/p>\n<p class=\"\" style=\"font-size: 17px; line-height: 1.8em; border-width: 0px; font-family: 'Roboto Slab'; font-weight: 400;\" data-font-family=\"Roboto Slab\" data-font-weight=\"400\" data-font-style=\"\"><span style=\"color: #000000;\"><span style=\"font-weight: 400;\">And after that, I will increment the <\/span><b>even<\/b><span style=\"font-weight: 400;\">. This counts how many even numbers are detected.<\/span><\/span><\/p>\n<p class=\"\" style=\"font-size: 17px; line-height: 1.8em; border-width: 0px; font-family: 'Roboto Slab'; font-weight: 400;\" data-font-family=\"Roboto Slab\" data-font-weight=\"400\" data-font-style=\"\"><span style=\"color: #000000;\"><span style=\"font-weight: 400;\">But you must initialize <\/span><b>even<\/b><span style=\"font-weight: 400;\"> before incrementing, so I would do that here even = 0 (18th line). Let&#8217;s start with 0.<\/span><\/span><\/p>\n<p class=\"\">&nbsp;<\/p>\n<p class=\"\" style=\"font-size: 17px; line-height: 1.8em; border-width: 0px; font-family: 'Roboto Slab'; font-weight: 400;\" data-font-family=\"Roboto Slab\" data-font-weight=\"400\" data-font-style=\"\"><span style=\"color: #000000;\"><span style=\"font-weight: 400;\">After that, you have to keep incrementing start_num. So, <\/span><span style=\"color: #454575;\"><b>start_num++<\/b><\/span><span style=\"font-weight: 400;\">. This is the end of the while loop.&nbsp;<\/span><\/span><\/p>\n<p class=\"\" style=\"font-size: 17px; line-height: 1.8em; border-width: 0px; font-family: 'Roboto Slab'; font-weight: 400;\" data-font-family=\"Roboto Slab\" data-font-weight=\"400\" data-font-style=\"\"><span style=\"font-weight: 400; color: #000000;\">So, start_num will be incremented, and again it will be looped back, and again it will be checked against the end number.<\/span><\/p>\n<p class=\"\" style=\"font-size: 17px; line-height: 1.8em; border-width: 0px; font-family: 'Roboto Slab'; font-weight: 400;\" data-font-family=\"Roboto Slab\" data-font-weight=\"400\" data-font-style=\"\"><span style=\"font-weight: 400; color: #000000;\">Here, I can just print one message &#8220;Even numbers are: \\n&#8221;.<\/span><\/p>\n<p class=\"\" style=\"font-size: 17px; line-height: 1.8em; border-width: 0px; font-family: 'Roboto Slab'; font-weight: 400;\" data-font-family=\"Roboto Slab\" data-font-weight=\"400\" data-font-style=\"\"><span style=\"font-weight: 400; color: #000000;\">And when that loop ends I&#8217;m going to print the total number of even numbers counted.&nbsp;&nbsp;<\/span><\/p>\n<p class=\"\" style=\"font-size: 17px; line-height: 1.8em; border-width: 0px; font-family: 'Roboto Slab'; font-weight: 400;\" data-font-family=\"Roboto Slab\" data-font-weight=\"400\" data-font-style=\"\"><span style=\"color: #454575;\"><b>printf(\u201cTotal even numbers: %u\\n\u201d, even);&nbsp;&nbsp;<\/b><\/span><\/p>\n<p class=\"\" style=\"font-size: 17px; line-height: 1.8em; border-width: 0px; font-family: 'Roboto Slab'; font-weight: 400;\" data-font-family=\"Roboto Slab\" data-font-weight=\"400\" data-font-style=\"\"><span style=\"color: #000000;\"><span style=\"font-weight: 400;\">Take <\/span><b>even<\/b><span style=\"font-weight: 400;\"> as unsigned, because the count cannot be negative. So, I would use data type uint32 for that.&nbsp;<\/span><\/span><\/p>\n<p class=\"\">&nbsp;<\/p>\n<p class=\"\" style=\"font-size: 25px; line-height: 40px; border-width: 0px; font-family: 'Roboto Slab'; font-weight: 400;\" data-font-family=\"Roboto Slab\" data-font-weight=\"400\" data-font-style=\"\"><span style=\"text-decoration: underline; color: #cc0c0c;\"><b>Output<\/b><\/span><\/p>\n<p class=\"\" style=\"font-size: 17px; line-height: 1.8em; border-width: 0px; font-family: 'Roboto Slab'; font-weight: 400;\" data-font-family=\"Roboto Slab\" data-font-weight=\"400\" data-font-style=\"\"><span style=\"font-weight: 400; color: #000000;\">Enter the starting and ending numbers, let&#8217;s try -2 to -8.<\/span><\/p>\n<p class=\"\" style=\"font-size: 17px; line-height: 1.8em; border-width: 0px; font-family: 'Roboto Slab'; font-weight: 400;\" data-font-family=\"Roboto Slab\" data-font-weight=\"400\" data-font-style=\"\"><span style=\"font-weight: 400; color: #000000;\">It is not working, because the ending number has to be greater than the starting number. So, that condition you have to check here.<\/span><\/p>\n<figure id=\"attachment_12080\" aria-describedby=\"caption-attachment-12080\" style=\"width: 577px\" class=\"wp-caption aligncenter\"><img fetchpriority=\"high\" decoding=\"async\" class=\"wp-image-12080 \" src=\"http:\/\/fastbitlab.com\/wp-content\/uploads\/2022\/10\/Figure-2-1024x214.png\" alt=\"Figure 2. Output\" width=\"577\" height=\"121\" srcset=\"https:\/\/fastbitlab.com\/blog\/wp-content\/uploads\/2022\/10\/Figure-2-1024x214.png 1024w, https:\/\/fastbitlab.com\/blog\/wp-content\/uploads\/2022\/10\/Figure-2-300x63.png 300w, https:\/\/fastbitlab.com\/blog\/wp-content\/uploads\/2022\/10\/Figure-2-768x160.png 768w, https:\/\/fastbitlab.com\/blog\/wp-content\/uploads\/2022\/10\/Figure-2-600x125.png 600w, https:\/\/fastbitlab.com\/blog\/wp-content\/uploads\/2022\/10\/Figure-2-200x42.png 200w, https:\/\/fastbitlab.com\/blog\/wp-content\/uploads\/2022\/10\/Figure-2-400x84.png 400w, https:\/\/fastbitlab.com\/blog\/wp-content\/uploads\/2022\/10\/Figure-2-800x167.png 800w, https:\/\/fastbitlab.com\/blog\/wp-content\/uploads\/2022\/10\/Figure-2-1200x251.png 1200w, https:\/\/fastbitlab.com\/blog\/wp-content\/uploads\/2022\/10\/Figure-2.png 1221w\" sizes=\"(max-width: 577px) 100vw, 577px\" \/><figcaption id=\"caption-attachment-12080\" class=\"wp-caption-text\"><span style=\"color: #000000;\">Figure 1. Output<\/span><\/figcaption><\/figure>\n<p class=\"\">&nbsp;<\/p>\n<p class=\"\" style=\"font-size: 17px; line-height: 1.8em; border-width: 0px; font-family: 'Roboto Slab'; font-weight: 400;\" data-font-family=\"Roboto Slab\" data-font-weight=\"400\" data-font-style=\"\"><span style=\"font-weight: 400; color: #000000;\">Here, the ending number is less than the starting number, which is not allowed. That gives an error. So, the ending number has to be greater than or equal to the starting number.&nbsp;<\/span><\/p>\n<p class=\"\" style=\"font-size: 17px; line-height: 1.8em; border-width: 0px; font-family: 'Roboto Slab'; font-weight: 400;\" data-font-family=\"Roboto Slab\" data-font-weight=\"400\" data-font-style=\"\"><span style=\"color: #454575;\"><b>if(end_num &lt; start_num)<\/b><\/span><\/p>\n<p class=\"\" style=\"font-size: 17px; line-height: 1.8em; border-width: 0px; font-family: 'Roboto Slab'; font-weight: 400;\" data-font-family=\"Roboto Slab\" data-font-weight=\"400\" data-font-style=\"\"><span style=\"font-weight: 400; color: #000000;\">If the end_num is less than start_num, then print that \u201cending number should be greater than starting number\u201d. Then you have to exit. So, let&#8217;s wait_for_user_input and return. The code is shown below.<\/span><\/p>\n<pre class=\"color-5-text-contrast color5-background-color\" style=\"font-size: 12px; box-shadow: #cecece 0px 0px 0px 0px inset;\" data-font-family=\"Roboto Slab\" data-font-weight=\"400\"><span style=\"color: #ff99cc;\">int<\/span> main(<span style=\"color: #ff99cc;\">void<\/span>)\r\n{\r\n<span style=\"color: #008000;\">    int32_t<\/span> start_num , end_num;\r\n<span style=\"color: #008000;\">    uint32_t<\/span> even;\r\n<span style=\"color: #ff00ff;\">    printf<\/span>(\"Enter starting and ending numbers(give space between 2 nos):\");\r\n<span style=\"color: #ff00ff;\">    scanf<\/span>(\"%d %d\",&amp;start_num,&amp;end_num);\r\n\r\n<strong><span style=\"color: #ffffff;\"><span style=\"color: #ff99cc;\">    if<\/span>(end_num &lt; start_num){<\/span><\/strong>\r\n<span style=\"color: #008000;\"><strong>      \/\/error<\/strong><\/span>\r\n<strong><span style=\"color: #ffffff;\"><span style=\"color: #ff00ff;\">       printf<\/span>(\"ending number should be &gt; starting number\\n\");<\/span><\/strong>\r\n<strong><span style=\"color: #ffffff;\">      wait_for_user_input();<\/span><\/strong>\r\n<strong><span style=\"color: #ffffff;\"><span style=\"color: #ff99cc;\">      return<\/span> 0;<\/span><\/strong>\r\n<strong><span style=\"color: #ffffff;\">}<\/span><\/strong>\r\n\r\n<span style=\"color: #ff00ff;\">    printf<\/span>(\"Even numbers are :\\n\");\r\n    even=0;\r\n<span style=\"color: #ff99cc;\">    while<\/span>(start_num &lt;= end_num){\r\n<span style=\"color: #ff99cc;\">          if<\/span>(!(start_num % 2) ){\r\n<span style=\"color: #ff00ff;\">               printf<\/span>(\"%d\\t\",start_num);\r\n               even++;\r\n           }\r\n     start_num++;\r\n     }<span style=\"color: #008000;\">\/\/end of while loop<\/span>\r\n\r\n<span style=\"color: #ff00ff;\">   printf<\/span>(\"\\nTotal even numbers : %u\\n\",even);\r\n\r\nwait_for_user_input();\r\n}<\/pre>\n<p class=\"\">&nbsp;<\/p>\n<p class=\"\">&nbsp;<\/p>\n<p class=\"\" style=\"font-size: 17px; line-height: 1.8em; border-width: 0px; font-family: 'Roboto Slab'; font-weight: 400;\" data-font-family=\"Roboto Slab\" data-font-weight=\"400\" data-font-style=\"\"><span style=\"font-weight: 400; color: #000000;\">I enter starting and ending numbers are -2 to -8.<\/span><\/p>\n<figure id=\"attachment_12083\" aria-describedby=\"caption-attachment-12083\" style=\"width: 608px\" class=\"wp-caption aligncenter\"><img decoding=\"async\" class=\"wp-image-12083\" src=\"http:\/\/fastbitlab.com\/wp-content\/uploads\/2022\/10\/Figure-4-1024x227.png\" alt=\"while loop exercise\" width=\"608\" height=\"135\" srcset=\"https:\/\/fastbitlab.com\/blog\/wp-content\/uploads\/2022\/10\/Figure-4-1024x227.png 1024w, https:\/\/fastbitlab.com\/blog\/wp-content\/uploads\/2022\/10\/Figure-4-300x67.png 300w, https:\/\/fastbitlab.com\/blog\/wp-content\/uploads\/2022\/10\/Figure-4-768x170.png 768w, https:\/\/fastbitlab.com\/blog\/wp-content\/uploads\/2022\/10\/Figure-4-600x133.png 600w, https:\/\/fastbitlab.com\/blog\/wp-content\/uploads\/2022\/10\/Figure-4-200x44.png 200w, https:\/\/fastbitlab.com\/blog\/wp-content\/uploads\/2022\/10\/Figure-4-400x89.png 400w, https:\/\/fastbitlab.com\/blog\/wp-content\/uploads\/2022\/10\/Figure-4-800x177.png 800w, https:\/\/fastbitlab.com\/blog\/wp-content\/uploads\/2022\/10\/Figure-4.png 1172w\" sizes=\"(max-width: 608px) 100vw, 608px\" \/><figcaption id=\"caption-attachment-12083\" class=\"wp-caption-text\"><span style=\"color: #000000;\">Figure 2. Output<\/span><\/figcaption><\/figure>\n<p class=\"\" data-font-family=\"Roboto Slab\" data-font-weight=\"400\" data-font-style=\"\">&nbsp;<\/p>\n<p class=\"\" style=\"font-size: 17px; line-height: 1.8em; border-width: 0px; font-family: 'Roboto Slab'; font-weight: 400;\" data-font-family=\"Roboto Slab\" data-font-weight=\"400\" data-font-style=\"\"><span style=\"font-weight: 400; color: #000000;\">It prints the ending number should be greater than starting number. That&#8217;s good.&nbsp;<\/span><\/p>\n<p class=\"\">&nbsp;<\/p>\n<p class=\"\" style=\"font-size: 17px; line-height: 1.8em; border-width: 0px; font-family: 'Roboto Slab'; font-weight: 400;\" data-font-family=\"Roboto Slab\" data-font-weight=\"400\" data-font-style=\"\"><span style=\"font-weight: 400; color: #000000;\">Let&#8217;s try -233 to 90. It prints the Even numbers, as shown in Figure 3.<\/span><\/p>\n<figure id=\"attachment_12084\" aria-describedby=\"caption-attachment-12084\" style=\"width: 670px\" class=\"wp-caption aligncenter\"><img decoding=\"async\" class=\"wp-image-12084\" src=\"http:\/\/fastbitlab.com\/wp-content\/uploads\/2022\/10\/Figure-5-1024x349.png\" alt=\"while loop exercise- Print Even Numbers\" width=\"670\" height=\"228\" srcset=\"https:\/\/fastbitlab.com\/blog\/wp-content\/uploads\/2022\/10\/Figure-5-1024x349.png 1024w, https:\/\/fastbitlab.com\/blog\/wp-content\/uploads\/2022\/10\/Figure-5-300x102.png 300w, https:\/\/fastbitlab.com\/blog\/wp-content\/uploads\/2022\/10\/Figure-5-768x262.png 768w, https:\/\/fastbitlab.com\/blog\/wp-content\/uploads\/2022\/10\/Figure-5-600x205.png 600w, https:\/\/fastbitlab.com\/blog\/wp-content\/uploads\/2022\/10\/Figure-5-1536x524.png 1536w, https:\/\/fastbitlab.com\/blog\/wp-content\/uploads\/2022\/10\/Figure-5-200x68.png 200w, https:\/\/fastbitlab.com\/blog\/wp-content\/uploads\/2022\/10\/Figure-5-400x136.png 400w, https:\/\/fastbitlab.com\/blog\/wp-content\/uploads\/2022\/10\/Figure-5-800x273.png 800w, https:\/\/fastbitlab.com\/blog\/wp-content\/uploads\/2022\/10\/Figure-5-1200x409.png 1200w, https:\/\/fastbitlab.com\/blog\/wp-content\/uploads\/2022\/10\/Figure-5.png 1857w\" sizes=\"(max-width: 670px) 100vw, 670px\" \/><figcaption id=\"caption-attachment-12084\" class=\"wp-caption-text\"><span style=\"color: #000000;\">Figure 3. Output<\/span><\/figcaption><\/figure>\n<p data-font-family=\"Roboto Slab\" data-font-weight=\"400\" data-font-style=\"\">&nbsp;<\/p>\n<p class=\"\" style=\"font-size: 17px; line-height: 1.8em; border-width: 0px; font-family: 'Roboto Slab'; font-weight: 400;\" data-font-family=\"Roboto Slab\" data-font-weight=\"400\" data-font-style=\"\"><span style=\"font-weight: 400; color: #000000;\">But take a look at the columns, they are not arranged properly. We can arrange it the proper way.&nbsp;<\/span><\/p>\n<p class=\"\" style=\"font-size: 17px; line-height: 1.8em; border-width: 0px; font-family: 'Roboto Slab'; font-weight: 400;\" data-font-family=\"Roboto Slab\" data-font-weight=\"400\" data-font-style=\"\"><span style=\"font-weight: 400; color: #000000;\">Here, let&#8217;s take -232. It actually consumes 4 spaces. There is one space for a negative sign, 2 is second place, 3 is third place, and 2 in fourth place. So, we have to print each number in that format.&nbsp;<\/span><\/p>\n<p class=\"\" style=\"font-size: 17px; line-height: 1.8em; border-width: 0px; font-family: 'Roboto Slab'; font-weight: 400;\" data-font-family=\"Roboto Slab\" data-font-weight=\"400\" data-font-style=\"\"><span style=\"font-weight: 400; color: #000000;\">And take 8, it consumes only one space. So, we have to force every digit to consume 4 places here. If you do that, it looks good.&nbsp;<\/span><\/p>\n<p class=\"\">&nbsp;<\/p>\n<p class=\"\" style=\"font-size: 17px; line-height: 1.8em; border-width: 0px; font-family: 'Roboto Slab'; font-weight: 400;\" data-font-family=\"Roboto Slab\" data-font-weight=\"400\" data-font-style=\"\"><span style=\"font-weight: 400; color: #000000;\">So, we can go to our printf where we are printing that(28th line).&nbsp;<\/span><\/p>\n<p class=\"\" style=\"font-size: 17px; line-height: 1.8em; border-width: 0px; font-family: 'Roboto Slab'; font-weight: 400;\" data-font-family=\"Roboto Slab\" data-font-weight=\"400\" data-font-style=\"\"><span style=\"font-weight: 400; color: #000000;\">Here, instead of %d give %4d, as shown in Figure 6. That means, now each output number consumes 4 spaces in the display. Now you are able to distinguish the output.<\/span><\/p>\n<figure id=\"attachment_12085\" aria-describedby=\"caption-attachment-12085\" style=\"width: 553px\" class=\"wp-caption aligncenter\"><img loading=\"lazy\" decoding=\"async\" class=\"wp-image-12085\" src=\"http:\/\/fastbitlab.com\/wp-content\/uploads\/2022\/10\/Figure-6-1024x469.png\" alt=\"while loop exercise- Print Even Numbers\" width=\"553\" height=\"253\" srcset=\"https:\/\/fastbitlab.com\/blog\/wp-content\/uploads\/2022\/10\/Figure-6-1024x469.png 1024w, https:\/\/fastbitlab.com\/blog\/wp-content\/uploads\/2022\/10\/Figure-6-300x137.png 300w, https:\/\/fastbitlab.com\/blog\/wp-content\/uploads\/2022\/10\/Figure-6-768x351.png 768w, https:\/\/fastbitlab.com\/blog\/wp-content\/uploads\/2022\/10\/Figure-6-600x275.png 600w, https:\/\/fastbitlab.com\/blog\/wp-content\/uploads\/2022\/10\/Figure-6-200x92.png 200w, https:\/\/fastbitlab.com\/blog\/wp-content\/uploads\/2022\/10\/Figure-6-400x183.png 400w, https:\/\/fastbitlab.com\/blog\/wp-content\/uploads\/2022\/10\/Figure-6-800x366.png 800w, https:\/\/fastbitlab.com\/blog\/wp-content\/uploads\/2022\/10\/Figure-6-1200x549.png 1200w, https:\/\/fastbitlab.com\/blog\/wp-content\/uploads\/2022\/10\/Figure-6.png 1283w\" sizes=\"(max-width: 553px) 100vw, 553px\" \/><figcaption id=\"caption-attachment-12085\" class=\"wp-caption-text\"><span style=\"color: #000000;\">Figure 4. Code<\/span><\/figcaption><\/figure>\n<p class=\"\">&nbsp;<\/p>\n<p class=\"\" style=\"font-size: 17px; line-height: 1.8em; border-width: 0px; font-family: 'Roboto Slab'; font-weight: 400;\" data-font-family=\"Roboto Slab\" data-font-weight=\"400\" data-font-style=\"\"><span style=\"font-weight: 400; color: #000000;\">Let&#8217;s run once again and try once again -233 to 90.<\/span><\/p>\n<p class=\"\" style=\"font-size: 17px; line-height: 1.8em; border-width: 0px; font-family: 'Roboto Slab'; font-weight: 400;\" data-font-family=\"Roboto Slab\" data-font-weight=\"400\" data-font-style=\"\"><span style=\"font-weight: 400; color: #000000;\">Now you can see that it is arranged properly. 8 has aligned to the right. That means here 3 dummy spaces have been inserted.&nbsp;<\/span><\/p>\n<p class=\"\" style=\"font-size: 17px; line-height: 1.8em; border-width: 0px; font-family: 'Roboto Slab'; font-weight: 400;\" data-font-family=\"Roboto Slab\" data-font-weight=\"400\" data-font-style=\"\"><span style=\"font-weight: 400; color: #000000;\">The total number of even numbers is 162. So, that works well.&nbsp;<\/span><\/p>\n<figure id=\"attachment_12086\" aria-describedby=\"caption-attachment-12086\" style=\"width: 643px\" class=\"wp-caption aligncenter\"><img loading=\"lazy\" decoding=\"async\" class=\"wp-image-12086\" src=\"http:\/\/fastbitlab.com\/wp-content\/uploads\/2022\/10\/Figure-7-1024x364.png\" alt=\"while loop exercise- Print Even Numbers\" width=\"643\" height=\"229\" srcset=\"https:\/\/fastbitlab.com\/blog\/wp-content\/uploads\/2022\/10\/Figure-7-1024x364.png 1024w, https:\/\/fastbitlab.com\/blog\/wp-content\/uploads\/2022\/10\/Figure-7-300x107.png 300w, https:\/\/fastbitlab.com\/blog\/wp-content\/uploads\/2022\/10\/Figure-7-768x273.png 768w, https:\/\/fastbitlab.com\/blog\/wp-content\/uploads\/2022\/10\/Figure-7-600x213.png 600w, https:\/\/fastbitlab.com\/blog\/wp-content\/uploads\/2022\/10\/Figure-7-1536x546.png 1536w, https:\/\/fastbitlab.com\/blog\/wp-content\/uploads\/2022\/10\/Figure-7-200x71.png 200w, https:\/\/fastbitlab.com\/blog\/wp-content\/uploads\/2022\/10\/Figure-7-400x142.png 400w, https:\/\/fastbitlab.com\/blog\/wp-content\/uploads\/2022\/10\/Figure-7-800x284.png 800w, https:\/\/fastbitlab.com\/blog\/wp-content\/uploads\/2022\/10\/Figure-7-1200x426.png 1200w, https:\/\/fastbitlab.com\/blog\/wp-content\/uploads\/2022\/10\/Figure-7.png 1855w\" sizes=\"(max-width: 643px) 100vw, 643px\" \/><figcaption id=\"caption-attachment-12086\" class=\"wp-caption-text\"><span style=\"color: #000000;\">Figure 5. Output<\/span><\/figcaption><\/figure>\n<p class=\"\">&nbsp;<\/p>\n<p class=\"\" style=\"font-size: 23px; border-width: 0px; font-family: 'Roboto Slab'; font-weight: 400; line-height: 30px;\" data-font-family=\"Roboto Slab\" data-font-weight=\"400\" data-font-style=\"\"><span style=\"color: #000080;\"><b>FastBit Embedded Brain Academy Courses<\/b><\/span><\/p>\n<p class=\"\" style=\"font-size: 17px; border-width: 0px;\"><span style=\"color: #000000;\">C<span style=\"font-weight: 400;\"><span style=\"color: #000000;\">lick here:<\/span><span style=\"color: #0000ff;\">&nbsp;<\/span><\/span><\/span><span style=\"color: #0000ff;\"><a style=\"color: #0000ff; text-decoration: underline;\" href=\"http:\/\/fastbitlab.com\/course1\" target=\"_blank\" rel=\"noopener\"><span style=\"font-weight: 400;\">https:\/\/fastbitlab.com\/course1<\/span><\/a><\/span><\/p>\n<p class=\"\">&nbsp;<\/p>\n<\/div>\n<\/div>\n<\/div>\n<\/div>\n","protected":false},"excerpt":{"rendered":"<p>&nbsp; while loop exercise &nbsp; &nbsp; Exercise: Write a program to print all even numbers between 0 to 100 (including the boundary numbers).&nbsp; Also, count and print how many even numbers you find.&nbsp; Use &#8216;while&#8217; loop, or &#8216;for&#8217; loop, or &#8216;do while&#8217; loop.&nbsp; Later change the program to accept boundary numbers from the user. &nbsp; [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":12079,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"ocean_post_layout":"","ocean_both_sidebars_style":"","ocean_both_sidebars_content_width":0,"ocean_both_sidebars_sidebars_width":0,"ocean_sidebar":"0","ocean_second_sidebar":"0","ocean_disable_margins":"enable","ocean_add_body_class":"","ocean_shortcode_before_top_bar":"","ocean_shortcode_after_top_bar":"","ocean_shortcode_before_header":"","ocean_shortcode_after_header":"","ocean_has_shortcode":"","ocean_shortcode_after_title":"","ocean_shortcode_before_footer_widgets":"","ocean_shortcode_after_footer_widgets":"","ocean_shortcode_before_footer_bottom":"","ocean_shortcode_after_footer_bottom":"","ocean_display_top_bar":"default","ocean_display_header":"default","ocean_header_style":"","ocean_center_header_left_menu":"0","ocean_custom_header_template":"0","ocean_custom_logo":0,"ocean_custom_retina_logo":0,"ocean_custom_logo_max_width":0,"ocean_custom_logo_tablet_max_width":0,"ocean_custom_logo_mobile_max_width":0,"ocean_custom_logo_max_height":0,"ocean_custom_logo_tablet_max_height":0,"ocean_custom_logo_mobile_max_height":0,"ocean_header_custom_menu":"0","ocean_menu_typo_font_family":"0","ocean_menu_typo_font_subset":"","ocean_menu_typo_font_size":0,"ocean_menu_typo_font_size_tablet":0,"ocean_menu_typo_font_size_mobile":0,"ocean_menu_typo_font_size_unit":"px","ocean_menu_typo_font_weight":"","ocean_menu_typo_font_weight_tablet":"","ocean_menu_typo_font_weight_mobile":"","ocean_menu_typo_transform":"","ocean_menu_typo_transform_tablet":"","ocean_menu_typo_transform_mobile":"","ocean_menu_typo_line_height":0,"ocean_menu_typo_line_height_tablet":0,"ocean_menu_typo_line_height_mobile":0,"ocean_menu_typo_line_height_unit":"","ocean_menu_typo_spacing":0,"ocean_menu_typo_spacing_tablet":0,"ocean_menu_typo_spacing_mobile":0,"ocean_menu_typo_spacing_unit":"","ocean_menu_link_color":"","ocean_menu_link_color_hover":"","ocean_menu_link_color_active":"","ocean_menu_link_background":"","ocean_menu_link_hover_background":"","ocean_menu_link_active_background":"","ocean_menu_social_links_bg":"","ocean_menu_social_hover_links_bg":"","ocean_menu_social_links_color":"","ocean_menu_social_hover_links_color":"","ocean_disable_title":"default","ocean_disable_heading":"default","ocean_post_title":"","ocean_post_subheading":"","ocean_post_title_style":"","ocean_post_title_background_color":"","ocean_post_title_background":0,"ocean_post_title_bg_image_position":"","ocean_post_title_bg_image_attachment":"","ocean_post_title_bg_image_repeat":"","ocean_post_title_bg_image_size":"","ocean_post_title_height":0,"ocean_post_title_bg_overlay":0.5,"ocean_post_title_bg_overlay_color":"","ocean_disable_breadcrumbs":"default","ocean_breadcrumbs_color":"","ocean_breadcrumbs_separator_color":"","ocean_breadcrumbs_links_color":"","ocean_breadcrumbs_links_hover_color":"","ocean_display_footer_widgets":"default","ocean_display_footer_bottom":"default","ocean_custom_footer_template":"0","ocean_post_oembed":"","ocean_post_self_hosted_media":"","ocean_post_video_embed":"","ocean_link_format":"","ocean_link_format_target":"self","ocean_quote_format":"","ocean_quote_format_link":"post","ocean_gallery_link_images":"off","ocean_gallery_id":[],"footnotes":""},"categories":[8],"tags":[16],"class_list":["post-12076","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-blog","tag-microcontroller-embedded-c-programming","entry","has-media"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v27.3 - https:\/\/yoast.com\/product\/yoast-seo-wordpress\/ -->\n<title>C Programming: Using While Loops to Print Even Numbers<\/title>\n<meta name=\"description\" content=\"Learn how to use C programming to print even numbers within a specified range, count them, and handle user input for dynamic boundaries.\" \/>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/fastbitlab.com\/blog\/microcontroller-embedded-c-programming-lecture-121-while-loop-exercise\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"C Programming: Using While Loops to Print Even Numbers\" \/>\n<meta property=\"og:description\" content=\"Learn how to use C programming to print even numbers within a specified range, count them, and handle user input for dynamic boundaries.\" \/>\n<meta property=\"og:url\" content=\"https:\/\/fastbitlab.com\/blog\/microcontroller-embedded-c-programming-lecture-121-while-loop-exercise\/\" \/>\n<meta property=\"og:site_name\" content=\"FastBit EBA\" \/>\n<meta property=\"article:publisher\" content=\"https:\/\/www.facebook.com\/fastbiteba\/\" \/>\n<meta property=\"article:published_time\" content=\"2022-10-03T04:11:46+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2023-09-28T07:16:13+00:00\" \/>\n<meta property=\"og:image\" content=\"https:\/\/fastbitlab.com\/blog\/wp-content\/uploads\/2022\/10\/Figure-1.png\" \/>\n\t<meta property=\"og:image:width\" content=\"1181\" \/>\n\t<meta property=\"og:image:height\" content=\"715\" \/>\n\t<meta property=\"og:image:type\" content=\"image\/png\" \/>\n<meta name=\"author\" content=\"FastBitLab\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:creator\" content=\"@fastbiteba\" \/>\n<meta name=\"twitter:site\" content=\"@fastbiteba\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"FastBitLab\" \/>\n\t<meta name=\"twitter:label2\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"5 minutes\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\\\/\\\/schema.org\",\"@graph\":[{\"@type\":\"Article\",\"@id\":\"https:\\\/\\\/fastbitlab.com\\\/blog\\\/microcontroller-embedded-c-programming-lecture-121-while-loop-exercise\\\/#article\",\"isPartOf\":{\"@id\":\"https:\\\/\\\/fastbitlab.com\\\/blog\\\/microcontroller-embedded-c-programming-lecture-121-while-loop-exercise\\\/\"},\"author\":{\"name\":\"FastBitLab\",\"@id\":\"https:\\\/\\\/fastbitlab.com\\\/blog\\\/#\\\/schema\\\/person\\\/e32b38e733a0d76ffa7e6bc998652e5d\"},\"headline\":\"Microcontroller Embedded C Programming Lecture 121| while loop exercise\",\"datePublished\":\"2022-10-03T04:11:46+00:00\",\"dateModified\":\"2023-09-28T07:16:13+00:00\",\"mainEntityOfPage\":{\"@id\":\"https:\\\/\\\/fastbitlab.com\\\/blog\\\/microcontroller-embedded-c-programming-lecture-121-while-loop-exercise\\\/\"},\"wordCount\":911,\"commentCount\":0,\"publisher\":{\"@id\":\"https:\\\/\\\/fastbitlab.com\\\/blog\\\/#organization\"},\"image\":{\"@id\":\"https:\\\/\\\/fastbitlab.com\\\/blog\\\/microcontroller-embedded-c-programming-lecture-121-while-loop-exercise\\\/#primaryimage\"},\"thumbnailUrl\":\"https:\\\/\\\/fastbitlab.com\\\/blog\\\/wp-content\\\/uploads\\\/2022\\\/10\\\/Figure-1.png\",\"keywords\":[\"Microcontroller Embedded C programming Lectures\"],\"articleSection\":[\"Blog\"],\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"CommentAction\",\"name\":\"Comment\",\"target\":[\"https:\\\/\\\/fastbitlab.com\\\/blog\\\/microcontroller-embedded-c-programming-lecture-121-while-loop-exercise\\\/#respond\"]}]},{\"@type\":\"WebPage\",\"@id\":\"https:\\\/\\\/fastbitlab.com\\\/blog\\\/microcontroller-embedded-c-programming-lecture-121-while-loop-exercise\\\/\",\"url\":\"https:\\\/\\\/fastbitlab.com\\\/blog\\\/microcontroller-embedded-c-programming-lecture-121-while-loop-exercise\\\/\",\"name\":\"C Programming: Using While Loops to Print Even Numbers\",\"isPartOf\":{\"@id\":\"https:\\\/\\\/fastbitlab.com\\\/blog\\\/#website\"},\"primaryImageOfPage\":{\"@id\":\"https:\\\/\\\/fastbitlab.com\\\/blog\\\/microcontroller-embedded-c-programming-lecture-121-while-loop-exercise\\\/#primaryimage\"},\"image\":{\"@id\":\"https:\\\/\\\/fastbitlab.com\\\/blog\\\/microcontroller-embedded-c-programming-lecture-121-while-loop-exercise\\\/#primaryimage\"},\"thumbnailUrl\":\"https:\\\/\\\/fastbitlab.com\\\/blog\\\/wp-content\\\/uploads\\\/2022\\\/10\\\/Figure-1.png\",\"datePublished\":\"2022-10-03T04:11:46+00:00\",\"dateModified\":\"2023-09-28T07:16:13+00:00\",\"description\":\"Learn how to use C programming to print even numbers within a specified range, count them, and handle user input for dynamic boundaries.\",\"breadcrumb\":{\"@id\":\"https:\\\/\\\/fastbitlab.com\\\/blog\\\/microcontroller-embedded-c-programming-lecture-121-while-loop-exercise\\\/#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\\\/\\\/fastbitlab.com\\\/blog\\\/microcontroller-embedded-c-programming-lecture-121-while-loop-exercise\\\/\"]}]},{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\\\/\\\/fastbitlab.com\\\/blog\\\/microcontroller-embedded-c-programming-lecture-121-while-loop-exercise\\\/#primaryimage\",\"url\":\"https:\\\/\\\/fastbitlab.com\\\/blog\\\/wp-content\\\/uploads\\\/2022\\\/10\\\/Figure-1.png\",\"contentUrl\":\"https:\\\/\\\/fastbitlab.com\\\/blog\\\/wp-content\\\/uploads\\\/2022\\\/10\\\/Figure-1.png\",\"width\":1181,\"height\":715,\"caption\":\"Figure 1. Code\"},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\\\/\\\/fastbitlab.com\\\/blog\\\/microcontroller-embedded-c-programming-lecture-121-while-loop-exercise\\\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\\\/\\\/fastbitlab.com\\\/blog\\\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"Microcontroller Embedded C Programming Lecture 121| while loop exercise\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\\\/\\\/fastbitlab.com\\\/blog\\\/#website\",\"url\":\"https:\\\/\\\/fastbitlab.com\\\/blog\\\/\",\"name\":\"FastBit EBA\",\"description\":\"Your Online Academy of Embedded Systems\",\"publisher\":{\"@id\":\"https:\\\/\\\/fastbitlab.com\\\/blog\\\/#organization\"},\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\\\/\\\/fastbitlab.com\\\/blog\\\/?s={search_term_string}\"},\"query-input\":{\"@type\":\"PropertyValueSpecification\",\"valueRequired\":true,\"valueName\":\"search_term_string\"}}],\"inLanguage\":\"en-US\"},{\"@type\":\"Organization\",\"@id\":\"https:\\\/\\\/fastbitlab.com\\\/blog\\\/#organization\",\"name\":\"FastBit EBA\",\"url\":\"https:\\\/\\\/fastbitlab.com\\\/blog\\\/\",\"logo\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\\\/\\\/fastbitlab.com\\\/blog\\\/#\\\/schema\\\/logo\\\/image\\\/\",\"url\":\"https:\\\/\\\/fastbitlab.com\\\/blog\\\/wp-content\\\/uploads\\\/2026\\\/04\\\/logo-EzNrEnyr.png\",\"contentUrl\":\"https:\\\/\\\/fastbitlab.com\\\/blog\\\/wp-content\\\/uploads\\\/2026\\\/04\\\/logo-EzNrEnyr.png\",\"width\":640,\"height\":640,\"caption\":\"FastBit EBA\"},\"image\":{\"@id\":\"https:\\\/\\\/fastbitlab.com\\\/blog\\\/#\\\/schema\\\/logo\\\/image\\\/\"},\"sameAs\":[\"https:\\\/\\\/www.facebook.com\\\/fastbiteba\\\/\",\"https:\\\/\\\/x.com\\\/fastbiteba\",\"https:\\\/\\\/www.linkedin.com\\\/in\\\/fastbit-embedded-brain-academy-b3167b124\\\/\",\"https:\\\/\\\/www.youtube.com\\\/channel\\\/UCa1REBV9hyrzGp2mjJCagBg\"]},{\"@type\":\"Person\",\"@id\":\"https:\\\/\\\/fastbitlab.com\\\/blog\\\/#\\\/schema\\\/person\\\/e32b38e733a0d76ffa7e6bc998652e5d\",\"name\":\"FastBitLab\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\\\/\\\/secure.gravatar.com\\\/avatar\\\/9230d0f9bdef28b63a01e7ca274ee7b2e8ed9abe932ee564af8809caaf52a0c8?s=96&d=mm&r=g\",\"url\":\"https:\\\/\\\/secure.gravatar.com\\\/avatar\\\/9230d0f9bdef28b63a01e7ca274ee7b2e8ed9abe932ee564af8809caaf52a0c8?s=96&d=mm&r=g\",\"contentUrl\":\"https:\\\/\\\/secure.gravatar.com\\\/avatar\\\/9230d0f9bdef28b63a01e7ca274ee7b2e8ed9abe932ee564af8809caaf52a0c8?s=96&d=mm&r=g\",\"caption\":\"FastBitLab\"},\"description\":\"The FastBit Embedded Brain Academy uses the power of internet to bring the online courses related to the field of embedded system programming, Real time operating system, Embedded Linux systems, etc at your finger tip with very low cost. Backed with strong experience of industry, we have produced lots of courses with the customer enrolment over 3000+ across 100+ countries.\"}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"C Programming: Using While Loops to Print Even Numbers","description":"Learn how to use C programming to print even numbers within a specified range, count them, and handle user input for dynamic boundaries.","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/fastbitlab.com\/blog\/microcontroller-embedded-c-programming-lecture-121-while-loop-exercise\/","og_locale":"en_US","og_type":"article","og_title":"C Programming: Using While Loops to Print Even Numbers","og_description":"Learn how to use C programming to print even numbers within a specified range, count them, and handle user input for dynamic boundaries.","og_url":"https:\/\/fastbitlab.com\/blog\/microcontroller-embedded-c-programming-lecture-121-while-loop-exercise\/","og_site_name":"FastBit EBA","article_publisher":"https:\/\/www.facebook.com\/fastbiteba\/","article_published_time":"2022-10-03T04:11:46+00:00","article_modified_time":"2023-09-28T07:16:13+00:00","og_image":[{"width":1181,"height":715,"url":"https:\/\/fastbitlab.com\/blog\/wp-content\/uploads\/2022\/10\/Figure-1.png","type":"image\/png"}],"author":"FastBitLab","twitter_card":"summary_large_image","twitter_creator":"@fastbiteba","twitter_site":"@fastbiteba","twitter_misc":{"Written by":"FastBitLab","Est. reading time":"5 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/fastbitlab.com\/blog\/microcontroller-embedded-c-programming-lecture-121-while-loop-exercise\/#article","isPartOf":{"@id":"https:\/\/fastbitlab.com\/blog\/microcontroller-embedded-c-programming-lecture-121-while-loop-exercise\/"},"author":{"name":"FastBitLab","@id":"https:\/\/fastbitlab.com\/blog\/#\/schema\/person\/e32b38e733a0d76ffa7e6bc998652e5d"},"headline":"Microcontroller Embedded C Programming Lecture 121| while loop exercise","datePublished":"2022-10-03T04:11:46+00:00","dateModified":"2023-09-28T07:16:13+00:00","mainEntityOfPage":{"@id":"https:\/\/fastbitlab.com\/blog\/microcontroller-embedded-c-programming-lecture-121-while-loop-exercise\/"},"wordCount":911,"commentCount":0,"publisher":{"@id":"https:\/\/fastbitlab.com\/blog\/#organization"},"image":{"@id":"https:\/\/fastbitlab.com\/blog\/microcontroller-embedded-c-programming-lecture-121-while-loop-exercise\/#primaryimage"},"thumbnailUrl":"https:\/\/fastbitlab.com\/blog\/wp-content\/uploads\/2022\/10\/Figure-1.png","keywords":["Microcontroller Embedded C programming Lectures"],"articleSection":["Blog"],"inLanguage":"en-US","potentialAction":[{"@type":"CommentAction","name":"Comment","target":["https:\/\/fastbitlab.com\/blog\/microcontroller-embedded-c-programming-lecture-121-while-loop-exercise\/#respond"]}]},{"@type":"WebPage","@id":"https:\/\/fastbitlab.com\/blog\/microcontroller-embedded-c-programming-lecture-121-while-loop-exercise\/","url":"https:\/\/fastbitlab.com\/blog\/microcontroller-embedded-c-programming-lecture-121-while-loop-exercise\/","name":"C Programming: Using While Loops to Print Even Numbers","isPartOf":{"@id":"https:\/\/fastbitlab.com\/blog\/#website"},"primaryImageOfPage":{"@id":"https:\/\/fastbitlab.com\/blog\/microcontroller-embedded-c-programming-lecture-121-while-loop-exercise\/#primaryimage"},"image":{"@id":"https:\/\/fastbitlab.com\/blog\/microcontroller-embedded-c-programming-lecture-121-while-loop-exercise\/#primaryimage"},"thumbnailUrl":"https:\/\/fastbitlab.com\/blog\/wp-content\/uploads\/2022\/10\/Figure-1.png","datePublished":"2022-10-03T04:11:46+00:00","dateModified":"2023-09-28T07:16:13+00:00","description":"Learn how to use C programming to print even numbers within a specified range, count them, and handle user input for dynamic boundaries.","breadcrumb":{"@id":"https:\/\/fastbitlab.com\/blog\/microcontroller-embedded-c-programming-lecture-121-while-loop-exercise\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/fastbitlab.com\/blog\/microcontroller-embedded-c-programming-lecture-121-while-loop-exercise\/"]}]},{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/fastbitlab.com\/blog\/microcontroller-embedded-c-programming-lecture-121-while-loop-exercise\/#primaryimage","url":"https:\/\/fastbitlab.com\/blog\/wp-content\/uploads\/2022\/10\/Figure-1.png","contentUrl":"https:\/\/fastbitlab.com\/blog\/wp-content\/uploads\/2022\/10\/Figure-1.png","width":1181,"height":715,"caption":"Figure 1. Code"},{"@type":"BreadcrumbList","@id":"https:\/\/fastbitlab.com\/blog\/microcontroller-embedded-c-programming-lecture-121-while-loop-exercise\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/fastbitlab.com\/blog\/"},{"@type":"ListItem","position":2,"name":"Microcontroller Embedded C Programming Lecture 121| while loop exercise"}]},{"@type":"WebSite","@id":"https:\/\/fastbitlab.com\/blog\/#website","url":"https:\/\/fastbitlab.com\/blog\/","name":"FastBit EBA","description":"Your Online Academy of Embedded Systems","publisher":{"@id":"https:\/\/fastbitlab.com\/blog\/#organization"},"potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/fastbitlab.com\/blog\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"en-US"},{"@type":"Organization","@id":"https:\/\/fastbitlab.com\/blog\/#organization","name":"FastBit EBA","url":"https:\/\/fastbitlab.com\/blog\/","logo":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/fastbitlab.com\/blog\/#\/schema\/logo\/image\/","url":"https:\/\/fastbitlab.com\/blog\/wp-content\/uploads\/2026\/04\/logo-EzNrEnyr.png","contentUrl":"https:\/\/fastbitlab.com\/blog\/wp-content\/uploads\/2026\/04\/logo-EzNrEnyr.png","width":640,"height":640,"caption":"FastBit EBA"},"image":{"@id":"https:\/\/fastbitlab.com\/blog\/#\/schema\/logo\/image\/"},"sameAs":["https:\/\/www.facebook.com\/fastbiteba\/","https:\/\/x.com\/fastbiteba","https:\/\/www.linkedin.com\/in\/fastbit-embedded-brain-academy-b3167b124\/","https:\/\/www.youtube.com\/channel\/UCa1REBV9hyrzGp2mjJCagBg"]},{"@type":"Person","@id":"https:\/\/fastbitlab.com\/blog\/#\/schema\/person\/e32b38e733a0d76ffa7e6bc998652e5d","name":"FastBitLab","image":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/secure.gravatar.com\/avatar\/9230d0f9bdef28b63a01e7ca274ee7b2e8ed9abe932ee564af8809caaf52a0c8?s=96&d=mm&r=g","url":"https:\/\/secure.gravatar.com\/avatar\/9230d0f9bdef28b63a01e7ca274ee7b2e8ed9abe932ee564af8809caaf52a0c8?s=96&d=mm&r=g","contentUrl":"https:\/\/secure.gravatar.com\/avatar\/9230d0f9bdef28b63a01e7ca274ee7b2e8ed9abe932ee564af8809caaf52a0c8?s=96&d=mm&r=g","caption":"FastBitLab"},"description":"The FastBit Embedded Brain Academy uses the power of internet to bring the online courses related to the field of embedded system programming, Real time operating system, Embedded Linux systems, etc at your finger tip with very low cost. Backed with strong experience of industry, we have produced lots of courses with the customer enrolment over 3000+ across 100+ countries."}]}},"_links":{"self":[{"href":"https:\/\/fastbitlab.com\/blog\/wp-json\/wp\/v2\/posts\/12076","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/fastbitlab.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/fastbitlab.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/fastbitlab.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/fastbitlab.com\/blog\/wp-json\/wp\/v2\/comments?post=12076"}],"version-history":[{"count":5,"href":"https:\/\/fastbitlab.com\/blog\/wp-json\/wp\/v2\/posts\/12076\/revisions"}],"predecessor-version":[{"id":16074,"href":"https:\/\/fastbitlab.com\/blog\/wp-json\/wp\/v2\/posts\/12076\/revisions\/16074"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/fastbitlab.com\/blog\/wp-json\/wp\/v2\/media\/12079"}],"wp:attachment":[{"href":"https:\/\/fastbitlab.com\/blog\/wp-json\/wp\/v2\/media?parent=12076"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/fastbitlab.com\/blog\/wp-json\/wp\/v2\/categories?post=12076"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/fastbitlab.com\/blog\/wp-json\/wp\/v2\/tags?post=12076"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}