Tag: c conversion programs

  • C Program to Convert Hexadecimal to Octal

    In this tutorial, we will write a program to convert hexadecimal to octal in C. Before that, you must have knowledge of the following topics in C.

    • C operator
    • C while loop
    • C for loop

    Hexadecimal number

    The hexadecimal number is represented with a base of 16. It has digits from 0 to 15 to represent, However after 9 the values are represented in Alphabet till 15 such as 10 is represented as A, 11 as B, 12 as C, 13 as D, 14 as E, and 15 as F.

    Octal number

    The octal numbers are the numbers with base 8 and use the digits 0 to 7. Example: 8 in decimal is represented as 10 in octal, 25 as 31, and so on.

    Let us go through a program for the Hexadecimal to Octal Conversion in C.


    C Program to Convert Hexadecimal to Octal

    #include <stdio.h>
    #include <math.h>
    
    int main()
    {
      int deci = 0, oct[30], rem, i = 0, length = 0;
      char hexDec[10];
    
      printf("Enter Hexadecimal number: ");
      scanf("%s", hexDec);
    
      //find the length of the number entered
      while (hexDec[i] != '\0')
      {
        length++;
        i++;
      }
    
      length--;
    
      i = 0;
      while (length >= 0)
      {
        rem = hexDec[length];
    
        if (rem >= 48 && rem <= 57)
          rem -= 48;
        else if (rem >= 65 && rem <= 70)
          rem -= 55;
        else if (rem >= 97 && rem <= 102)
          rem -= 87;
        else
        {
          printf("The hexadecimal number entered is invalid.");
          return 0;	//exit the program
        }
    
        deci += (rem* pow(16, i));
        length--;
        i++;
      }
    
      i = 0;
      while (deci != 0)
      {
        oct[i] = deci % 8;
        i++;
        deci /= 8;
      }
    
      //display
      printf("Equivalent Octal Value: ");
      for (i = i - 1; i >= 0; i--)
        printf("%d", oct[i]);
    
      return 0;
    }

    Output:

    Enter Hexadecimal number: FF
    Equivalent Octal Value: 377


  • C Program to Convert Octal to Hexadecimal

    In this tutorial, we will write a program to convert octal to hexadecimal in C. Before that, you must have knowledge of the following topics in C.

    • C operator
    • C while loop
    • C for loop

    Hexadecimal number

    The hexadecimal number is represented with a base of 16. It has digits from 0 to 15 to represent, However after 9 the values are represented in Alphabet till 15 such as 10 is represented as A, 11 as B, 12 as C, 13 as D, 14 as E, and 15 as F.

    Octal number

    The octal numbers are the numbers with base 8 and use the digits 0 to 7. Example: 8 in decimal is represented as 10 in octal, 25 as 31, and so on.

    Let us go through a program for the Octal to Hexadecimal Conversion in C.


    C Program to Convert Octal to Hexadecimal

    #include <stdio.h>
    #include <math.h>
    
    int main()
    {
      int octalNum, rem, i = 0;
      char HexaDecimal[100];
      int decimalNum = 0, sem = 0;
    
      printf("Enter an Octal number: ");
      scanf("%d", &octalNum);
    
      //Octal to decimal covert
      while (octalNum != 0)
      {
        decimalNum = decimalNum + (octalNum % 10) *pow(8, sem);
        sem++;
        octalNum /= 10;
      }
    
      //Decimal to Hexadecimal
      while (decimalNum != 0)
      {
        rem = decimalNum % 16;
    
        if (rem < 10)
          HexaDecimal[i++] = rem + 48; // 48 Ascii=0
        else
          HexaDecimal[i++] = rem + 55; //55 Ascii=7
    
        decimalNum /= 16;
      }
    
      printf("Equivalent Hexadecimal Value: ");
      int j;
    
      for (j = i - 1; j >= 0; j--)
        printf("%c", HexaDecimal[j]);
    
      return 0;
    }

    Output:

    Enter an Octal number: 377
    Equivalent Hexadecimal Value: FF


  • C Program to Convert Binary to Hexadecimal

    In this tutorial, we will write a C Program to Convert a Binary Number to Hexadecimal Number. Before that, you must have knowledge of the following topics in C.

    Binary number

    The binary numbers are based on 0 and 1, so it is a base 2 number. They are the combination of 0 and 1. For example, 1001, 110101, etc.

    Hexadecimal number

    The hexadecimal number is represented with a base of 16. It has digits from 0 to 15 to represent, However after 9 the values are represented in Alphabet till 15 such as 10 is represented as A, 11 as B, 12 as C, 13 as D, 14 as E, and 15 as F.

    Input: 11011111
    Output: DF

    Input: 10001101
    Output: 8D

    Now let us go through a program for Binary to hexadecimal conversion in C.


    C Program to Convert Binary to Hexadecimal

    #include <stdio.h>
    #include <conio.h>
    
    int main()
    {
      long int binarynum, hexadecimalnum = 0, i = 1, rem;
    
      printf("Enter a Binary number: ");
      scanf("%ld", &binarynum);
    
      while (binarynum != 0)
      {
        rem = binarynum % 10;
        hexadecimalnum = hexadecimalnum + rem * i;
        i = i * 2;
        binarynum = binarynum / 10;
      }
    
      printf("Equivalent Hexadecimal number: %lX", hexadecimalnum);
    
      getch();
      return 0;
    }

    Output:

    Enter a Binary number: 10001101
    Equivalent Hexadecimal number: 8D


  • C Program to Convert Hexadecimal to Binary

    In this tutorial, we will write a C Program to Convert a Hexadecimal Number to Binary Number. Before that, you must have knowledge of the following topics in C.

    Binary number

    The binary numbers are based on 0 and 1, so it is a base 2 number. They are the combination of 0 and 1. For example, 1001, 110101, etc.

    Hexadecimal number

    The hexadecimal number is represented with a base of 16. It has digits from 0 to 15 to represent, However after 9 the values are represented in Alphabet till 15 such as 10 is represented as A, 11 as B, 12 as C, 13 as D, 14 as E, and 15 as F.

    Input: DF
    Output: 11011111

    Input: 8D
    Output: 10001101

    Now let us go through a program for hexadecimal to decimal conversion in C.


    C Program to Convert Hexadecimal to Binary

    #include <stdio.h>
    #define MAX 1000
    
    int main()
    {
      char binarynum[MAX], hexadecimalnum[MAX];
      long int i = 0;
    
      printf("Enter a hexadecimal number: ");
      scanf("%s", hexadecimalnum);
    
      printf("Equivalent binary value: ");
      while (hexadecimalnum[i])
      {
        switch (hexadecimalnum[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("Invalid hexadecimal Number.");
            return 0;
        }
    
        i++;
      }
    
      return 0;
    }

    Output:

    Enter a hexadecimal number: 8D
    Equivalent binary value: 10001101


  • C Program to Convert Decimal to Hexadecimal

    This is a C Program to Convert a Decimal Number to its Hexadecimal Equivalent. Before that, you must have knowledge of the following topics in C.

    Hexadecimal number

    The hexadecimal number is represented with a base of 16. It has digits from 0 to 15 to represent, However after 9 the values are represented in Alphabet till 15 such as 10 is represented as A, 11 as B, 12 as C, 13 as D, 14 as E, and 15 as F.

    Decimal Number

    These are the numbers with a base of 10, which ranges from 0 to 9. These numbers are formed with the combination of 0 to 9 digits such as 24, 345, etc.

    Now let us go through a program for decimal to hexadecimal conversion in C.


    C Program to Convert Decimal to Hexadecimal

    #include <stdio.h>
    #include <conio.h>
    
    int main()
    {
      int decimalnum, rem, i = 0;
      char hexanum[50];
    
      printf("Enter a Decimal number: ");
      scanf("%d", &decimalnum);
    
      while (decimalnum != 0)
      {
        rem = decimalnum % 16;
        if (rem < 10)
          rem = rem + 48;
        else
          rem = rem + 55;
        hexanum[i] = rem;
        i++;
        decimalnum /= 16;
      }
    
      printf("Equivalent Hexadecimal Value: ");
      for (i = i - 1; i >= 0; i--)
        printf("%c", hexanum[i]);
    
      getch();
      return 0;
    }

    Output:

    Enter a Decimal number: 125
    Equivalent Hexadecimal Value: 7D


  • C Program to convert Hexadecimal to Decimal

    This is a C Program to Convert a Hexadecimal Number to its decimal Equivalent. Before that, you must have knowledge of the following topics in C.

    Hexadecimal number

    The hexadecimal number is represented with a base of 16. It has digits from 0 to 15 to represent, However after 9 the values are represented in Alphabet till 15 such as 10 is represented as A, 11 as B, 12 as C, 13 as D, 14 as E, and 15 as F.

    Decimal Number

    These are the numbers with a base of 10, which ranges from 0 to 9. These numbers are formed with the combination of 0 to 9 digits such as 24, 345, etc.

    Now let us go through a program for Hexadecimal to decimal conversion in C.


    C Program to convert Hexadecimal to Decimal

    #include <stdio.h>
    #include <conio.h>
    #include <math.h>
    
    int main()
    {
      int decimal = 0, rem, i = 0, len = 0;
      char hexadecimal[20];
    
      printf("Enter a Hexadecimal number: ");
      scanf("%s", hexadecimal);
    
      while (hexadecimal[i] != '\0')
      {
        len++;
        i++;
      }
    
      len--;
      i = 0;
      while (len >= 0)
      {
        rem = hexadecimal[len];
        if (rem >= 48 && rem <= 57)
          rem = rem - 48;
        else if (rem >= 65 && rem <= 70)
          rem = rem - 55;
        else if (rem >= 97 && rem <= 102)
          rem = rem - 87;
    
        decimal = decimal + (rem* pow(16, i));
        len--;
        i++;
      }
    
      printf("Equivalent Decimal Value: %d", decimal);
    
      getch();
      return 0;
    }

    Output:

    Enter a Hexadecimal number: 7D
    Equivalent Decimal Value: 125


  • C Program to Convert Octal to Decimal

    In this tutorial, we will write a program to convert octal to decimal in C using whille loop. Before that, you must have knowledge of the following topics in C.

    Octal number

    The octal numbers are the numbers with base 8 and use the digits 0 to 7. Example: 8 in decimal is represented as 10 in octal, 25 as 31, and so on.

    Decimal Number

    These are the numbers with a base of 10, which ranges from 0 to 9. These numbers are formed with the combination of 0 to 9 digits such as 24, 345, etc.

    Example:

    Input: 400
    Output: 256

    Let us go through a program for the Octal to Decimal Conversion in C. we will perform two different C programs.

    1. Basic program
    2. Using user-defined function

    C Program to Convert Octal to Decimal

    Source code:

    #include <stdio.h>
    #include <math.h>
    
    int main()
    {
      int octalNum, decimalNum = 0, i = 0;
    
      printf("Enter an octal number: ");
      scanf("%d", &octalNum);
    
      while (octalNum != 0)
      {
        decimalNum = decimalNum + (octalNum % 10) *pow(8, i++);
        octalNum = octalNum / 10;
      }
    
      printf("Equivalent Decimal Value: %d", decimalNum);
    
      return 0;
    }

    Output:

    Enter an octal number: 400
    Equivalent Decimal Value: 256


    Using Function

    Source code: convert octal to decimal using function in C.

    #include <stdio.h>
    #include <math.h>
    
    int conversionFunc(int oct)
    {
      int i = 0, decimalNum = 0, rem;
    
      while (oct > 0)
      {
        rem = oct % 10;
        oct /= 10;
    
        decimalNum = decimalNum + rem* pow(8, i);
        ++i;
      }
    
      return decimalNum;
    }
    
    int main()
    {
      int octNum;
    
      printf("Enter an Octal number: ");
      scanf("%d", &octNum);
    
      printf("Equivalent Decimal Value: %d", conversionFunc(octNum));
    
      return 0;
    }

    Output: After the successful execution of the program, it will produce the same out as the above program.


  • C Program to Convert Decimal to Octal using Recursion and Loop

    In this tutorial, we will write a program to convert decimal to octal in C. Before that, you must have knowledge of the following topics in C.

    Octal number

    The octal numbers are the numbers with base 8 and use the digits 0 to 7. Example: 8 in decimal is represented as 10 in octal, 25 as 31, and so on.

    Decimal Number

    These are the numbers with a base of 10, which ranges from 0 to 9. These numbers are formed with the combination of 0 to 9 digits such as 24, 345, etc.

    Example:

    Input: 256
    Output: 400

    Let us go through a program for the Decimal to Octal Conversion in C.


    1. Using While loop

    Here, we will write a C program to convert decimal number to octal number using while loop. The program simply takes a decimal number as an input from the user and after calculation gives an octal output.

    #include <stdio.h>
    
    int main()
    {
      int deciNum, octalNum = 0, rem, i = 1;
    
      printf("Enter a Decimal number: ");
      scanf("%d", &deciNum);
    
      while (deciNum != 0)
      {
        rem = deciNum % 8;
        octalNum = (rem *i) + octalNum;
        deciNum /= 8;
    
        i *= 10;
      }
    
      printf("Equivalent Octal Value: %d", octalNum);
    
      return 0;
    }

    Output:

    Enter a Decimal number: 256
    Equivalent Octal Value: 400


    2. Using Recursion

    Here, we will convert a decimal number to octal number using recursion in C. Learn more on Recursion and its use.

    #include <stdio.h>
    
    void conversionFunc(int); //prototype
    
    int main()
    {
      int deciNum;
    
      printf("Enter a Decimal number: ");
      scanf("%d", &deciNum);
    
      printf("Equivalent Octal Value: ");
      conversionFunc(deciNum);
    
      return 0;
    }
    
    //recursive function
    void conversionFunc(int dec)
    {
      if (dec > 0)
      {
        conversionFunc(dec / 8);
        printf("%d", dec % 8);
      }
    }

    Output: After the successful execution of the program, it will produce the same out as the above program.


  • C Program to Convert Binary to Octal

    In this tutorial, we will write a C program to convert a binary number into octal using a while loop. Before that, you must have knowledge of the following topics in C.

    Binary number

    The binary numbers are based on 0 and 1, so it is a base 2 number. They are the combination of 0 and 1. For example, 1001, 110101, etc.

    Octal number

    The octal numbers are the numbers with base 8 and use the digits 0 to 7. Example: 8 in decimal is represented as 10 in octal, 25 as 31, and so on.

    Example:

    Input: 101001
    Output: 51

    Let us go through a program for the Binary to Octal Conversion in C.


    C Program to Convert Binary to Octal

    Source Code: convert binary to octal using a function in C

    #include <stdio.h>
    #include <math.h>
    
    int conversionFunc(int binaryNum)
    {
      int octalNum = 0, decNum = 0, i = 0;
    
      while (binaryNum != 0)
      {
        decNum += (binaryNum % 10) *pow(2, i);
        ++i;
        binaryNum /= 10;
      }
    
      i = 1;
    
      while (decNum != 0)
      {
        octalNum += (decNum % 8) *i;
        decNum = decNum / 8;
        i *= 10;
      }
    
      return octalNum;
    }
    
    int main()
    {
      int binaryNum;
    
      printf("Enter a binary number: ");
      scanf("%d", &binaryNum);
    
      printf("Octal Equivalent Value: %d", conversionFunc(binaryNum));
    
      return 0;
    }

    Output:

    Enter a binary number: 101001
    Octal Equivalent Value: 51


  • C Program to Convert Octal to Binary

    In this tutorial, we will write a C program to convert an octal number into binary using a while loop. Before that, you must have knowledge of the following topics in C.

    Binary number

    The binary numbers are based on 0 and 1, so it is a base 2 number. They are the combination of 0 and 1. For example, 1001, 110101, etc.

    Octal number

    The octal numbers are the numbers with base 8 and use the digits 0 to 7. Example: 8 in decimal is represented as 10 in octal, 25 as 31, and so on.

    Example:

    Input: 51
    Output: 101001

    Let us go through a program for the Octal to Binary Conversion in C.


    C Program to Convert Octal to Binary

    Source code: convert octal number to binary number using function

    #include <math.h>
    #include <stdio.h>
    
    int conversionFunc(int octalNum)
    {
      int decNum = 0, i = 0;
      int binaryNum = 0;
    
      while (octalNum != 0)
      {
        decNum += (octalNum % 10) *pow(8, i);
        ++i;
        octalNum /= 10;
      }
    
      i = 1;
    
      while (decNum != 0)
      {
        binaryNum += (decNum % 2) *i;
        decNum = decNum / 2;
        i *= 10;
      }
    
      return binaryNum;
    }
    
    int main()
    {
      int octalNum;
    
      printf("Enter an octal number: ");
      scanf("%d", &octalNum);
    
      printf("Equivalent Binary Value: %d", conversionFunc(octalNum));
    
      return 0;
    }

    Output:

    Enter an octal number: 51
    Octal Binary Value: 101001