Given a decimal number as input, you are asked to write a program in C++ to convert the given decimal number into equivalent Hexadecimal number. Convert the number with base value 10 (Decimal) to base value 16 (Hexadecimal). The base value of a number system determines the number of digits used to represent a numeric value. For example, the binary number system uses two digits 0 and 1, Hexadecimal number system uses 16 digits from 0-9, A, B, C, D, E, and F and decimal number system uses 10 digits 0-9 to represent any numeric value. using this code SAMPLE Sample Input: Enter a decimal value (Base-ten): 245 Sample Output: Decimal value = 245 Hexadecimal value = F5 Programmer name:………….. Algorithm: If the given decimal number is 245. Step 1: Remainder when 245 is divided by 16 is 5. Push the value 5 in the stack. Step 2: Divide 245 by 15. New number is 245/16 = 15. Step 3: Remainder when 15 is divided by 16 is 15. Push the value 15 in the stack. Step 4: Divide 15 by 16. New number is 15/16 = 0. Step 5: Since number becomes = 0. Stop repeating the steps and Pop the items from the stack and display until the stack is EMPTY. To display the Hexadecimal number While (Stack Not Empty) Pop the item from stack and display Continue Sample Input 2: Enter a decimal value (Base-ten): 1243 Sample Output 2: Decimal value = 1243 Hexadecimal value = 4DB Programmer name:………….. Sample Input 3: Enter a decimal value (Base-ten): 1000 Sample Output 3: Decimal value = 1000 Hexadecimal value = 3E8 /*---------------Stack.h----------------------------------- This header file defines a stack data type. Basic operations: constructor: Constructs an empty stack empty: Check if the stack is empty push: Modifies a stack by adding a value at the top pop: Remove an element from top of the stack */ #ifndef STACK_H #define STACK_H using namespace std; const int STACK_CAPACITY =10; typedef int StackElement; class Stack { private: /******* Data Members **************/ StackElement myArray[ STACK_CAPACITY]; int myTop; public: /******* Function Members **************/ Stack(); //Constructor - An empty stack has been constructed. bool empty() const; //Check if the stack is empty. void push(const StackElement & value); //Adds an element to a stack. StackElement pop(); //Remove an element from top of the stack }; #endif ------------------------------------------------------------------------------------ /* This file implements Stack member functions. stack.cpp */ #include "Stack.h" #include using namespace std; //---------Definition of Stack constructor--------------- Stack::Stack() { //Default Constructor myTop = -1; } //---------Definition of empty()------------------------- bool Stack::empty() const { return (myTop == -1); } //---------Definition of push()-------------------------- void Stack::push(const StackElement & value) { if (myTop < STACK_CAPACITY -1) { ++myTop; myArray[myTop] = value; } else { cerr << "*** Stack full -- can't add new value Must increase value of STACK_CAPACITY in Stack.h ***\n"; exit(1); } } //---------Definition of pop()-------------------------- StackElement Stack::pop() { StackElement value; if (!empty()) { value = myArray[myTop]; myTop--; } else { cerr << "*** Stack is empty --can't remove a value ***\n"; exit(1); } return value; }
Given a decimal number as input, you are asked to write a program in C++ to convert the given decimal number into equivalent Hexadecimal number. Convert the number with base value 10 (Decimal) to base value 16 (Hexadecimal).
The base value of a number system determines the number of digits used to represent a numeric value. For example, the binary number system uses two digits 0 and 1, Hexadecimal number system uses 16 digits from 0-9, A, B, C, D, E, and F and decimal number system uses 10 digits 0-9 to represent any numeric value.
using this code
SAMPLE
Sample Input:
Enter a decimal value (Base-ten): 245
Sample Output:
Decimal value = 245
Hexadecimal value = F5
Programmer name:…………..
Algorithm:
If the given decimal number is 245.
Step 1: Remainder when 245 is divided by 16 is 5. Push the value 5 in the stack.
Step 2: Divide 245 by 15. New number is 245/16 = 15.
Step 3: Remainder when 15 is divided by 16 is 15. Push the value 15 in the stack.
Step 4: Divide 15 by 16. New number is 15/16 = 0.
Step 5: Since number becomes = 0. Stop repeating the steps and Pop the items from the stack and display until the stack is EMPTY.
To display the Hexadecimal number
While (Stack Not Empty)
Pop the item from stack and display
Continue
Sample Input 2:
Enter a decimal value (Base-ten): 1243
Sample Output 2:
Decimal value = 1243
Hexadecimal value = 4DB
Programmer name:…………..
Sample Input 3:
Enter a decimal value (Base-ten): 1000
Sample Output 3:
Decimal value = 1000
Hexadecimal value = 3E8
/*---------------Stack.h-----------------------------------
This header file defines a stack data type.
Basic operations:
constructor: Constructs an empty stack
empty: Check if the stack is empty
push: Modifies a stack by adding a value at the top
pop: Remove an element from top of the stack
*/
#ifndef STACK_H
#define STACK_H
using namespace std;
const int STACK_CAPACITY =10;
typedef int StackElement;
class Stack
{
private:
/******* Data Members **************/
StackElement myArray[ STACK_CAPACITY];
int myTop;
public:
/******* Function Members **************/
Stack(); //Constructor - An empty stack has been constructed.
bool empty() const; //Check if the stack is empty.
void push(const StackElement & value); //Adds an element to a stack.
StackElement pop(); //Remove an element from top of the stack
};
#endif
------------------------------------------------------------------------------------
/* This file implements Stack member functions. stack.cpp */
#include "Stack.h"
#include <iostream>
using namespace std;
//---------Definition of Stack constructor---------------
Stack::Stack()
{
//Default Constructor
myTop = -1;
}
//---------Definition of empty()-------------------------
bool Stack::empty() const
{
return (myTop == -1);
}
//---------Definition of push()--------------------------
void Stack::push(const StackElement & value)
{
if (myTop < STACK_CAPACITY -1)
{
++myTop;
myArray[myTop] = value;
}
else
{
cerr << "*** Stack full -- can't add new value Must increase value of STACK_CAPACITY in Stack.h ***\n";
exit(1);
}
}
//---------Definition of pop()--------------------------
StackElement Stack::pop()
{
StackElement value;
if (!empty())
{
value = myArray[myTop];
myTop--;
}
else
{
cerr << "*** Stack is empty --can't remove a value ***\n";
exit(1);
}
return value;
}
Trending now
This is a popular solution!
Step by step
Solved in 2 steps with 3 images