Tuesday, February 10, 2015

Binary to Octal

/* Convert Binary number to Octal number */ #include<stdio.h> #define MAX 1000 int main() { char binaryNumber[MAX]; char 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--]); } getch(); return 0; }

Share

& Comment

0 comments:

Post a Comment

 

Copyright © 2015 Code Fervor™ is a registered trademark.

Designed by Templateism By Naman Kumar