#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
Comments