Menu Close

Conversion of binary to Octal…[Cprogm] FTC

Simon is studying B.Tech.-Mechanical Engineering.  He’s going to attend a computer science-based subject exam this semester. Due to the less preparation in the previous monthly tests,  his internal mark decreased.  His computer science Professor made an offer one more chance to boost up his internal marks. 

Professor assigns a program to Simon for the internal mark bootup. So Simon wants to solve Questions of conversion from Binary to octal.

can you help him in solving Questions by creating a function for the conversion of binary to octal?

Explanation:
Consider the binary number 1111 which has to be converted to octal.
Divide the binary number into groups of three from right to left. 1111 can be grouped as 001 111 Now, find the octal number for each group.
Three-bit binary numbers from (000 to 111) have equal decimal and octal representations.
Hence, the octal numbers for binary numbers in the range (000 to 111) are same as that of the decimal numbers. The octal number of 001 is 1 and 111 is 7. The octal equivalent of 1111 is 17.

Input:
     Input as an integer (010101) from the user 

Output:
      Print the “octal number” value.
#include <stdio.h>
#include <math.h>
int convertBinarytoOctal(long long binaryNumber);
int main()
{
  int long n;
  scanf("%ld",&n);
  printf("%d\n",convertBinarytoOctal(n));
return 0;
}
int convertBinarytoOctal(long long binaryNumber)
{
 int oct=0,dec=0,i=0;
 while(binaryNumber!=0){
 dec+=(binaryNumber%10)*pow(2,i);
 ++i;
 binaryNumber/=10;
 }
 i=1;
 while(dec!=0){
 oct+=(dec%8)*i;
 dec/=8;
 i*=10;
 }
return oct;
}

INPUT_1:
101001

OUTPUT:
51


INPUT_2:
101110

OUTPUT:
56


INPUT_3:
111111

OUTPUT:
77


INPUT_4:
110011

OUTPUT:
63


ILLUSTRATION

Executed using gcc