{"id":17666,"date":"2022-06-08T11:12:59","date_gmt":"2022-06-08T11:12:59","guid":{"rendered":"https:\/\/linuxways.net\/?p=17666"},"modified":"2022-06-08T11:12:59","modified_gmt":"2022-06-08T11:12:59","slug":"how-to-check-if-a-number-is-prime-in-python","status":"publish","type":"post","link":"https:\/\/linuxways.net\/de\/scripting\/how-to-check-if-a-number-is-prime-in-python\/","title":{"rendered":"How to Check if a Number is Prime in Python"},"content":{"rendered":"<p>A prime number is a number that is not divisible by any other number except one so in this article we will teach you multiple ways how you can check if the number is prime or not in python.<\/p>\n<ol>\n<li>By user defined function<\/li>\n<li>By using a flag variable<\/li>\n<li>By using while loop<\/li>\n<\/ol>\n<h2>1. By User Defined Function<\/h2>\n<p>In python we can define a function using def keyword which is a good choice to define a function to determine whether the number is a prime number or no as defining function increases the usability of code<\/p>\n<pre>def checkprime(x):\r\n\r\nif x &gt; 1:\r\n\r\nfor y in range(2, int(x\/2) + 1):\r\n\r\nif (x % y) == 0:\r\n\r\nprint(x, \"is not a prime number\")\r\n\r\nbreak\r\n\r\nelse:\r\n\r\nprint(x, \"is a prime number\")\r\n\r\nelse:\r\n\r\nprint(x, \"is not a prime number\")\r\n\r\na= int(input(\"Enter a number = \"))\r\n\r\ncheckprime(a)<\/pre>\n<p><img loading=\"lazy\" decoding=\"async\" width=\"424\" height=\"271\" class=\"wp-image-17667\" src=\"http:\/\/linuxways.net\/wp-content\/uploads\/2022\/06\/text-description-automatically-generated-14.png\" alt=\"Text Description automatically generated\" srcset=\"https:\/\/linuxways.net\/wp-content\/uploads\/2022\/06\/text-description-automatically-generated-14.png 424w, https:\/\/linuxways.net\/wp-content\/uploads\/2022\/06\/text-description-automatically-generated-14-300x192.png 300w\" sizes=\"auto, (max-width: 424px) 100vw, 424px\" \/><\/p>\n<p>Figure 1: Using nested if else<\/p>\n<p><strong>Output:<\/strong><\/p>\n<p><strong><img loading=\"lazy\" decoding=\"async\" width=\"215\" height=\"57\" class=\"wp-image-17668\" src=\"http:\/\/linuxways.net\/wp-content\/uploads\/2022\/06\/text-description-automatically-generated-15.png\" alt=\"Text Description automatically generated\" \/><\/strong><\/p>\n<p>Figure 2: Output<\/p>\n<p>In the figure 1, first we have declared a function checkprime(a) that takes a number as parameter and checks whether the given number is bigger than one. If the number is greater than 1, for loop will be executed to check if \u2018x\u2019 is fully divisible by \u2018y\u2019 then \u2018x\u2019 is not a prime number otherwise, the \u2018x\u2019 is a prime number. If the number is not larger than 1, it will go straight to the else part and print \u2019x\u2019 is not a prime number as shown in figure 2.<\/p>\n<h2>2. By using a flag variable<\/h2>\n<p>In Python, we can verify if a number is prime or not by using a flag variable. Flag variable can have two values: true or false.<\/p>\n<pre>x= int(input(\"Enter a number = \"))\r\n\r\nflag = False\r\n\r\nif x &gt; 1:\r\n\r\nfor y in range(2, x):\r\n\r\nif (x % y) == 0:\r\n\r\nflag = True\r\n\r\nbreak\r\n\r\nif flag:\r\n\r\nprint(x, \"is not a prime number\")\r\n\r\nelse:\r\n\r\nprint(x, \"is a prime number\")<\/pre>\n<p><img loading=\"lazy\" decoding=\"async\" width=\"335\" height=\"264\" class=\"wp-image-17669\" src=\"http:\/\/linuxways.net\/wp-content\/uploads\/2022\/06\/text-description-automatically-generated-16.png\" alt=\"Text Description automatically generated\" srcset=\"https:\/\/linuxways.net\/wp-content\/uploads\/2022\/06\/text-description-automatically-generated-16.png 335w, https:\/\/linuxways.net\/wp-content\/uploads\/2022\/06\/text-description-automatically-generated-16-300x236.png 300w\" sizes=\"auto, (max-width: 335px) 100vw, 335px\" \/><\/p>\n<p>Figure 3: Using flag variable<\/p>\n<p><strong>Output:<\/strong><\/p>\n<p><strong><img loading=\"lazy\" decoding=\"async\" width=\"249\" height=\"61\" class=\"wp-image-17670\" src=\"http:\/\/linuxways.net\/wp-content\/uploads\/2022\/06\/text-description-automatically-generated-17.png\" alt=\"Text Description automatically generated\" \/><\/strong><\/p>\n<p>Figure 4: Output<\/p>\n<p>In the above example, User enters a number and we check if entered number \u2018x\u2019 is more than one or not, so we only proceed when \u2018x\u2019 is more than one. If &#8216;x&#8217; is divisible by any number in the range (2, x), set flag to True and exit the loop if a factor in that range is found, indicating that the integer is not prime, as illustrated in figure 2. Otherwise, the value of the flag will stay False, indicating that the number is prime.<\/p>\n<h2>3. By using while loop<\/h2>\n<p><strong>Prime number can be checked using while loop.<\/strong><\/p>\n<pre>x= int(input(\"Enter a number greater than 1= \"))\r\n\r\nf=0\r\n\r\ny=2\r\n\r\nwhile y &lt;= x \/ 2:\r\n\r\nif (x % y) == 0:\r\n\r\nf = 1\r\n\r\nbreak\r\n\r\ny+=1\r\n\r\nif f:\r\n\r\nprint(x, \"is not a prime number\")\r\n\r\nelse:\r\n\r\nprint(x, \"is a prime number\")\r\n\r\n<img loading=\"lazy\" decoding=\"async\" width=\"408\" height=\"272\" class=\"wp-image-17671\" src=\"http:\/\/linuxways.net\/wp-content\/uploads\/2022\/06\/text-description-automatically-generated-18.png\" alt=\"Text Description automatically generated\" srcset=\"https:\/\/linuxways.net\/wp-content\/uploads\/2022\/06\/text-description-automatically-generated-18.png 408w, https:\/\/linuxways.net\/wp-content\/uploads\/2022\/06\/text-description-automatically-generated-18-300x200.png 300w\" sizes=\"auto, (max-width: 408px) 100vw, 408px\" \/><\/pre>\n<p>Figure 5: Using while loop<\/p>\n<p><strong>Output:<\/strong><\/p>\n<p><strong><img loading=\"lazy\" decoding=\"async\" width=\"291\" height=\"63\" class=\"wp-image-17672\" src=\"http:\/\/linuxways.net\/wp-content\/uploads\/2022\/06\/text-description-automatically-generated-19.png\" alt=\"Text Description automatically generated\" \/><\/strong><\/p>\n<p>Figure 6: Output<\/p>\n<p>In the above example user is asked to enter a number \u2018x\u2019 greater than 1. The while loop condition is checked if it\u2019s true then it will be executed. Firstly, remainder will be calculated to see if \u2018x\u2019 is fully divisible by any other number than itself. If \u2018x\u2019 is fully divisible then value of variable \u2018f\u2019 will be updated otherwise it will not be updated.<\/p>","protected":false},"excerpt":{"rendered":"<p>A prime number is a number that is not divisible by any other number except one so in this article we will teach you multiple ways how you&hellip;<\/p>","protected":false},"author":1,"featured_media":17675,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[168],"tags":[10],"class_list":["post-17666","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-scripting","tag-python"],"_links":{"self":[{"href":"https:\/\/linuxways.net\/de\/wp-json\/wp\/v2\/posts\/17666","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/linuxways.net\/de\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/linuxways.net\/de\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/linuxways.net\/de\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/linuxways.net\/de\/wp-json\/wp\/v2\/comments?post=17666"}],"version-history":[{"count":0,"href":"https:\/\/linuxways.net\/de\/wp-json\/wp\/v2\/posts\/17666\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/linuxways.net\/de\/wp-json\/wp\/v2\/media\/17675"}],"wp:attachment":[{"href":"https:\/\/linuxways.net\/de\/wp-json\/wp\/v2\/media?parent=17666"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/linuxways.net\/de\/wp-json\/wp\/v2\/categories?post=17666"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/linuxways.net\/de\/wp-json\/wp\/v2\/tags?post=17666"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}