BINARY TO OCTAL CONVERSION

Consider the following I/O samples.





SAMPLE INPUT : 10010

SAMPLE OUTPUT:  22





C PROGRAM:

#include <stdio.h>
#include<math.h>
int main()
{
    long int n, oct=0, dec=0, i=0;
    scanf("%ld",&n);
    while(n)
    {
        dec+=(n%10)*pow(2,i);
        ++i;
        n/=10;
    }
    i=1;
    while(dec)
    {
        oct+=(dec%8)*i;
        dec/=8;
        i*=10;
    }
    printf("%ld",oct);
    return 0;
}

 


OUTPUT:

 



  
The given source code in C program is short and simple to understand. The source code is well tested in DEV-C++ and is completely error free. 
If you have any feedback about this article and want to improve this, please comment in the comment section.

 

Comments

Popular posts from this blog

C PROGRAM FOR Mth LARGEST AND Mth SMALLEST ELEMENT IN AN ARRAY

C PROGRAM TO CHECK BALANCED PARENTHESIS - ( { [

TRIANGULAR NUMBER OR NOT