Lex program to check perfect numbers Last Updated : 30 Apr, 2019 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 perfect numbers thakur_aman Follow Improve Article Tags : C Programs Compiler Design Lex program Similar Reads C Program to Check Armstrong Number An Armstrong number is defined as a number that is equal to the sum of the Kth power of each digit in the number, where K is the number of digits in it.Example:Input: 153Output: YesExplanation: 153 is an Armstrong number of 3 digits, since the sum of cubes of each digit is equal to the number itself 4 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 CÂ Program To Check Neon Number Given a number (num) we need to check whether it is a Neon Number ( i.e. a number where the sum of digits of the square of the number is equal to the number ) and return "true" or "false" accordingly. Example: Input: num = 9 Output: true Explanation: square of 9 is 9 * 9 = 81 , sum of digit of squar 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 check valid Mobile Number Problem: Write a Lex Program to check valid Mobile Number. Explanation: FLEX (Fast Lexical Analyzer Generator) is a tool/computer program for generating lexical analyzers (scanners or lexers) written by Vern Paxson in C around 1987. Lex reads an input stream specifying the lexical analyzer and outpu 1 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 t 1 min read 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 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 lex 2 min read Like