Menu BAR

FEEL FREE TO ASK ANY PROGRAM and please mention any error if you find it

28 Feb 2013

Convert Binary to Octal Number

Q)Write a C program to convert the BINARY NUMBER to OCTAL NUMBER


Sol)

#include<stdio.h>

int main( )
{

    long int binary_no, octal_no=0, j=1, rem;
   
    printf("Enter any Binary number: " ) ;
    scanf("%ld", &binary_no) ;

    while(binary_no!=0)
       {
              rem=binary_no%10 ;
              octal_no=octal_no+rem*j ;
              j=j*2;
              binary_no=binary_no/10 ;
       }

     printf("Equivalent octal value: %ld", octal_no) ;
     return 0;
}

1 comment:

  1. use "%o" instead of %d...otherwise it will print decimal equivalent of the binary number...

    ReplyDelete