1. Question:C program to convert binary to octal 

    Answer
    #include<stdio.h>
    #define MAX 1000
    int main(){
       
        char octalNumber[MAX];
        long int i=0;
    
        printf("Enter any octal number: ");
        scanf("%s",octalNumber);
    
        printf("Equivalent binary value: ");
        while(octalNumber[i]){
            switch(octalNumber[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("\nInvalid octal digit %c ",octalNumber[i]); return 0;
        }
        i++;
     }
    
        return 0;
    }
    Sample output: Enter any octal number: 123 Equivalent binary value: 001010011

    1. Report
  2. Question:C code to convert octal number to decimal number 

    Answer
    #include<stdio.h>
    #include<math.h>
    
    int main(){
       
        long int octal,decimal =0;
        int i=0;
    
        printf("Enter any octal number: ");
        scanf("%ld",&octal);
    
        while(octal!=0){
             decimal = decimal + (octal % 10) * pow(8,i++);
             octal = octal/10;
        }
    
        printf("Equivalent decimal value: %ld",decimal);
    
       return 0;
    }
    Sample output: Enter any octal number: 346 Equivalent decimal value: 230

    1. Report
  3. Question:C program to change octal to decimal 

    Answer
    #include<stdio.h>
    
    int main(){
       
        long int octalNumber;
    
        printf("Enter any octal number: ");
        scanf("%o",&octalNumber);
    
        printf("Equivalent decimal number is: %d",octalNumber);
    
        return 0;
    }
    Sample output: Enter any octal number: 17 Equivalent decimal number is: 15

    1. Report
  4. Question:C program for hexadecimal to binary conversion 

    Answer
    #include<stdio.h>
    #define MAX 1000
    
    int main(){
        char binaryNumber[MAX],hexaDecimal[MAX];
        long int i=0;
    
        printf("Enter any hexadecimal number: ");
        scanf("%s",hexaDecimal);
    
        printf("\nEquivalent binary value: ");
        while(hexaDecimal[i]){
             switch(hexaDecimal[i]){
                 case '0': printf("0000"); break;
                 case '1': printf("0001"); break;
                 case '2': printf("0010"); break;
                 case '3': printf("0011"); break;
                 case '4': printf("0100"); break;
                 case '5': printf("0101"); break;
                 case '6': printf("0110"); break;
                 case '7': printf("0111"); break;
                 case '8': printf("1000"); break;
                 case '9': printf("1001"); break;
                 case 'A': printf("1010"); break;
                 case 'B': printf("1011"); break;
                 case 'C': printf("1100"); break;
                 case 'D': printf("1101"); break;
                 case 'E': printf("1110"); break;
                 case 'F': printf("1111"); break;
                 case 'a': printf("1010"); break;
                 case 'b': printf("1011"); break;
                 case 'c': printf("1100"); break;
                 case 'd': printf("1101"); break;
                 case 'e': printf("1110"); break;
                 case 'f': printf("1111"); break;
                 default:  printf("\nInvalid hexadecimal digit %c ",hexaDecimal[i]); return 0;
             }
             i++;
        }
    
        return 0;
    }
    Sample output: Enter any hexadecimal number: 2AD5 Equivalent binary value: 0010101011010101

    1. Report
  5. Question:C program c program to convert binary to octal 

    Answer
    #include<stdio.h>
    int main(){
       
        long int binaryNumber,octalNumber=0,j=1,remainder;
    
        printf("Enter any number any binary number: ");
        scanf("%ld",&binaryNumber);
    
        while(binaryNumber!=0){
             remainder=binaryNumber%10;
            octalNumber=octalNumber+remainder*j;
            j=j*2;
            binaryNumber=binaryNumber/10;
        }
    
        printf("Equivalent octal value: %lo",octalNumber);
    
        return 0;
    }
    Sample output: Enter any number any binary number: 1101 Equivalent hexadecimal value: 15

    1. Report
  6. Question:C code for how to convert large binary to octal 

    Answer
    #include<stdio.h>
    #define MAX 1000
    
    int main(){
       
        char binaryNumber[MAX],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--]);
        }
    
        return 0;
    }
    Sample output: Enter any number any binary number: 1111111111111111111 1111111111111111111111111111111111111111111111111111111 1111111111111111111111111111111111111111111111111111111 1111111111111111111111111111111111111111111111111111111 1111111111111111111111111111111111111111111111111111111 11111111 Equivalent octal value: 3777777777777777777777777777777 7777777777777777777777777777777777777777777777777777

    1. Report
  7. Question:C code for binary to decimal conversion 

    Answer
    #include<stdio.h>
    
    int main(){
       
        long int binaryNumber,decimalNumber=0,j=1,remainder;
    
        printf("Enter any number any binary number: ");
        scanf("%ld",&binaryNumber);
    
        while(binaryNumber!=0){
             remainder=binaryNumber%10;
            decimalNumber=decimalNumber+remainder*j;
            j=j*2;
            binaryNumber=binaryNumber/10;
        }
    
        printf("Equivalent decimal value: %ld",decimalNumber);
    
        return 0;
    }
    Sample output: Enter any number any binary number: 1101 Equivalent decimal value: 13

    1. Report
  8. Question:C program for hexadecimal to binary conversion 

    Answer
    #include<stdio.h>
    int main(){
       
        long int binaryNumber,hexadecimalNumber=0,j=1,remainder;
       
        printf("Enter any number any binary number: ");
        scanf("%ld",&binaryNumber);
       
        while(binaryNumber!=0){
        remainder=binaryNumber%10;
        hexadecimalNumber=hexadecimalNumber+remainder*j;
            j=j*2;
            binaryNumber=binaryNumber/10;
          }
       
        printf("Equivalent hexadecimal value: %lX",hexadecimalNumber);
    
        return 0;
    }
    Sample output: Enter any number any binary number: 1101 Equivalent hexadecimal value: D

    1. Report
  9. Question:How to convert large binary number to hexadecimal 

    Answer
    #include<stdio.h>
    #define MAX 1000
    
    int main(){
    
       char binaryNumber[MAX],hexaDecimal[MAX];
       int temp;
       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){
           temp =  binaryNumber[i-3] *8 + binaryNumber[i-2] *4 +  binaryNumber[i-1] *2 + binaryNumber[i] ;
           if(temp > 9)
                hexaDecimal[j++] = temp + 55;
           else
                hexaDecimal[j++] = temp + 48;
           i=i-4;
       }
    
       if(i ==1)
          hexaDecimal[j] = binaryNumber[i-1] *2 + binaryNumber[i] + 48 ;
       else if(i==0)
          hexaDecimal[j] =  binaryNumber[i] + 48 ;
        else
          --j;
    
       printf("Equivalent hexadecimal value: ");
       while(j>=0){
          printf("%c",hexaDecimal[j--]);
       }
    
       return 0;
    }
    Sample output: Enter any number any binary number: 1010011011100011110 001001111011110001000100011101110111011110 Equivalent hexadecimal value: 14DC789EF111DDDE

    1. Report
  10. Question:C code for sum of two binary numbers 

    Answer
    #include<stdio.h>
    
    int main(){
    
        long int binary1,binary2;
        int i=0,remainder = 0,sum[20];
    
        printf("Enter any first binary number: ");
        scanf("%ld",&binary1);
        printf("Enter any second binary number: ");
        scanf("%ld",&binary2);
    
        while(binary1!=0||binary2!=0){
             sum[i++] =  (binary1 %10 + binary2 %10 + remainder ) % 2;
             remainder = (binary1 %10 + binary2 %10 + remainder ) / 2;
             binary1 = binary1/10;
             binary2 = binary2/10;
        }
    
        if(remainder!=0)
             sum[i++] = remainder;
    
        --i;
        printf("Sum of two binary numbers: ");
        while(i>=0)
             printf("%d",sum[i--]);
    
       return 0;
    }
    Sample output: Enter any first binary number: 1100011 Enter any second binary number: 1101 Sum of two binary numbers: 1110000

    1. Report
Copyright © 2024. Powered by Intellect Software Ltd