<\/span><\/h2>\n\n\n\n\/\/ C Program To Check Whether a Number is Prime or Not\n#include <stdio.h>\nint main(){\n int num, i, c = 0;\n \n \/\/ Asking for Input\n printf(\"Enter an Number: \");\n scanf(\"%d\", &num);\n \n \/\/ logic\n for (i = 1; i <= num; i++){\n if (num % i == 0){\n c++;\n }\n }\n if (c == 2){\n printf(\"%d is a Prime Number.\", num);\n }\n else {\n printf(\"%d is not a Prime Number.\", num);\n }\n return 0;\n}\n<\/pre>\n\n\n\nOutput 1<\/strong><\/p>\n\n\n\nEnter an Number: 7\r\n7 is a Prime Number.\r<\/code><\/pre>\n\n\n\nOutput 2<\/strong><\/p>\n\n\n\nEnter an Number: 57\r\n57 is not a Prime Number.\r<\/code><\/pre>\n\n\n\n<\/span>How Does This Program Work ?<\/strong><\/span><\/h2>\n\n\n\n int num, i, c = 0;<\/strong>\r<\/code><\/pre>\n\n\n\nIn this program, we have declared three int data type variables named as num<\/strong>, i<\/strong> and c<\/strong>. Variable c<\/strong> is assigned a value of 0<\/strong>.<\/p>\n\n\n\n \/\/ Asking for Input\r\n printf(\"Enter an Number: \");\r\n scanf(\"%d\", &num);<\/strong>\r<\/code><\/pre>\n\n\n\nThen, the user is asked to enter a number which he\/she wants to check. This number will get stored in the num<\/strong> named variable.<\/p>\n\n\n\n \/\/ logic\r\n for (i = 1; i <= num; i++){\r\n if (num % i == 0){\r\n c++;\r\n }\r\n }<\/strong>\r<\/code><\/pre>\n\n\n\nNow, this program checks for the number of integers, num<\/strong> is completely divisible with. And the value of the number of factors is stored in the c<\/strong> named variable.<\/p>\n\n\n\n if (c == 2){\r\n printf(\"%d is a Prime Number.\", num);\r\n }\r\n else {\r\n printf(\"%d is not a Prime Number.\", num);\r\n }<\/strong>\r<\/code><\/pre>\n\n\n\nIf the num<\/strong> contains exactly 2<\/strong> factors, then the given number is a Prime number<\/strong> otherwise it\u2019s not a Prime number<\/strong>.<\/p>\n\n\n\nSome of the used terms are as follow:<\/p>\n\n\n\n
#include <stdio.h><\/strong> – In the first line we have used #include, it is a preprocessor command that tells the compiler to include the contents of the stdio.h(standard input and output) file in the program. <\/p>\n\n\n\nThe stdio.h <\/strong>is a file which contains input and output functions like scanf()<\/strong> and printf()<\/strong> to take input and display output respectively. <\/p>\n\n\n\nInt main()<\/strong> – Here main() is the function name and int is the return type of this function. The Execution of any Programming written in C language begins with main() function. <\/p>\n\n\n\nscanf()<\/strong> – scanf() function is used to take input from the user. <\/p>\n\n\n\nprintf()<\/strong> – printf() function is used to display and print the string under the quotation to the screen. <\/p>\n\n\n\nfor loop <\/strong>– A loop is used for initializing a block of statements repeatedly until a given condition returns false.<\/p>\n\n\n\nIf. . . else <\/strong>– An if statement can be followed by an optional else statement, which executes when the Boolean expression is false. If Statement executes when the Boolean expression is True. <\/p>\n\n\n\n% <\/strong>– It is known as Modulus Operator and provides remainder after division.<\/p>\n\n\n\n\/\/ <\/strong>– Used for Commenting in C.\u00a0<\/p>\n\n\n\n<\/span>Conclusion<\/strong><\/span><\/h2>\n\n\n\nI hope after going through this post, you understand how to check whether a number is prime or not using C Programming language.<\/p>\n\n\n\n
If you have any doubt regarding the topic, feel free to contact us in the comment section. We will be delighted to help you.<\/p>\n","protected":false},"excerpt":{"rendered":"
In this post, we will learn how to check whether a number is prime or not using C Programming language.<\/p>\n","protected":false},"author":2,"featured_media":4135,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"site-sidebar-layout":"default","site-content-layout":"default","ast-site-content-layout":"","site-content-style":"default","site-sidebar-style":"default","ast-global-header-display":"","ast-banner-title-visibility":"","ast-main-header-display":"","ast-hfb-above-header-display":"","ast-hfb-below-header-display":"","ast-hfb-mobile-header-display":"","site-post-title":"","ast-breadcrumbs-content":"","ast-featured-img":"","footer-sml-layout":"","theme-transparent-header-meta":"default","adv-header-id-meta":"","stick-header-meta":"","header-above-stick-meta":"","header-main-stick-meta":"","header-below-stick-meta":"","astra-migrate-meta-layouts":"default","ast-page-background-enabled":"default","ast-page-background-meta":{"desktop":{"background-color":"","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-gradient":""},"tablet":{"background-color":"","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-gradient":""},"mobile":{"background-color":"","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-gradient":""}},"ast-content-background-meta":{"desktop":{"background-color":"var(--ast-global-color-5)","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-gradient":""},"tablet":{"background-color":"var(--ast-global-color-5)","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-gradient":""},"mobile":{"background-color":"var(--ast-global-color-5)","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-gradient":""}},"_jetpack_memberships_contains_paid_content":false,"footnotes":""},"categories":[167,294],"tags":[222,286,285,206],"jetpack_sharing_enabled":true,"jetpack_featured_media_url":"https:\/\/www.codingbroz.com\/wp-content\/uploads\/2021\/09\/c-program-to-check-prime-number-codingbroz.png","jetpack-related-posts":[],"_links":{"self":[{"href":"https:\/\/www.codingbroz.com\/wp-json\/wp\/v2\/posts\/4133"}],"collection":[{"href":"https:\/\/www.codingbroz.com\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.codingbroz.com\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.codingbroz.com\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/www.codingbroz.com\/wp-json\/wp\/v2\/comments?post=4133"}],"version-history":[{"count":2,"href":"https:\/\/www.codingbroz.com\/wp-json\/wp\/v2\/posts\/4133\/revisions"}],"predecessor-version":[{"id":4136,"href":"https:\/\/www.codingbroz.com\/wp-json\/wp\/v2\/posts\/4133\/revisions\/4136"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.codingbroz.com\/wp-json\/wp\/v2\/media\/4135"}],"wp:attachment":[{"href":"https:\/\/www.codingbroz.com\/wp-json\/wp\/v2\/media?parent=4133"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.codingbroz.com\/wp-json\/wp\/v2\/categories?post=4133"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.codingbroz.com\/wp-json\/wp\/v2\/tags?post=4133"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}