A C Program to convert Binary to Octal number – IGNOU MCA Assignment 2013

By | September 12, 2013

MASTER OF COMPUTER APPLICATIONS
Course Code : MCS-011
Course Title : Problem Solving and Programming
Assignment Number : MCA(1)/011/Assign/13
Assignment 2013 

 

An interactive C program to convert a binary number to its octal equivalent.

 

#include<stdio.h>
void main()
{
     char S[15];
     int I,Y,LIMIT,LEN,OCT[15],BIN[15],RESULT,FLAG=0;
     clrscr();
     printf("ENTER BINARY NUMBER TO BE CONVERTED : \n");
     scanf("%s",&S);
     LEN=strlen(S);
     for(I=0;I<LEN;I++)
     {
       if( S[I]=='0' || S[I]=='1' )
       {
     if( S[I]=='0')
        BIN[I]=0;
     else
        BIN[I]=1;
       }
       else
        FLAG=1;
     }
  if(FLAG==0)
  {
     printf("\nBINARY NUMBER YOU ENTERED TO BE CONVERTED IS \n");
     for(I=0;I<LEN;I++)
     {
    printf("%d ",BIN[I]);
     }

     Y=0;
     for(I=0;I<LEN;I++)
     {
    Y=Y*2+BIN[I];
     }
     printf("\n\nCONVERTED BINARY TO DECIMAL FORM IS \t%d",Y);

     LEN=0;
     while(Y>0)
     {
    OCT[LEN]=Y%8;
    Y=Y/8;
    LEN++;
     };
     printf("\n\nCONVERTED BINARY TO OCTAL FORM IS \t");
     for(I=LEN-1;I>-1;I--)
     {
    printf("%d",OCT[I]);
     }

  }
  else
    printf("YOU HAVE ENTERED WRONG BINARY VALUE !!");
  getch();
}

CODE : -

  1. #include<stdio.h>
  2. void main()
  3. {
  4. char S[15];
  5. int I,Y,LIMIT,LEN,OCT[15],BIN[15],RESULT,FLAG=0;
  6. clrscr();
  7. printf("ENTER BINARY NUMBER TO BE CONVERTED : \n");
  8. scanf("%s",&S);
  9. LEN=strlen(S);
  10. for(I=0;I<LEN;I++)
  11. {
  12. if( S[I]=='0' || S[I]=='1' )
  13. {
  14. if( S[I]=='0')
  15. BIN[I]=0;
  16. else
  17. BIN[I]=1;
  18. }
  19. else
  20. FLAG=1;
  21. }
  22. if(FLAG==0)
  23. {
  24. printf("\nBINARY NUMBER YOU ENTERED TO BE CONVERTED IS \n");
  25. for(I=0;I<LEN;I++)
  26. {
  27. printf("%d ",BIN[I]);
  28. }
  29.  
  30. Y=0;
  31. for(I=0;I<LEN;I++)
  32. {
  33. Y=Y*2+BIN[I];
  34. }
  35. printf("\n\nCONVERTED BINARY TO DECIMAL FORM IS \t%d",Y);
  36.  
  37. LEN=0;
  38. while(Y>0)
  39. {
  40. OCT[LEN]=Y%8;
  41. Y=Y/8;
  42. LEN++;
  43. };
  44. printf("\n\nCONVERTED BINARY TO OCTAL FORM IS \t");
  45. for(I=LEN-1;I>-1;I--)
  46. {
  47. printf("%d",OCT[I]);
  48. }
  49.  
  50. }
  51. else
  52. printf("YOU HAVE ENTERED WRONG BINARY VALUE !!");
  53. getch();
  54. }

SCREEN SHOTS :-

MCS011_Q2

MCS011_Q2_Output

Leave a Reply