Lex Program to check whether a number is Prime or Not

Problem: Write a Lex Program to check whether a number is Prime or Not.

Lex is a computer program that generates lexical analyzers and was written by Mike Lesk and Eric Schmidt. Lex reads an input stream specifying the lexical analyzer and outputs source code implementing the lexer in the C programming language.

Definition: A prime number is a whole number greater than 1 whose only factors are 1 and itself. A factor is a whole numbers that can be divided evenly into another number. The first few prime numbers are: 2, 3, 5, 7, 11, 13, 17, 19, 23 and 29.


Input: 2 
Output: Prime number

Input: 6
Output: Not a Prime number 






/* Lex Program to check whether a number is Prime or Not */
   /* Definition section */
   int flag,c,j;
/* Rule Section */
[0-9]+ {c=atoi(yytext);
           printf("\n Prime number");
         else if(c==0 || c==1)
           printf("\n Not a Prime number");
           printf("\n Not a prime number");
         else if(flag==0)
           printf("\n Prime number");
// driver code
int main()
  return 0;



GeeksforGeeks has prepared a complete interview preparation course with premium videos, theory, practice problems, TA support and many more features. Please refer Placement 100 for details

My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.

Article Tags :

Be the First to upvote.

Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.