fork download
  1. %{
  2. #include <stdio.h>
  3. #include <math.h>
  4. %}
  5.  
  6. DIGIT [0-9]
  7.  
  8. %%
  9.  
  10. {DIGIT}+ {
  11. int num = atoi(yytext); // Convert the matched string to an integer
  12. int originalNum = num;
  13. int numDigits = 0;
  14. int sum = 0;
  15.  
  16. // Count the number of digits
  17. while (num > 0) {
  18. num /= 10;
  19. numDigits++;
  20. }
  21.  
  22. num = originalNum; // Reset num to its original value
  23.  
  24. // Calculate the sum of digits raised to the power of the number of digits
  25. while (num > 0) {
  26. int digit = num % 10;
  27. sum += pow(digit, numDigits);
  28. num /= 10;
  29. }
  30.  
  31. if (sum == originalNum) {
  32. printf("%s is an Armstrong number.\n", yytext);
  33. } else {
  34. printf("%s is not an Armstrong number.\n", yytext);
  35. }
  36. }
  37.  
  38. .|\n ; // Ignore any other characters
  39.  
  40. %%
  41.  
  42. int main() {
  43. yylex();
  44. return 0;
  45. }
  46.  
Success #stdin #stdout #stderr 0.02s 6852KB
stdin
Standard input is empty
stdout
Standard output is empty
stderr
ERROR: /home/ntReiQ/prog:45:1: Syntax error: Unexpected end of file
ERROR: '$runtoplevel'/0: Undefined procedure: program/0
   Exception: (3) program ? EOF: exit