Lex program to check whether a given number is even or odd
Given a number n, the task is to check whether the given n is even or odd using Lex program.
Examples:
Input : 10 Output : Even Input : 5 Output : Odd
Prerequisite: FLEX (Fast Lexical Analyzer Generator)
An even number is an integer which is “evenly divisible” by 2. This means that if the integer is divided by 2, it yields no remainder or 0 as remainder. Similarly, an Odd number is an integer which is not “evenly divisible” by 2 and will leaves 1 as remainder.
Below is the implementation of above approach:
/*Lex program to take check whether the given number is even or odd */ %{ #include<stdio.h> int i; %} %% [0-9]+ {i=atoi(yytext); if(i%2==0) printf("Even"); else printf("Odd");} %% int yywrap(){} /* Driver code */int main() { yylex(); return 0; } |
Output:

Recommended Posts:
- Lex program to check whether given number is armstrong number or not
- Lex program to check whether input number is odd or even
- Lex Program to check whether a number is Prime or Not
- Lex program to check valid Mobile Number
- Lex Program to check valid email
- Lex program to check whether given string is Palindrome or Not
- Lex program to check whether the input is digit or not
- Lex program to check if a Date is valid or not
- Lex program to check perfect numbers
- YAAC program to check whether given string is Palindrome or not
- Compiler Design | C program to check syntax of 'for' loop
- Lex Program to count number of words
- Lex program to check whether an year is a leap year or not
- Lex program to count the number of lines, spaces and tabs
- LEX program to count the number of vowels and consonants in a given string
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.



