262 Views

How to write a c++ program to convert decimal to binary, octa and hexadecimal? In one program..


Sahithi K 17th Sep, 2019
Answer (1)
Shubham Kumar Gupta 17th Sep, 2019

Hello Sahithi, Greetings!

Required program without recursion:

Hello Sahithi, Greetings!

Required program without recursion:

#include<iostream> using namespace std; const int STACK_SIZE = 100; class stack { private: int count;// number of items in the stack int data[STACK_SIZE]; public: stack(); ~stack(); void push(const int item);// push an item on the stack int pop(void);// pop item off the stack }; stack::stack()// constructor { count = 0;// zero the stack } stack::~stack() {}// default destructor void stack::push(const int item) { if (count < STACK_SIZE) { data[count] = item; ++count; } else cout << "Overflow!\n"; } int stack::pop(void) { if (count >0) { --count; return (data[count]); } else { cout << "Underflow!\n"; return 0; } } int menu(); void toBinary(); void toOctal(); void toHex(); int main() { int choice = menu(); switch(choice) { case (0): toBinary(); break; case (1): toOctal(); break; case(2): toHex(); break; } return 0; } int menu() { int choice; cout << " *****Menu***** " << endl; cout << "Convert the number from decimal into: " << endl; cout << "0-Binary" << endl; cout << "1-Octal" << endl; cout << "2-Hexadecimal" << endl; cin >> choice; return choice; } void toBinary() { int num; int total = 0; stack reverse; // declare a local stack!!!!!!!!!!!!!!!!!! int ctr=0; // declare a local counter!!!!!!!!!!!!!!! cout << "Please enter a decimal: "; cin >> num; cout << "The decimal number " << num << " converts to the binary number: "; while(num > 0) { total = num % 2; num /= 2; //cout << total << " "; reverse.push(total); // save to stack instead of printing!!!!!!!!! ctr++; // count the number of digits saved!!!!!!!!!!!! } while (ctr > 0) { cout << reverse.pop() << " "; ctr--; } } void toOctal() { int num; int total = 0; stack reverse; // declare a local stack!!!!!!!!!!!!!!!!!! int ctr=0; // declare a local counter!!!!!!!!!!!!!!! cout << "Please enter a decimal: "; cin >> num; cout << "The decimal number " << num << " converts to the octal number: "; while(num > 0) { total = num % 8; num /= 8; //cout << total << " "; reverse.push(total); // save to stack instead of printing!!!!!!!!! ctr++; // count the number of digits saved!!!!!!!!!!!! } while (ctr > 0) { cout << reverse.pop() << " "; ctr--; } } void toHex() { int num,counter,x,a,hex[100]; //char c[100]; cout<<"Please enter a decimal: "; cin>>num; cout<<"\nThe Decimal number "<<num<<" converts to the Hexadecimal number: "; for(counter=0;num!=0;counter++) { a=num%16; hex[counter]=a; num=num/16; } for(x=counter-1;x>=0;x--) { if(hex[x]==10) { cout<<"A"; } else if(hex[x]==11) { cout<<"B"; } else if(hex[x]==12) { cout<<"C"; } else if(hex[x]==13) { cout<<"D"; } else if(hex[x]==14) { cout<<"E"; } else if(hex[x]==15) { cout<<"F"; } else { cout<<hex[x]; } } }

Thank you, hope this helps.

Related Questions

Amity University-Noida B.Tech...
Apply
Among top 100 Universities Globally in the Times Higher Education (THE) Interdisciplinary Science Rankings 2026
Amity University-Noida MBA Ad...
Apply
Ranked among top 10 B-Schools in India by multiple publications | Top Recruiters-Google, MicKinsey, Amazon, BCG & many more.
Narayana Business School MBA/...
Apply
Top 30 Private B-Schools Nationally and Top 3 in Gujarat | Highest CTC- 20 LPA | Average CTC- 8.4 LPA | 40% Pre-Placement Offers | 670+ Recruiters 
JAGSoM PGDM Admissions 2026
Apply
Highest CTC 51.38 LPA | Median CTC 10.32 LPA | Top 25% Average CTC 14.32 LPA
IFMR Graduate School of Busin...
Apply
Application Deadline 15th Jan’26 | UGC Approved Programs | Near 100% Placement Record | Up to 100% Scholarships | Highest CTC 21.32 LPA
Amity University-Noida Law Ad...
Apply
Among top 100 Universities Globally in the Times Higher Education (THE) Interdisciplinary Science Rankings 2026
View All Application Forms

Download the Careers360 App on your Android phone

Regular exam updates, QnA, Predictors, College Applications & E-books now on your Mobile

150M+ Students
30,000+ Colleges
500+ Exams
1500+ E-books