how to do the calculation.

hye,
i need to do a program in which i need to insert hexadecimal numbers. for example:
AB-CD-23. Then, i need to select B,D,3. i need to do calculation like this:
A+B+C+D+2+3= 33
33 = 110011[b](right now my code was up to this steps.)[/b] +B+D+3
= 110011 + 1011 +1101 + 0011
= 1001110

[code]
#include


void dec_bin(int number);

void main()
{
char s [80];
int i,value,sum=0;



printf ("Type a string less than 80 characters:");
gets(s);
printf ("The string types is:");
puts(s);

printf("Binary Equivalent= ");

for(i=0;s[i]!=NULL;i++)
{
switch(s[i])
{
case '0':
printf("0000");
value = 0;
break;
case '1':
printf("0001");
value = 1;
break;
case '2':
printf("0010");
value = 2;
break;
case '3':
printf("0011");
value = 3;
break;
case '4':
printf("0100");
value = 4;
break;
case '5':
printf("0101");
value = 5;
break;
case '6':
printf("0110");
value = 6;
break;
case '7':
printf("0111");
value = 7;
break;
case '8':
printf("1000");
value = 8;
break;
case '9':
printf("1001");
value = 9;
break;
case 'a':
case 'A':
printf("1010");
value = 10;
break;
case 'b':
case 'B':
printf("1011");
value = 11;
break;
case 'c':
case 'C':
printf("1100");
value = 12;
break;
case 'd':
case 'D':
printf("1101");
value = 13;
break;
case 'e':
case 'E':
printf("1110");
value = 14;
break;
case 'f':
case 'F':
printf("1111");
value = 15;
break;
default:
printf("Entered number is not Hexadecimal.Printed value is not correct.");
break;
}
sum=sum+value;
printf("+");






}

printf("
%d
",sum);
(sum >= 0) && (sum < 256) ? dec_bin(sum) : exit(1);

}


void dec_bin(int number) {
int x, y;
x = y = 0;

for(y = 7; y >= 0; y--) {
x = number / (1 << y);
number = number - x * (1 << y);
printf("%d", x);
}

printf("
");
}

[/code]
Sign In or Register to comment.

Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Categories