Lex program to check perfect numbers Last Updated : 11 Jul, 2025 Comments Improve Suggest changes Like Article Like Report 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: Perfect number, a positive integer that is equal to the sum of its proper divisors, for example: 6 = 1+2+3. Examples: Input: 3 Output: 3 is not perfect number Input: 6 Output: 6 is perfect number Implementation: CPP /*Lex program to check perfect numbers*/ % { #include <string.h> void check(char*); % } /*Rule Section*/ % % [0 - 9] + check(yytext); % % int main() { // the input stream pointer extern FILE* yyin; // open a file handle to a particular file yyin = fopen("num", "r"); // The function that starts the analysis yylex(); return 0; } void check(char* a) { int len = strlen(a), i, num = 0; for (i = 0; i < len; i++) num = num * 10 + (a[i] - '0'); int x = 0, temp = num; for (i = 1; i < num; i++) { if (num % i == 0) x = x + i; } if (x == temp) printf("%d is perfect number \n", num); else printf("%d is not perfect number \n", num); } Output: Comment More infoAdvertise with us Next Article Lex program to check whether input number is odd or even T thakur_aman Follow Improve Article Tags : C Programs Compiler Design Lex program Similar Reads Lex program to check whether input number is odd or even 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.ou 1 min read Lex program to check whether input number is odd or even 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.ou 1 min read C Program to Print Armstrong Numbers Between 1 to 1000 Armstrong numbers are those numbers in which the sum of digits raised to the power of a number of digits in that number will be equal to the number itself. Here will see how to build a C Program to Display Armstrong numbers between 1 to 1000. Example: 153 13 + 53 + 33 1 + 125 + 27 = 153Approach 1:Co 3 min read Check whether the number can be made perfect square after adding 1 Given an integer N, the task is to check whether N the given number can be made a perfect square after adding 1 to it. Examples: Input: 3 Output: Yes 3 + 1 = 4 which is a perfect square i.e. 22 Input: 5 Output: No 5 + 1 = 6 which is not a perfect square. Approach: Check whether n + 1 is a perfect sq 4 min read Lex Program to accept a valid integer and float value 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 -efl ./a.ou 1 min read Lex Program to accept a valid integer and float value 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 -efl ./a.ou 1 min read Like