C++ Examples

WAP in C++ to Convert Octal number to a hexadecimal

The Program in C++ to Convert octal number to a hexadecimal number is given below:

#include <iostream>
#include <math.h>
using namespace std;

int main() {
  int decimal = 0, octal, remainder, product = 0;
  string hexadecimal = "";
  cout <<"Hello Codeauri Family,Enter an octal number to get converted into hexadecimal number:\n";
cin>>octal;
  
  // convert octal to decimal
  while(octal != 0) {
    decimal += (octal%10) * pow(8,product);
    ++product; 
    octal /= 10;
  }

  // convert decimal to hexadecimal
  while (decimal != 0) {
    remainder = decimal % 16;
    if (remainder < 10) {
      hexadecimal = char(remainder + '0') + hexadecimal;
    } else {
      hexadecimal = char(remainder - 10 + 'A') + hexadecimal;
    }
    decimal /= 16;
  }

  cout << "Well,the number in the hexadecimal form is: " << hexadecimal;
  return 0;
}

Output:

Hello Codeauri Family,Enter an octal number to get converted into hexadecimal number:
5
Well,the number in the hexadecimal form is: 5

Pro-Tips💡

This program works by first converting the input octal number to a decimal number using mathematical operations,

and then converting the decimal number to a hexadecimal number using a similar process.

The user is prompted to input an octal number, and the final hexadecimal number is outputted to the user.

The program uses remainder and a while loop to successively find the least significant digit of the decimal number and converts the digit to its corresponding hexadecimal digit.

Learn C-Sharp ↗

C-sharp covers every topic to learn about C-Sharp thoroughly.

Learn C Programming ↗

C-Programming covers every topic to learn about C-Sharp thoroughly.

Learn C++ Programming↗

C++ covers every topic to learn about C-Sharp thoroughly.

Codeauri is Code Learning Hub and Community for every Coder to learn Coding by navigating Structurally from Basic Programming to Front-End Development, Back-End Development to Database, and many more.

Related Posts

C# Program to Find Sum of Rows & Columns of a Matrix

The Program in C# Program to Find Sum of Rows & Columns of a Matrix is given below: Output: Hello Codeauri Family,enter the number of rows and columns…

C# Program to Calculate Determinant of Given Matrix

The Program in C# Program to Calculate Determinant of Given Matrix is given below: Output: Hello Codeauri Family, enter the number of rows and columns of the matrix…

C# Program to Find Sum of right Diagonals of a Matrix

The Program in C# Program to Find Sum of right Diagonals of a Matrix is given below: Output: Hello Codeauri Family, enter the number of rows and columns…

C# Program to Find Transpose of Given Matrix

The Program in C# Program to Find Transpose of Given Matrix is given below: Output: Hello Codeauri Family, enter the number of rows and columns in the matrix:22Enter…

C# Program for Multiplication of two square Matrices

The Program in C# Program for Multiplication of two square Matrices is given below: Output: Hello Codeauri Family, enter the number of rows/columns in the matrices:2Enter the elements…

C# Program to Delete Element at Desired position From Array

The Program in C# Program to Delete Element at Desired position From Array is given below: Output: Hello Codeauri Family, enter the number of elements in the array:4Enter…

Leave a Reply

Your email address will not be published. Required fields are marked *

Your Journey into Code Begins Now: Discover the Wonders of Basic Programming

X