Category: CPlusPlus Programs

c++ logo

  • C++ Program to Convert Hexadecimal to Binary

    This is a C++ Program to Convert a HexadecimalNumber to its Binary Equivalent. 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.

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


    C++ Program to Convert Hexadecimal to Binary

    #include<iostream>
    using namespace std;
    
    void conversionFunc(string);
    
    //main function
    int main()
    {
        string hexDeci;
        
        cout << "Enter the Hexadecimal Number: ";
        cin >> hexDeci;
        
       cout << "Equivalent Binary value: " ;
       conversionFunc(hexDeci);
       
       return 0;
    }
    
    //user-defined function
    void conversionFunc(string hexDeci)
    {
        int i = 0;
        
       while (hexDeci[i])
       {
          switch (hexDeci[i])
          {
          case '0':
             cout << "0000";
             break;
          case '1':
             cout << "0001";
             break;
          case '2':
             cout << "0010";
             break;
          case '3':
             cout << "0011";
             break;
          case '4':
             cout << "0100";
             break;
          case '5':
             cout << "0101";
             break;
          case '6':
             cout << "0110";
             break;
          case '7':
             cout << "0111";
             break;
          case '8':
             cout << "1000";
             break;
          case '9':
             cout << "1001";
             break;
          case 'A':
          case 'a':
             cout << "1010";
             break;
          case 'B':
          case 'b':
             cout << "1011";
             break;
          case 'C':
          case 'c':
             cout << "1100";
             break;
          case 'D':
          case 'd':
             cout << "1101";
             break;
          case 'E':
          case 'e':
             cout << "1110";
             break;
          case 'F':
          case 'f':
             cout << "1111";
             break;
          default:
             cout << "Invalid HexaDecimal Number----"<< hexDeci[i];
          }
       i++;
       }
    }
    

    Output:

    //Run 1
    Enter the Hexadecimal Number: A
    Equivalent Binary value: 1010

    //Run 2
    Enter the Hexadecimal Number: 2A
    Equivalent Binary value: 00101010

    You may go through the vice-versa program:


  • C++ Program to Convert Binary to Hexadecimal

    This is a C++ Program to Convert a Binary Number to its Hexadecimal Equivalent. 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.

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


    C++ Program to Convert Binary to Hexadecimal

    #include <iostream>
    #include <math.h>
    using namespace std;
    
    int main()
    {
       int hexNumber[100];
       int i = 1, j = 0, rem, dec = 0, binNumber;
    
       cout << "Enter a Binary number: ";
       cin >> binNumber;
    
       while (binNumber > 0)
       {
          rem = binNumber % 2;
          dec = dec + rem * i;
          i = i * 2;
          binNumber = binNumber / 10;
       }
    
       i = 0;
    
       while (dec != 0)
       {
          hexNumber[i] = dec % 16;
          dec = dec / 16;
          i++;
       }
    
       cout << "Equivalent Hexadecimal value: ";
       for (j = i - 1; j >= 0; j--)
       {
          if (hexNumber[j] > 9)
          {
             cout << (char)(hexNumber[j] + 55);
          }
          else
          {
             cout << hexNumber[j];
          }
       }
    }

    Output:

    Enter a Binary number: 11111
    Equivalent Hexadecimal value: 1F

    You may go through the vice-versa 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 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.

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


    C++ Program to Convert Binary to Octal

    #include <iostream>
    #include <cmath>
    using namespace std;
    
    //user defined function
    int conversionFunc(int binNumber)
    {
       int octNumber = 0, deciNum = 0, i = 0;
    
       while (binNumber != 0)
       {
          deciNum += (binNumber % 10) *pow(2, i);
          ++i;
          binNumber /= 10;
       }
    
       i = 1;
    
       while (deciNum != 0)
       {
          octNumber += (deciNum % 8) *i;
          deciNum /= 8;
          i *= 10;
       }
    
       return octNumber;
    }
    
    // MAIN Function
    int main()
    {
       int binNumber, result;
    
       cout << "Enter a binary number: ";
       cin >> binNumber;
    
       result = conversionFunc(binNumber);
       cout << "Equivalent Octal value: " << result;
    
       return 0;
    }

    Output:

    Enter a binary number: 1101110
    Equivalent Octal value: 156

    You may go through the vice versa conversion:


  • C++ Program to Convert Octal to Binary

    In this tutorial, we will write a program to convert an octal number into binary in C++. 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.

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


    C++ Program to Convert Octal to Binary

    #include <iostream>
    #include <cmath>
    using namespace std;
    
    //user defined function
    int conversionFunc(int octNumber)
    {
       int deciNum = 0, binNumber = 0, i = 0;
    
       while (octNumber != 0)
       {
          deciNum += (octNumber % 10) *pow(8, i);
          ++i;
          octNumber /= 10;
       }
    
       i = 1;
    
       while (deciNum != 0)
       {
          binNumber += (deciNum % 2) *i;
          deciNum /= 2;
          i *= 10;
       }
    
       return binNumber;
    }
    
    //Main function
    int main()
    {
       int octNumber, result;
    
       cout << "Enter an octal number: ";
       cin >> octNumber;
    
       result = conversionFunc(octNumber);
       cout << "Equivalent Binary Number: " << result;
    
       return 0;
    }

    Output:

    Enter an octal number: 64
    Equivalent Binary Number: 110100

    You may go through the vice versa conversion:


  • 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 <iostream>
    #include <math.h>
    using namespace std;
    
    int main()
    {
       char hexDeci[20];
       int deci = 0, rem, i = 0, len = 0;
    
       cout << "Enter the Hexadecimal Number: ";
       cin >> hexDeci;
    
       //calculating the length of hexa entered
       while (hexDeci[i] != '\0')
       {
          len++;
          i++;
       }
    
       len--;
       i = 0;
    
       while (len >= 0)
       {
          rem = hexDeci[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;
          else
          {
             cout << "The entered hexa digit is invalid";
             return 0; //exit the program
          }
    
          deci += (rem* pow(16, i));
          len--;
          i++;
       }
    
       //Display
       cout << "Decimal Value: " << deci;
    
       return 0;
    }

    Output:

    //Run 1
    Enter the Hexadecimal Number: 119E
    Decimal Value: 4510

    //Run 2
    Enter the Hexadecimal Number: 45H
    The entered hexa digit is invalid

    Also, here we have used a while loop to calculate the length of the hexadecimal number entered. However, you can use strlen() function to calculate the length. Remember to include string.h header file before using this string function.

    You may go through the following vice versa program:


  • 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

    The programs ask the user to enter a decimal number and iterating the number with the help of a while loop, the program calculates the equivalent hexadecimal value.

    #include <iostream>
    using namespace std;
    
    int main()
    {
       char hexaDeci[50];
       int deci, rem, i = 0;
    
       cout << "Enter the Decimal Number: ";
       cin >> deci;
    
       while (deci != 0)
       {
          rem = deci % 16;
          if (rem < 10)
             rem = rem + 48;
          else
             rem = rem + 55;
    
          hexaDeci[i] = rem;
          i++;
          deci = deci / 16;
       }
    
       //Display
       cout << "Hexadecimal value: ";
       for (i = i - 1; i >= 0; i--)
          cout << hexaDeci[i];
    
       return 0;
    }

    Output:

    Enter the Decimal Number: 4510
    Hexadecimal value: 119E

    You may go through the following vice versa program:


  • C++ Program to Convert Decimal to Octal Number

    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.

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


    C++ Program to Convert Decimal to Octal Number

    The program takes a user input for a decimal number. And then iterate using a while loop. You can also use for loop instead of while loop, the code remains the same. Lastly, display the equivalent octal value.

    #include <iostream>
    using namespace std;
    
    int main()
    {
       int deci, oct[50], i = 0;
    
       cout << "Enter a Decimal number: ";
       cin >> deci;
    
       // calculation
       while (deci != 0)
       {
          oct[i] = deci % 8;
          i++;
          deci = deci / 8;
       }
    
       //Display the result
       cout << "Octal Value: ";
       for (i = (i - 1); i >= 0; i--)
          cout << oct[i];
    
       return 0;
    }

    Output:

    Enter a Decimal number: 8
    Octal Value: 10

    You may go through the following program for vice-versa:


  • C++ Program to Convert Octal to Decimal

    In this tutorial, we will write a program to convert octal numbers into decimal numbers 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.

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


    C++ Program to Convert Octal to Decimal Number

    The program takes a user input for an octal number. And then iterate using a while loop. You can also use for loop instead of while loop, the code remains the same. Lastly, display the equivalent decimal value.

    One of the libraries math functions (pow()) is used. This function returns the value of power calculation maths. It is present in math.h header file.

    #include <iostream>
    #include <math.h>
    using namespace std;
    
    int main()
    {
       int oct, deci = 0, i = 0, rem;
    
       cout << "Enter the Octal Number: ";
       cin >> oct;
    
       while (oct != 0)
       {
          rem = oct % 10;
          deci = deci + (rem* pow(8, i));
          i++;
          oct = oct / 10;
       }
    
       cout << "Decimal Value: " << deci;
    
       return 0;
    }

    Output:

    Enter the Octal Number: 10
    Decimal Value: 8


    You can also achieve the octal to decimal conversion using a function in C++. The way to do that is by creating a separate function where you pass the user entered octal value as an argument in a function.

    After calculation, the function returns the decimal to the main function where the final result is displayed.

    //convert octal to decimal using function in C++
    
    #include <iostream>
    #include <cmath>
    using namespace std;
    
    // User defined function
    int conversionFunc(int oct)
    {
       int rem, deci = 0, i = 0;
    
       while (oct != 0)
       {
          rem = oct % 10;
          oct = oct / 10;
          deci = deci + (rem* pow(8, i));
          ++i;
       }
    
       return deci;
    }
    
    //MAIN FUNCTION
    int main()
    {
       int octal, result;
       cout << "Enter an octal number: ";
       cin >> octal;
    
       //calling a function
       result = conversionFunc(octal);
       cout << "Decimal value: " << result;
    
       return 0;
    }

    Output:

    Enter an octal number: 10
    Decimal value: 8

    You may go through the following program for vice-versa:


  • C++ Program to Convert Binary to Decimal

    In this tutorial, we will write a program to convert a binary number into decimal in C++. 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.

    Decimal Number

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

    Here is the chart where for the equivalent value of decimal and binary numbers.

    Decimal to binary

    Now let us go through a program for the conversion of binary to decimal in C++.


    C++ Program to Convert Binary to Decimal

    The program asks the user to enter a binary number and using a while loop it calculates the decimal value. Lastly, display the equivalent decimal value.

    #include <iostream>
    using namespace std;
    
    int main()
    {
       int bin, deci = 0, i = 1, rem;
    
       cout << "Enter a Binary Number: ";
       cin >> bin;
    
       while (bin != 0)
       {
          rem = bin % 10;
          deci = deci + (rem *i);
          i = i * 2;
          bin = bin / 10;
       }
    
       //Display the equivalent decimal number
       cout << "Decimal Value: " << deci;
    
       return 0;
    }

    Output:

    Enter a Binary Number: 1010
    Decimal Value: 10

    Also, check out a similar program:


  • C++ Program to Convert Decimal to Binary

    In this tutorial, we will learn to convert a decimal number into binary in C++. 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.

    Decimal Number

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

    Here is the chart where for the equivalent value of decimal and binary numbers.

    Decimal to binary

    Now let us go through a program for the conversion of decimal to binary in C++.


    C++ Program to Convert Decimal to Binary

    The program asks the user to enter a decimal number and using a for loop it calculates the binary value. Lastly, display the equivalent binary value.

    #include <iostream>
    using namespace std;
    
    int main()
    {
       int bin[10], deci, i;
    
       cout << "Enter the decimal number: ";
       cin >> deci;
    
       for (i = 0; deci > 0; i++)
       {
          bin[i] = deci % 2;
          deci = deci / 2;
       }
    
       //Display the converted decimal number
       cout << "Binary value: ";
       for (i = i - 1; i >= 0; i--)
          cout << bin[i];
    
       return 0;
    }

    Output:

    Enter the decimal number: 10
    Binary value: 1010

    Also, check out a similar program: