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 : –

[codesyntax lang=”c” lines=”normal”]

#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();
}

[/codesyntax]

SCREEN SHOTS :-

MCS011_Q2

MCS011_Q2_Output

Leave a Reply