Posts

Showing posts with the label Conversion

C program to Convert Decimal to Hexadecimal

  1 /* Write a program to convert a decimal number to its 2 equivalent hexadecimal number. */ 3 4 #include <stdio.h> 5 main() 6 { 7 unsigned long dec; 8 int a[ 25 ],c= 0 ,i; 9 clrscr(); 10 printf( "\nENTER A DECIMAL NUMBER: " ); 11 scanf( "%lu" ,&dec); 12 printf( "\n%lu IN HEXADECIMAL FORMAT: " ,dec); 13 while (dec> 0 ) 14 { 15 a[c]=dec% 16 ; 16 dec=dec/ 16 ; 17 c++; 18 } 19 for (i=c- 1 ;i>= 0 ;i--) 20 { 21 if (a[i]>= 10 ) 22 printf( "%c" ,a[i]+ 55 ); 23 else 24 printf( "%d" ,a[i]); 25 } 26 getch(); 27 }}

C program to convert decimal to Binary

  #include <stdio.h> #include <stdlib.h> int main (){ system ( "clear" ); int decimal = 0 , arr [ 50 ] = { 0 }, remainder = 0 ; printf ( "Enter the Decimal Digit :" ); scanf ( " %d " , & decimal ); static int i ; for ( i = 0 ; i <= 50 ; i ++ ) { if ( decimal >= 2 ) { remainder = decimal % 2 ; arr [ i ] = remainder ; decimal = decimal / 2 ; } else { arr [ i ] = decimal ; break ; } } for ( i ; i >= 0 ; i -- ){ printf ( " %d " , arr [ i ]); } }