C and C++

Moderators: None (Apply to moderate this forum)
Number of threads: 28629
Number of posts: 94611

This Forum Only
Post New Thread
Single Post View       Linear View       Threaded View      f

Report
how to do the calculation. Posted by ierayahaya on 2 Feb 2010 at 2:42 AM
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(right now my code was up to this steps.) +B+D+3
= 110011 + 1011 +1101 + 0011
= 1001110

#include <stdio.h>


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("\n%d\n",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("\n");
}





 

Recent Jobs

Official Programmer's Heaven Blogs
Web Hosting | Browser and Social Games | Gadgets

Popular resources on Programmersheaven.com
Assembly | Basic | C | C# | C++ | Delphi | Flash | Java | JavaScript | Pascal | Perl | PHP | Python | Ruby | Visual Basic
© Copyright 2011 Programmersheaven.com - All rights reserved.
Reproduction in whole or in part, in any form or medium without express written permission is prohibited.
Violators of this policy may be subject to legal action. Please read our Terms Of Use and Privacy Statement for more information.
Operated by CommunityHeaven, a BootstrapLabs company.