Hexadecimal calculator

/*
* Title: Implimentation of addition & multiplication of Hexadecimal number
* Name: Akshay Thakare
* Roll No: 6790
*/

#include
#include
#include

int toint(char a[])
{
	int i,x=0,b=1;
	for(i=strlen(a)-1;i>=0;i--)
	{
		if(isdigit(a[i]))
			x=x+(a[i]-48)*b;
		else if(isalpha(a[i]))
			x=x+(a[i]-55)*b;
		b=b*16;
	}
	return x;
}

void tohex(int x)
{
	int i=0,a;
	char c[10];
	while(x!=0)
	{
		a=x%16;
		x=x/16;
		if(a=0;i--)
		printf("%c",c[i]);
}

int main()
{
	char a[10],b[10];
	int i,x;
	printf("Enter 1st Hex no.:\n");
	scanf("%s",a);
	printf("Enter 2nd Hex no.:\n");
	scanf("%s",b);
	x=toint(a)+toint(b);
	printf("Addition=  ");
	tohex(x);
	printf("\nMultiplication=  ");
	x=toint(a)*toint(b);
	tohex(x);
}

/*
OUTPUT 1
Enter 1st Hex no.:
1F
Enter 2nd Hex no.:
2
Addition=  21
Multiplication=  3E

OUTPUT 2
Enter 1st Hex no.:
ABC 
Enter 2nd Hex no.:
ABC
Addition=  1578
Multiplication=  733A10 

Output 4
Enter 1st Hex no.:
10
Enter 2nd Hex no.:
12
Addition=  22
Multiplication=  120

Output 4
Enter 1st Hex no.:
A
Enter 2nd Hex no.:
C
Addition=  16
Multiplication=  78
*/
Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s