The intent of this page is to collection the best program and help programmers to find good blog posts to read. Some of these blogs may not be written by Java developers, but Java developers should find it useful or interesting. Reading those blogs should be fun and often bring some fresh ideas

Saturday, February 8, 2020

C program:Converting Decimal number to any base(Binary/Octail/Hexademial)

C program to Converting  Decimal Number to Any Number

#include<stdio.h>

char * DectoAny_conversion(int no, int base){
static char buff[17];
int i;
for(i=8*sizeof(int)-1;no;i-- ,no/=base)
buff[i]="0123456789ABCDEF"[no%base];
return buff+i+1;
}

void main(){
int no,base;
printf("Enter the decimal number: ");
scanf("%d",&no);
 printf("Enter the Base: ");
scanf("%d",&base);
printf("%s",DectoAny_conversion(no,base));
}

OUTPUT: 

Enter the Decimal Number: 10

Enter Base: 2

1010

--------------------------------------------------------------------------------------------------------------------------


Decimal Number Conversion

A repeated division and remainder algorithm can convert decimal to binary, octal, or hexadecimal.
  1. Divide the decimal number by the desired target radix (2, 8, or 16).
  2. Append the remainder as the next most significant digit.
  3. Repeat until the decimal number has reached zero.

Decimal to Binary

Here is an example of using repeated division to convert 1792 decimal to binary:
Decimal Number  Operation  Quotient  Remainder  Binary Result
1792÷ 2 =89600
896÷ 2 =448000
448÷ 2 =2240000
224÷ 2 =11200000
112÷ 2 =56000000
56÷ 2 =280000000
28÷ 2 =1400000000
14÷ 2 =7000000000
7÷ 2 =31100000000
3÷ 2 =111100000000
1÷ 2 =0111100000000
0done.

Decimal to Octal

Here is an example of using repeated division to convert 1792 decimal to octal:
Decimal Number  Operation  Quotient  Remainder  Octal Result
1792÷ 8 =22400
224÷ 8 =28000
28÷ 8 =34400
3÷ 8 =033400
0done.

Decimal to Hexadecimal

Here is an example of using repeated division to convert 1792 decimal to hexadecimal:
Decimal Number  Operation  Quotient  Remainder  Hexadecimal Result
1792÷ 16 =11200
112÷ 16 =7000
7÷ 16 =07700
0done.
The only addition to the algorithm when converting from decimal to hexadecimal is that a table must be used to obtain the hexadecimal digit if the remainder is greater than decimal 9.
Decimal:01234567
Hexadecimal:01234567
Decimal:89101112131415
Hexadecimal:89ABCDEF

The addition of letters can make for funny hexadecimal values. For example, 48879 decimal converted to hex is:
Decimal Number  Operation  Quotient  Remainder  Hexadecimal Result
48879÷ 16 =305415F
3054÷ 16 =19014EF
190÷ 16 =1114EEF
11÷ 16 =011BEEF
0done.
Other fun hexadecimal numbers include: AD, BE, FAD, FADE, ADD, BED, BEE, BEAD, DEAF, FEE, ODD, BOD, DEAD, DEED, BABE, CAFE, C0FFEE, FED, FEED, FACE, BAD, F00D, and my initials DAC.

No comments:

Post a Comment

If you have any doubt . Please let me know

String Operation

String Operation Using collection Framework Sorting Order  public static void sortAccending(String s) { char[] arr = s.toCharArray(); ...

Adbox