BeginnersBook.in

Programming For Geeks

  • Home
  • All Tutorials
    • Learn C
    • Learn Python
  • Programs
    • Java Programs
    • C++ Programs
    • C Programs
  • Algorithms
    • Sorting
  • MCQ’s
    • MySQL
    • Operating System
  • WordPress
    • Genesis Pro Themes
    • Blogs
  • Tools
    • Direct Download Link Generator
    • HTML Compiler
  • How to
    • Web Examples
  • Write For Us

C++ Program to Convert Hexadecimal Number to Octal

May 3, 2018 By Mr. Robot

In this example, you will learn to convert a hexadecimal number to octal number. The program will take a hexadecimal number as an input from the user and convert it into a octal.

Example: Hexadecimal to Octal Conversion

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
#include<iostream>
#include<string.h>
#include<math.h>
using namespace std;
int hex_to_oct(char hexval[]);
 
int main()
{
    char hexval[20],c;
    int n;
    cout<<"Enter Hexadecimal Number : ";
    cin>>hexval;
    cout<<"Octal Value of "<<hexval<<" is: "<<hex_to_oct(hexval);
    return 0;
}
int hex_to_oct(char hexval[])
{
    int i,len, dec=0, oct=0;
    for(len=0; hexval[len]!='\0'; len++);
        for(i=0; hexval[i]!='\0'; i++,len--)
        {
            if(hexval[i]>='0' && hexval[i]<='9')
            {
                dec= dec + (hexval[i]-'0')*pow(16,len-1);
            }
            if(hexval[i]>='A' && hexval[i]<='F')
            {
                dec = dec + (hexval[i]-55)*pow(16,len-1);
            }
            if(hexval[i]>='a' && hexval[i]<='f')
            {
                dec = dec + (hexval[i]-87)*pow(16,len-1);
            }
        } /* Now dec contains the decimal value of given hexadecimal number. */
        i = 1;
        while(dec != 0)
        {
            oct = oct + (dec % 8) * i;
            dec = dec / 8;
            i = i * 10;
        }
        return oct;
}
When you run the program, the output will be following:
1
2
Enter Hexadecimal Number : 7E                                                                                                
Octal Value of 7E is: 176

Filed Under: C++ Examples

Related Posts

  • C++ Program to Check Spy Number
  • C++ Program to Add Two Matrix
  • C++ Program to Print Numbers Without Using Loop

Recent Posts

  • Download Grand Theft Auto: Vice City Audio File | Highly Compressed
  • Grand Theft Auto: Vice City Game Download For PC | Full Setup
  • SQL SHOW DATABASES Statement
  • Direct view storage Tube (DVST) in Computer Graphics
  • CRT Color Monitor in Computer Graphics

C++ Programs

  • More 300+ Examples
  • “Hello World” Program
  • Add Two Numbers
  • Area of Circle
  • Even or Odd Number
  • Find Factorial Number
  • Simple Calculator Program
  • Swap Two Numbers
  • Check Vowel and Consonant
  • Reverse a Number
  • Fibonacci Series
  • Leap Year
  • Toggle Characters
  • Check Super Digit
  • Check Anagram Strings
  • Spy Numbers
  • Check Automorphic Number
  • Check Neon Number
  • Check Pandigital Number
  • Strong Number
  • Inverse of Matrix

More C++ Examples…

  • Star(*) Pattern Programs
  • Wave Pattern Programs
  • Sorting Algorithms

Tutorials

  • C Tutorial

Copyright © 2019 - BeginnersBook.in - All Right Reserved || Sitemap