 ### C++ Program to Convert Decimal to Binary, Octal and Hexadecimal

```#include <iostream>
#include <iterator>
#include <algorithm>
#include <bitset>//For binary conversion

//Function with converts the number to octal number
std::string toOctal(int num) {
std::string octalNum("");
while(num > 0) {
int total = num % 8;
num /= 8;
octalNum.append(std::to_string(total));
}
std::reverse(octalNum.begin(),octalNum.end());
return (std::move(octalNum));
}

//Function with converts the number to Binary number
std::string toBinary(int num) {
return std::move((std::bitset<8>(num).to_string()));
}

//Function with converts the number to Hexadecimal number
std::string toHex(int num) {
std::string hexNumber("");
while(num > 0) {
int rem = num % 16;
if(rem > 9){
switch(rem) {
case 10: hexNumber.append("A");break;
case 11: hexNumber.append("B");break;
case 12: hexNumber.append("C");break;
case 13: hexNumber.append("D");break;
case 14: hexNumber.append("E");break;
case 15: hexNumber.append("F");break;
}
}
else
hexNumber.append(std::to_string(rem));
num /= 16;
}
//Reversing the the string
std::reverse(hexNumber.begin(),hexNumber.end());
return(std::move(hexNumber));

}

std::string conversion(int num) {
std::string result("\n\nDecimal: ");
result.append(std::to_string(num));
result.append("\n");

//Converting to Binary
result.append("Binary: ");
result.append(toBinary(num));
result.append("\n");

//Converting to Octal
result.append("Octal: ");
result.append(toOctal(num));
result.append("\n");

//Converting to hex
result.append("Hex: ");
result.append(toHex(num));
result.append("\n");
return (std::move(result));
}

int main() {
std::cout << "Enter Number: ";

std::transform(std::istream_iterator<int>(std::cin),std::istream_iterator<int>(),
std::ostream_iterator<std::string>(std::cout,"\nNumber:"),conversion);
}
```

`OUTPUT:`

`Enter Number: 332`
```Decimal: 332
Binary: 0000000101001100
Octal: 514
Hex: 14C

Number:1234

Decimal: 1234
Binary: 0000010011010010
Octal: 2322
Hex: 4D2``` #### More Articles of Hatim Master:

