Tuesday 1 April 2014

1.To check whether the given number in the file is armstrong or not

     Armstrong numbers are numbers whose sum of digits each raised to the power of number of digit is equal to the initial number.
    for example 351=(3*3*3)+(5*5*5)+(1*1*1)=27+125+1=153
                       370=(3*3*3)+(7*7*7)+(0*0*0)=27+343+0=370
                       1=1
All single digit numbers are armstrong and no two digit armstrong numbers are there.

Here is the lex program that takes  input from a file named "num"


%{
#include<string.h>
#include<math.h>
void check(char *);
%}
%%
[0-9]+ check(yytext);
%%
main()
{
    extern FILE *yyin;
    yyin=fopen("num","r");
    yylex();
}
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;
    while(num>0)
    {
        x=x+pow(num%10,len);
        num=num/10;
    }
    if(x==temp)
        printf("%d is armstrong \n",temp);
    else
        printf("%d is not armstrong \n",temp);
}

No comments:

Post a Comment