{"id":4780,"date":"2023-03-22T15:53:04","date_gmt":"2023-03-22T15:53:04","guid":{"rendered":"https:\/\/codethataint.com\/blog\/?p=4780"},"modified":"2023-03-22T16:39:14","modified_gmt":"2023-03-22T16:39:14","slug":"for-loop","status":"publish","type":"post","link":"https:\/\/codethataint.com\/blog\/for-loop\/","title":{"rendered":"For Loop"},"content":{"rendered":"<p><strong>For Loop Syntax<\/strong><\/p>\n<pre class=\"brush: java; title: ; notranslate\" title=\"\">\r\nfor(Initialization; Condition; Updation)\r\n{\r\n  Loop Work\r\n}\r\n\r\n<\/pre>\n<p><strong>Print Odd numbers in For loop<\/strong><\/p>\n<pre class=\"brush: java; title: ; notranslate\" title=\"\">\r\nimport java.util.Scanner;\r\n\r\npublic class Main {\r\n    public static void main(String&#x5B;] args) {\r\n        Scanner scn = new Scanner(System.in);\r\n        int number = scn.nextInt();\r\n        int count = 1;\r\n        \r\n        for(int i=1;i&lt;number;i=i+2){\r\n          System.out.print(i + &quot; &quot;);                          \r\n        }\r\n        \r\n    }\r\n}\r\n<\/pre>\n<p><strong>Input<\/strong><\/p>\n<pre>10<\/pre>\n<p><strong>Output<\/strong><\/p>\n<pre>1 3 5 7 9<\/pre>\n<p><strong>Factors<\/strong><br \/>\nAn Integer  X is a factor of N if X divides N with no reminder<\/p>\n<ol>\n<li>2 is factor of 4<\/li>\n<li> 4 is factor of 24<\/li>\n<\/ol>\n<p>   Factors of 10 are 1,2,5,10 leaving reminder 0<br \/>\n   Factors of 24 are 1,2,3,4,6,8,12,24 leaving reminder 0<\/p>\n<p>   Minimum factor is the least factor which would be always 1<br \/>\n   Maximum factor would be always N<\/p>\n<p>   All Factors lie between [1 to N] <\/p>\n<p><strong>Print Factors of a number<\/strong><\/p>\n<pre class=\"brush: java; title: ; notranslate\" title=\"\">\r\nimport java.util.Scanner;\r\n\r\npublic class Main {\r\n    public static void main(String&#x5B;] args) {\r\n        Scanner scn = new Scanner(System.in);\r\n        int number = scn.nextInt();\r\n        int count = 1;\r\n        \r\n        for(int i=1;i&lt;=number;i++){\r\n          if(number%i == 0){\r\n             System.out.println(i);   \r\n          }\r\n        }\r\n        \r\n    }\r\n}\r\n<\/pre>\n<p><strong>Input<\/strong><\/p>\n<pre>24<\/pre>\n<p><strong>Output<\/strong><\/p>\n<pre>1 2 3 4 6 8 12 24 <\/pre>\n<p><strong>Check the number is prime number<\/strong><\/p>\n<pre class=\"brush: java; title: ; notranslate\" title=\"\">\r\nimport java.util.Scanner;\r\n\r\npublic class Main {\r\n    public static void main(String&#x5B;] args) {\r\n        Scanner scn = new Scanner(System.in);\r\n        int number = scn.nextInt();\r\n        int factors = 0;\r\n        \r\n        for(int i=1;i&lt;=number;i++){\r\n          if(number%i == 0){\r\n             factors++;   \r\n          }\r\n        }\r\n\r\n     if(factors == 2){\r\n       System.out.println(&quot;Prime No&quot;);\r\n     }\r\n        \r\n    }\r\n}\r\n<\/pre>\n<p><strong>Input<\/strong><\/p>\n<pre>13<\/pre>\n<p><strong>Output<\/strong><\/p>\n<pre>Prime No<\/pre>\n<p>Now in the above code I am checking the factors is equals to 2 and deciding whether the input is<br \/>\nprime no or not<\/p>\n<p>Imagine you are giving 12 as input for above code<\/p>\n<p>Input :- 12<\/p>\n<pre>\r\n\r\n         i               factor\r\n\t 1                  1\r\n\t 2                  2\r\n\t 3                  3-----> You can break here as we already know factor is greater than 2\r\n\t 4                  4\r\n\t 5                  4\r\n\t 6                  5\r\n\t 7                  5\r\n\t 8                  5\r\n\t 9                  5\r\n\t 10                 5\r\n\t 11                 5\r\n\t 12                 6\r\n<\/pre>\n<p>Refactoring the above for loop by adding break conditon<\/p>\n<pre>\r\n.\r\n.\r\n for(int i=1;i<=number;i++){\r\n     if(number%i == 0){\r\n         factors++;   \r\n      }\r\n  }\r\n\r\n  if(factors > 2){ \/\/Break Factor \r\n     break;\r\n  }\r\n.\r\n.\r\n<\/pre>\n<p><strong>Using break factor in for loop<\/strong><\/p>\n<pre class=\"brush: java; title: ; notranslate\" title=\"\">\r\npublic class CheckPrimen {\r\n    public static void main(String&#x5B;] args) {\r\n        Scanner scn = new Scanner(System.in);\r\n        int number = scn.nextInt();\r\n        int factors = 0;\r\n\r\n        for (int i = 1; i &lt;= number; i++) {\r\n            if (number % i == 0) {\r\n                factors++;\r\n            }\r\n\r\n            if(factors &gt; 2){ \/\/Break Factor \r\n                break;\r\n            }\r\n        }\r\n\r\n        if(factors == 2){\r\n            System.out.println(&quot;Prime No&quot;);\r\n        }else{\r\n            System.out.println(&quot;Not a Prime No&quot;);\r\n        }\r\n    }\r\n}\r\n<\/pre>\n<p><strong>Input<\/strong><\/p>\n<pre><\/pre>\n<p><strong>Output<\/strong><\/p>\n<pre><\/pre>\n<p><strong>Print Odd numbers in For loop<\/strong><\/p>\n<pre class=\"brush: java; title: ; notranslate\" title=\"\">\r\n<\/pre>\n<p><strong>Input<\/strong><\/p>\n<pre><\/pre>\n<p><strong>Output<\/strong><\/p>\n<pre><\/pre>\n","protected":false},"excerpt":{"rendered":"<p>For Loop Syntax for(Initialization; Condition; Updation) { Loop Work } Print Odd numbers in For loop import java.util.Scanner; public class Main { public static void main(String&#x5B;] args) { Scanner scn = new Scanner(System.in); int number = scn.nextInt(); int count = 1; for(int i=1;i&lt;number;i=i+2){ System.out.print(i + &quot; &quot;); } } } Input 10 Output 1 3&hellip; <a href=\"https:\/\/codethataint.com\/blog\/for-loop\/\">Continue reading <span class=\"meta-nav\">&rarr;<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[345],"tags":[],"class_list":["post-4780","post","type-post","status-publish","format-standard","hentry","category-conditional-statements"],"_links":{"self":[{"href":"https:\/\/codethataint.com\/blog\/wp-json\/wp\/v2\/posts\/4780","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/codethataint.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/codethataint.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/codethataint.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/codethataint.com\/blog\/wp-json\/wp\/v2\/comments?post=4780"}],"version-history":[{"count":6,"href":"https:\/\/codethataint.com\/blog\/wp-json\/wp\/v2\/posts\/4780\/revisions"}],"predecessor-version":[{"id":4790,"href":"https:\/\/codethataint.com\/blog\/wp-json\/wp\/v2\/posts\/4780\/revisions\/4790"}],"wp:attachment":[{"href":"https:\/\/codethataint.com\/blog\/wp-json\/wp\/v2\/media?parent=4780"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/codethataint.com\/blog\/wp-json\/wp\/v2\/categories?post=4780"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/codethataint.com\/blog\/wp-json\/wp\/v2\/tags?post=4780"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}