Name Views Likes
Check if removing an edge can divide a Binary Tree in two halves 772 18
C++11 std::has_virtual_destructor 829 18
C++ program to find inorder successor in binary search tree using templates 1466 18
C++ future std::future::future (Constructor) 899 23
C++ Program to Merge Mails 780 21
C++ program for swapping two strings 578 19
C++ Program to count number of lines in a file 960 23
C++ Progam for checking prime numbers 647 15
C++ Program to Find Hash of File 3162 11
C++ program to check if an array represents Inorder of Binary Search tree or not 568 19
C++ future std::future::wait_until 1369 19
C++ future std::future::valid 809 22
C++ program for merge sort 995 18
C++ program to find median of binary search tree in O(n) using templates 687 19
C++11 std::genrate with std::array 727 17
C++ program to convert Binary to decimal,octal,hex 696 19
C++ Program to Check Vowel or Not 564 20
C++ std::find_first_of with vector 971 20
C++ program to find if there is a triplet in a Balanced binary search tree that adds to zero 600 16
C++ Program to Find the Factorial of a Number 682 21
C++ Exception std::exception(Constructor) 873 13
C++ atomic std::atomic::fetch_and 783 20
Check if a Binary Tree contains duplicate subtrees of size 2 or more 1128 17
C++ Program to Print the Fibonacci sequence 664 13
C++ future std::async 996 14
C++ program to check if a given binary tree is height balanced like a red-black tree 751 13
C++ program to insert an element into red black tree using template 2771 20
C++ program for Bubble sort using template 2734 13
C++11 std::genrate_n with std::vector 775 14
C++ program for copying string 509 11
C++ Program to Lowercase to Uppercase 643 15
C++ program to copy the content of the file 658 18
C++ program to count and display the number of lines not starting with the given alphabet in the file 1331 16
Finding Distance between two nodes 1029 12
C++ program for removing Punctuation 81 19
C++ program to find distance between two nodes of a binary search tree using templates 602 27
C++ program for solving Quadratic equation 761 26
C++ Program to Display Powers of 2 Using Anonymous Function 659 20
C++ program to add page at the beginning of the PDF 861 14
Print root to leaf paths without using recursion 745 26
C++ Program to Display Calendar 1289 29
C++11 std::genrate with std::forward_list 727 22
C++ std::find_if_not with std::deque 687 19
C++ program to query for ancestor-descendant relationship in a tree 805 13
C++ Program to Find ncR & nPr 830 21
Create loops of even and odd values in a binary tree 779 21
C++ atomic std::atomic::fetch_or 946 23
C++ program to convert kilometers to miles 693 27
C++ Program for comparing string 661 11
C++ Program to Find Factors of Number 604 24
C++ std::genrate with std::vector 741 14
C++ program to find vertical sum in binary tree 757 14
Program for shutdown or Restart PC. 721 20
C++ Program to Remove Words from Sentence 703 17
C++ program for Transpose matrix 732 22
C++ Constructor 857 20
C++ atomic std::atomic::compare_exchange_strong 806 16
Combining two sorted list and creating another sorted list 552 13
C++11 std::is_arithmetic 762 16
C++ program for adding matrices 672 20
C++ program to find check if given sorted sub-sequence exists in binary search tree 667 16
C++ Program to Convert Decimal to Binary, Octal and Hexadecimal 5605 22
C++ Program to Convert Fahrenheit to Celsius 725 21
C++ Program for finding size of string 617 24
C++ program to find a word in the file and print that line along with line number 1398 19
C++ program to reverse a path in binary search tree using queue using templates 920 12
C++ Program to Remove Spaces from String 733 21
C++ Program to Shuffle Deck of Cards 9214 18
C++ is_lock_free 689 16
C++ program to convert image/images to PDF 2508 24
C++ atomic std::atomic::compare_exchange_weak 811 19
C++ program for removing vowels from a string 752 25
C++ program for swapping two variable 614 18
Find Numbers Divisible by Number 1332 17
std::find_if_not with std::deque 715 18
C++ Program to Check Reverse equal Original 633 14
C++ program to count pairs from two binary search trees whose sum is equal to a given value x 544 14
C++ program for Insertion Sort 1232 18
C++ Program to Illustrate Different Set Operations 634 20
C++ Program to One Dimension Array Program 638 20
C++ program to count the number occurance of the given charactor in a file. 59 21
C++ program to print unique words 1571 16
C++11 std::is_abstract 698 26
C++ Program to Count the Number of Each Vowel 558 16
C++ program to find all the prime number in the given interval 590 26
C++ Program to Add Digits of Number 608 18
C++ Program to Print ASCII Values 613 15
Convert a Binary Tree into Doubly Linked List in spiral fashion 812 13
C++11 std::genrate with std::deque 656 20
C++ future std::future::get 944 21
C++ program to count the number of spaces present in a file. 627 19
C++ future std::future::share 831 18
C++ atomic std::atomic::fetch_xor 725 17
C++ program to check for identical binary serach trees without building the trees 515 13
C++ program to convert hexadecimal to binary,decimal,octal 787 22
C++ atomic std::atomic::operator-- 821 14
C++ program for multiplying matrices 586 27
C++ Program to append the file 778 24
C++ program for radix sort 1786 11
C++ program to read the content from a file, count and display the number of alphabets present in it. 581 26
std::find_first_of with std::deque 621 20
C++ program to construct ancestor matrix from a given binary tree using templates 577 15
C++ program to find list of files present in the directory 627 16
C++ std::atomic::store 1407 11
C++ program to find median of binary search tree 797 26
Print all root to leaf paths with there relative positions 708 24
Convert a tree to forest of even nodes 617 17
Convert a Binary Tree to a Circular Doubly Link List 626 16
Merge Two Binary Trees by doing Node Sum 779 23
C++ program to implement linux grep command. 2435 21
C++ Program to Check Leap Year 649 15
C++11 std::genrate_n with std::list 703 28
C++ atomic std::atomic::operator++ 888 23
C++ program to count the number of word in the file 1097 18
C++ Program for Counting sort 870 28
C++ program to get number of page of the pdf file 1324 19
C++ Program to Check Alphabet or Not 478 12
C++11 std::genrate_n with std::array 699 18
C++ future std::future::wait_for 1704 18
C++ program for concatenate String 725 28
std::find_if_not with std::array 786 26
std::find_if_not with std::vector 776 30
C++ Program to Uppercase to Lowercase 575 18
C++ Program to Count Word in Sentence 739 11
C++ program for Subtracting matrices 527 26
C++ Program to Make a Simple Calculator 529 17
Random Number 594 14
C++ program for checking the number is even or odd 722 27
std::find_if_not with std::list 692 28
C++ Program to convert octal to binary,decimal and hex 601 11
C++ program to encrypt a PDF file 1803 13
c++ program to get size of the file 572 15
C++ program to create a doubly linked list from a ternary tree 561 27
std::find_first_of with std::list 1062 20
C++ future std::future::wait 856 27
C++ Program to Display Fibonacci Sequence Using Recursion 645 16
C++ Program to Find Armstrong Number in an Interval 742 16
C++ program for Bucket Sort 4352 26
C++ std::genrate with std::list 573 18
C++ program to create a tree with left-child right-sibling representation using templates 1320 20
C++ program to shuffle words of each line of the given file 2184 21
C++ program to find simple recursive solution to check whether binary search tree contains dead end using templates 513 15
C++ atomic_flag 845 13
Maximum spiral sum in Binary Tree 671 27
C++ atomic std::atomic::fetch_sub 753 18
C++ program to read content of multiple files and generate one output file 913 22
C++ Program to print Pattern 606 22
C++ program to Decrypt a PDF File 1678 13
C++ Program to Merge Two Files 1775 19
C++ Program for reversing the String 555 24
C++ program to merge two files 738 14
Check if leaf traversal of two Binary Trees is same? 590 20
C++ program to get a page from PDF file 1122 19
C++ program for counting the character in the String 456 17
C++ Program to Check Armstrong Number 683 25
C++ program to check given array of size n can represent binary search tree of n levels or not 688 27
C++ Program to Sort Words in Alphabetic Order 655 24
C++11 std::alignment_of 671 13
C++ program to count the number of new lines present in a file. 636 16
C++ Program to Find HCF & LCM 642 20
C++ program to shuffle lines of the given file 647 12
C++ Program to Find the Size (Resolution) of a Image 1867 20
std::find_if_not with std::forward_list 693 23
C++ program to construct tree from ancestor matrix using templates 910 14
C++ std::atomic::exchange 760 19
std::find_fist_of with std::forward_list 622 15
C++11 std::extent 755 14
C++ program to merge PDF files 3049 22