Showing posts with label Baseconversion. Show all posts
Showing posts with label Baseconversion. Show all posts

Convert Decimal Number to Hexadecimal Number in a File - Lex Program - Compiler Design

Program:




// lex file: conv.l

%{
    #include<stdio.h>
    int num,r,digit=0,count,pcount=0,i;
    char a[20];
%}
DIGIT [0-9]

%%

{DIGIT}+ {    num=atoi(yytext);
        while(num!=0)
        {
            r=num%16;
            digit='0'+r;
            if(digit>'9')
            digit+=7;
            a[count++]=digit;
            num=num/16;
        }
        for(i=count-1;i>=pcount;--i)
                printf("%c",a[i]);
                pcount=count;
        }
.|\n    ECHO;
       
%%
main()
{
    yylex();
    return 0;
}      

Output:
nn@linuxmint ~ $ lex conv.l
nn@linuxmint ~ $ gcc lex.yy.c -lfl
nn@linuxmint ~ $ ./a.out<c.txt
DEEPAK A
F HUNAIF
NAVITHA 10
15 RAJINA
ABID 18
1A SANITHA
nn@linuxmint ~ $


// c.txt

DEEPAK 10
15 HUNAIF
NAVITHA 16
21 RAJINA
ABID 24
26 SANITHA

Base Conversion- Any Base to Any Base -C Program

Program:
#include<stdio.h>
#include<string.h>
void baseconversion(char s[20],int,int);
main()
{   
    char s[20];
    int base1,base2;
    printf("Enter the number and base:");
    scanf("%s%d",s,&base1);
    printf("Enter the base to be converted:");
    scanf("%d",&base2);
    baseconversion(s,base1,base2);
}

void baseconversion(char s[20],int b1,int b2)
{
    int count=0,r,digit,i,n=0,b=1;
    for(i=strlen(s)-1;i>=0;i--)
        {
         if(s[i]>='A'&&s[i]<='Z')
            {
             digit=s[i]-'0'-7;
            }
         else
            {
             digit=s[i]-'0';
            }
        n=digit*b+n;
        b=b*b1;
        }
    while(n!=0)
    {
        r=n%b2;
        digit='0'+r;
        if(digit>'9')
        {
            digit+=7;
        }
         s[count]=digit;
         count++;
         n=n/b2;
    }
for(i=count-1;i>=0;i--)
    {
     printf("%c",s[i]);
    }
 printf("\n");

}

Output:
nn@linuxmint ~ $ gcc anybse.c
nn@linuxmint ~ $ ./a.out
Enter the number and base:10
10
Enter the base to be converted:2
1010
nn@linuxmint ~ $ ./a.out
Enter the number and base:1010
2
Enter the base to be converted:10
10
nn@linuxmint ~ $

Base Conversion- Number (Decimal) to Any Base -C Program

Program:
//  #include<conio.h>
 #include<stdio.h>
  void main()
  {
      int b,n,i,r,digit,p,count=0;
      char a[100];// clrscr();
      printf("\nEnter the decimal number:\n");
     scanf("%d",&n);
      printf("\nEnter the base to be converted:\n");
    scanf("%d",&b);
      p=n;
      do
    {
        r=p%b;
        digit='0'+r;
        if(digit>'9')
            digit=digit+7;
        a[count]=digit;
        count++;
        p=p/b;
         } while(p!=0);
      printf("\nbase %d equivalent of num %d is ",b,n);
      for(i=count-1;i>=0;--i)
        printf("%c",a[i]);
    printf(".\n");
//  getch();
  }

Output:
nn@linuxmint ~ $ gcc c16.c
nn@linuxmint ~ $ ./a.out

Enter the decimal number:
10

Enter the base to be converted:
2

base 2 equivalent of num 10 is 1010.
nn@linuxmint ~ $ ./a.out

Enter the decimal number:
10

Enter the base to be converted:
8

base 8 equivalent of num 10 is 12.
nn@linuxmint ~ $ ./a.out

Enter the decimal number:
15

Enter the base to be converted:
16

base 16 equivalent of num 15 is F.
nn@linuxmint ~ $
Related Posts Plugin for WordPress, Blogger...