Program For Sum And Product For Hexadecimal Number-------------
#include<stdio.h>
#include<iostream.h>
#include<ctype.h>
#include<string.h>
#include<conio.h>
typedef struct hex_number
{
int data[15];
int n;
}hexnumber;
void leftshift(int count, hexnumber temp)
{
temp.n=temp.n+count;
for(;count>0;count--)
temp.data[temp.n+count-1]=0;
}
void read(hexnumber temp)
{
char a[10];
int i;
gets(a);
for(i=0;a[i]!='\0';i++)
{
if(isdigit(a[i]))
{
temp.data[i]=a[i] & 0x0F;
}
else
temp.data[i]=a[i] & 0x0F + 9;
}
temp.n=i;
}
void print(hexnumber temp)
{
int i;
for(i=0;i< temp.n;i++)
{
if(temp.data[i] <= 9)
printf("%d", temp.data[i]);
else
printf("%c", (char)(temp.data[i]+55));
}
}
hexnumber add(hexnumber a, hexnumber b)
{
hexnumber c;
int i, j, k, A,B, carry=0;
i=a.n -1;
j=b.n -1;
if(a.n > b.n)
k=a.n;
else
k=b.n;
c.n =k+1;
for(;i>=0 || j>=0 ;i--, j--, k-- )
{
A=B=0;
if(i>=0)
A =a.data[i];
if(j>=0)
B =a.data[j];
c.data[k]=(carry + A +B)%16;
carry = (carry + A +B)/16;
}
c.data[k]=carry;
return(c);
}
hexnumber multiply(hexnumber x, hexnumber y)
{
hexnumber temp, z;
int n;
int count=0, product, carry=0, i, j, digit;
for(i=y.n-1;j>=0;j--)
{
digit= y.data[i];
for(j=x.n-1;j>=0;j--)
{
product =digit * x.data[j]+carry;
temp.data[j+1]=product%16;
carry=product/16;
}
temp.data[j+1]=carry;
temp.n=x.n +1;
leftshift(count, temp);
count++;
z=add(z, temp);
}
return z;
}
void main()
{
hexnumber a,b,c;
clrscr();
printf("\n Enter first hex numbers :");
read(a);
printf("\n Enter second hex numbers :");
read(b);
c= add(a,b);
printf("\n sum=");
print(c);
printf("\n Product =");
c=multiply(a,b);
print(c);
getch();
}
#include<stdio.h>
#include<iostream.h>
#include<ctype.h>
#include<string.h>
#include<conio.h>
typedef struct hex_number
{
int data[15];
int n;
}hexnumber;
void leftshift(int count, hexnumber temp)
{
temp.n=temp.n+count;
for(;count>0;count--)
temp.data[temp.n+count-1]=0;
}
void read(hexnumber temp)
{
char a[10];
int i;
gets(a);
for(i=0;a[i]!='\0';i++)
{
if(isdigit(a[i]))
{
temp.data[i]=a[i] & 0x0F;
}
else
temp.data[i]=a[i] & 0x0F + 9;
}
temp.n=i;
}
void print(hexnumber temp)
{
int i;
for(i=0;i< temp.n;i++)
{
if(temp.data[i] <= 9)
printf("%d", temp.data[i]);
else
printf("%c", (char)(temp.data[i]+55));
}
}
hexnumber add(hexnumber a, hexnumber b)
{
hexnumber c;
int i, j, k, A,B, carry=0;
i=a.n -1;
j=b.n -1;
if(a.n > b.n)
k=a.n;
else
k=b.n;
c.n =k+1;
for(;i>=0 || j>=0 ;i--, j--, k-- )
{
A=B=0;
if(i>=0)
A =a.data[i];
if(j>=0)
B =a.data[j];
c.data[k]=(carry + A +B)%16;
carry = (carry + A +B)/16;
}
c.data[k]=carry;
return(c);
}
hexnumber multiply(hexnumber x, hexnumber y)
{
hexnumber temp, z;
int n;
int count=0, product, carry=0, i, j, digit;
for(i=y.n-1;j>=0;j--)
{
digit= y.data[i];
for(j=x.n-1;j>=0;j--)
{
product =digit * x.data[j]+carry;
temp.data[j+1]=product%16;
carry=product/16;
}
temp.data[j+1]=carry;
temp.n=x.n +1;
leftshift(count, temp);
count++;
z=add(z, temp);
}
return z;
}
void main()
{
hexnumber a,b,c;
clrscr();
printf("\n Enter first hex numbers :");
read(a);
printf("\n Enter second hex numbers :");
read(b);
c= add(a,b);
printf("\n sum=");
print(c);
printf("\n Product =");
c=multiply(a,b);
print(c);
getch();
}
No comments:
Post a Comment