Loading...

Octal to Binary

Programme:

#include <stdio.h>
#define MAX 1000

int main()
{
    char octalnum[MAX];
    long i =0;
    
    printf("Enter any octal number: ");
    scanf("%s", octalnum);
    printf("Equivalent binary value: ");
    while(octalnum[i])
    {
        switch(octalnum[i])
        {
            case'0':
                printf("000");
            break;
            
            case'1':
                printf("001");
            break;
            
            case'2':
                printf("010");
            break;
            
            case'3':
                printf("011");
            break;
            
            case'4':
                printf("100");
            break;
            
            case'5':
                printf("101");
            break;
            
            case'6':
                printf("110");
            break;
            
            case'7':
                printf("111");
            break;
            
            default:
                printf("\n Invalid octal digit %c", octalnum[i]);
            return 0;
        }
        i++;
    }
    return 0;
}

Output:

Enter any octal number: 32                                                                                                  
Equivalent binary value: 011010