Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include<stdio.h>
- #include<math.h>
- int main()
- {
- int base, bin, oct, dec, hex, d, r, i;
- printf("Enter Base: ");
- I:
- scanf("%d",&base);
- if(base!=2 && base!=8 && base!=10 && base!=16) //INVALID
- {
- printf("Error!!! Invalid Base!\n\nThe Base Should be: \n2 for Binary. or \n8 for Octal. or \n10 for Decimal or.\n16 for Hexadecimal\n\nEnter The Base Again: ");
- goto I;
- }
- else if(base==2) //BINARY
- {
- printf("Enter The Binary Number: ");
- scanf("%d",&bin);
- dec=0, i=0;
- while(bin!=0)
- {
- r=bin%10;
- bin=bin/10;
- dec=dec+(r*pow(2,i));
- i++;
- }
- printf("The Corresponding Octal Value is: %o\n",dec);
- printf("The Corresponding Decimal Value is: %d\n",dec);
- printf("The Corresponding HexaDecimal Value is: %x\n",dec);
- }
- else if(base==8) //OCTAL
- {
- printf("Enter The Octal Number: ");
- scanf("%o",&oct);
- dec=oct, i=1, bin=0;
- while(dec!=0)
- {
- r=dec%2;
- dec/=2;
- bin+=r*i;
- i*=10;
- }
- printf("The Corresponding Binary Value is: %d\n",bin);
- printf("The Corresponding Decimal Value is: %d\n",oct);
- printf("The Corresponding Hexadecimal Value is: %x\n",oct);
- }
- else if(base==10) //DECIMAL
- {
- printf("Enter The Decimal Number: ");
- scanf("%d",&dec);
- d=dec, i=1, bin=0;
- while(d!=0)
- {
- r=d%2;
- d/=2;
- bin+=r*i;
- i*=10;
- }
- printf("The Corresponding Binary Value is: %d\n",bin);
- printf("The Corresponding Octal Value is: %o\n",dec);
- printf("The Corresponding HexaDecimal Value is: %x\n",dec);
- }
- else if(base==16) //HEXADECIMAL
- {
- printf("Enter The Hexadecimal Number: ");
- scanf("%x",&hex);
- dec=hex, i=1, bin=0;
- while(dec!=0)
- {
- r=dec%2;
- dec/=2;
- bin+=r*i;
- i*=10;
- }
- printf("The Corresponding Binary Value is: %d\n",bin);
- printf("The Corresponding Octal Value is: %o\n",hex);
- printf("The Corresponding Decimal Value is: %d\n",hex);
- }
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement