Program to input two hexadecimal numbers and display their sum in octal form, using manipulators in C/C++

LEVEL: Beginner

Whats in this post?

  1. Code
  2. Output
  3. Background and Explanation of the code
  4. Download Code


To copy code: Click  View Source “View Source” on the right of this code snippet.

Background and Explanation of the code

1. In the above program , the variables ‘a’ and ‘b’ are input in hexadecimal form using hex manipulator.

2. The variable ‘sum’ is declared within the expression (sum=a+b) which is valid in C++.

3. Finally the sum is displayed in octal form using oct manipuator.

Enter two hexadecimal numbers = a 9(Press Enter)
Sum in octal form =23

Important information for beginners:
The manipulators are special functions that can be used with insertion (<<) and extraction (>>) operators to manipulate or format the data in a desired way. There are certain manipulators used that are used with ‘<<‘ operator to display the output in a particular format wheras certain manipulators are used with >> operator to input the data in a desired form.

Dec, Hex, Oct Manipulators
All the numbers are displayed and read in decimal notation by default. However you may change the base of a integer value to octal or hexadecimal or back to decimal by using the manipulators oct, hex or dec respectively. These manipulators are preceded by appropriate variables to be used with.