C code for how to convert large binary to octal

Posted By: Matpal Media - 5:26 AM
#include<stdio.h>
#define MAX 1000

int main(){
   
    char binaryNumber[MAX],octalNumber[MAX];
    long int i=0,j=0;

    printf("Enter any number any binary number: ");
    scanf("%s",binaryNumber);

    while(binaryNumber[i]){
      binaryNumber[i] = binaryNumber[i] -48;
      ++i;
    }

    --i;
    while(i-2>=0){
    octalNumber[j++] = binaryNumber[i-2] *4 +  binaryNumber[i-1] *2 + binaryNumber[i] ;
    i=i-3;
    }

    if(i ==1)
      octalNumber[j] = binaryNumber[i-1] *2 + binaryNumber[i] ;
    else if(i==0)
      octalNumber[j] =  binaryNumber[i] ;
    else
      --j;

    printf("Equivalent octal value: ");
    while(j>=0){
      printf("%d",octalNumber[j--]);
    }

    return 0;
}

0 comments:

Post a Comment

Note: Only a member of this blog may post a comment.