Sunday 14 October 2012

Case structure006

Write a program to convert an input number in Binary, Octal and Hexadecimal number. The choice should be input by user.

#include <stdio.h>
#include <conio.h>
void main ( )
{            
        int          no, cho, md[50], c, i;
        clrscr ( );
                  
        while ( 1 )
        {                 
               printf ("\n\t\t\t\t MENU");
               printf ("\n\t\t\t\t ====");
               printf ("\n\n\t\t   1. CONVERT FROM DECIMAL TO BINARY\n");
               printf ("\t\t   2. CONVERT FROM DECIMAL TO OCTAL \n");
               printf ("\t\t   3. CONVERT FROM DECIMAL TO HEXADECIMAL \n");
               printf ("\t\t   4. EXIT \n");
               
               printf ("\n\n\n\t\t   Your Choice ? "); scanf ("%d", &cho);
                           
               switch ( cho )
               {             
                     case 1:
                                 printf ("\n\n\t\t Enter a decimal number.......:"); scanf ("%d",&no);
                                 c = 0;
                                 while ( no > 0 )
                                 {            
                                          md[c] = no % 2;
                                          no = no / 2;
                                          c++;
                                 }
                                 printf ("\n\t\t BINARY NUMBER = ");
                                 for ( i = c - 1; i >= 0; i-- )
                                 {                     
                                          printf ("%d", md[i] );
                                 }
                                 break;
                          
                     case 2:       
                                 printf ("\n\n\t\t Enter the number.......:"); scanf ("%d",&no);
                                 c = 0;
                                 while ( no > 0 )
                                 {
                                          md[c] = no % 8;
                                          no = no / 8;
                                          c++;
                                 }
                                 printf ("\n\t\t OCTAL NUMBER = ");
                                 for ( i = c - 1; i >= 0; i-- )
                                 {              
                                          printf ("%d", md[i] );
                                 }
                                 break;
                    
                     case 3:      
                                 printf ("\n\n\t\t Enter the number.......:"); scanf ("%d",&no);
                                 c = 0;
                                 while ( no > 0 )
                                 {                    
                                          md[c] = no % 16;
                                          no = no / 16;
                                          c++;
                                 }
                                 printf ("\n\t\t HEXADECIMAL NUMBER = ");
                                 for ( i = c - 1; i >= 0; i-- )
                                 {            
                                          if ( md[i] > 9 )
                                          {                       
                                                      printf ("%c", md[i] + 55 );
                                          }
                                          else
                                          {
                                                      printf ("%d", md[i] );
                                          }
                                 }
                                 break;
                    
                     case 4:
                                 exit ( );
                                              
                     default:
                                 printf("\n WRONG CHOICE.");
              }
              getch ( );
              clrscr ( );
       }

}
Click here to contact us

No comments:

Post a Comment

Related Posts Plugin for WordPress, Blogger...