Open In App

Lex program to check whether input number is odd or even

Last Updated : 30 Apr, 2019
Improve
Improve
Like Article
Like
Save
Share
Report

Lex is a computer program that generates lexical analyzers. Lex reads an input stream specifying the lexical analyzer and outputs source code implementing the lexer in the C programming language.

The commands for executing the lex program are:

lex abc.l (abc is the file name)
gcc lex.yy.c -ll
./a.out

Let’s see lex program to check whether input number is odd or even.

Examples:

Input : 
22
Output :
Input Number is Even

Input :
53
Output :
Input Number is odd

Below is the implementation:




/% Lex Program to check whether 
      - input number is odd or even. %/
  
% {
   int i;
% }
  
%%
[0-9]+ {i = atoi(yytext); 
  
if(i%2==0) 
 printf("Input Number is Even"); 
  
else 
 printf("Input Number is Odd");
};
  
%%
 int main()
  {
    yylex();
    return 1;
  }


Output:


Similar Reads

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 rema
1 min read
Lex program to check whether the input is digit or not
Lex is a computer program that generates lexical analyzers. Lex reads an input stream specifying the lexical analyzer and outputs source code implementing the lexer in the C programming language. Prerequisite: Flex (Fast lexical Analyzer Generator). Given an input, the task is to check if the input is digit or not. Examples: Input: 28 Output: digit
1 min read
Lex program to check whether given number is armstrong number or not
Problem: Lex program to check whether given number is armstrong number or not. Explanation: 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. Description: An
2 min read
DFA in LEX code which accepts Odd number of 0’s and even number of 1’s
Prerequisite: Designing Finite Automata Problem: Design a LEX code to construct a DFA which accepts the language: all strings with Odd number of 0’s and even number of 1’s over inputs '0' and '1'. Examples: Input: 10001 Output: Accepted Input: 10011 Output: Not Accepted Input: asdf Output: Invalid Approach: LEX provides us with an INITIAL state by
2 min read
DFA in LEX code which accepts even number of zeros and even number of ones
Lex is a computer program that generates lexical analyzers, which is commonly used with the YACC parser generator. Lex, originally written by Mike Lesk and Eric Schmidt and described in 1975, is the standard lexical analyzer generator on many Unix systems, and an equivalent tool is specified as part of the POSIX standard. Lex reads an input stream
6 min read
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. Explanation: 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 num
1 min read
LEX Code that accepts string having even number's of 'a' over input alphabet {a,b}
Prerequisite : Designing Finite Automata Introduction :In this article, we will discuss the DFA in LEX Code that accepts the string having even number's of 'a' over input alphabet {a, b}. with the help of example. Let’s discuss it one y one. Problem Overview :LEX Code that accepts the string having even number's of 'a' over input alphabet {a, b}. E
2 min read
Lex program to check whether an year is a leap year or not
Problem: Write a Lex program to check whether an year is a leap year or not. Explanation: 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. Note: Leap years a
1 min read
Lex program to check whether given string is Palindrome or Not
Problem: Write a Lex program to check whether given string is Palindrome or Not. Explanation: 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. Description: A
2 min read
Lex Program to print the total characters, white spaces, tabs in the given input file
Lex is a computer program that generates lexical analyzers. Lex reads an input stream specifying the lexical analyzer and outputs source code implementing the lexer in the C programming language. The commands for executing the lex program are: lex abc.l (abc is the file name) cc lex.yy.c -lfl ./a.out Let’s see Lex program to print the total charact
1 min read