answersLogoWhite

0


Best Answer

ALGORITHM: function outputInBinary(Integer n) Stack s = new Stack while n > 0 do Integer bit = n modulo 2 s.push(bit) if s is fullthen return error end if n = floor(n / 2) end while while s is not empty dooutput(s.pop()) end while end function

User Avatar

Wiki User

11y ago
This answer is:
User Avatar
More answers
User Avatar

Wiki User

13y ago

void push (int value); /* you write it */

int pop (void); /* you write it */

int isempty (void); /* you write it */

int dec_to_bin (int n, char *to)

{

. do {

. . push (n%2);

. . n /= 2;

. } while (n);

. while (! isempty ()) {

. . *to++ = '0' + pop();

. }

. *to = '\0';

}

This answer is:
User Avatar

User Avatar

Wiki User

9y ago

#include

#include

#include

#include

unsigned input_num (std::string prompt)

{

unsigned num = 0;

while (1)

{

std::cout << prompt << ": ";

std::string input = "";

std::getline (std::cin, input);

std::stringstream ss (input);

if (ss >> num)

break;

std::cout << "Invalid input." << std::endl;

}

return (num);

}

std::string dec2bin (unsigned dec)

{

// create stack

std::stack stack;

while (dec)

{

// if bit 0 is set, push 1 else push 0

stack.push (dec & 0x1);

// bit-shift right by 1 bit

dec >>= 1;

}

// convert stack to a string

std::string bin = "";

while (!stack.empty())

{

// append '0' or '1' char

bin += stack.top() + '0';

stack.pop();

}

return bin;

}

int main()

{

while (unsigned num = input_num ("Enter a real number (0 to exit)"))

{

std::string bin = dec2bin (num);

std::cout << num << " in binary is ";

std::cout << bin << '\n' << std::endl;

}

std::cout << "\nExiting...\n" << std::endl;

}

Example Output

Enter a real number (0 to exit): 1

1 in binary is 1

Enter a real number (0 to exit): 2

2 in binary is 10

Enter a real number (0 to exit): 3

3 in binary is 11

Enter a real number (0 to exit): 4

4 in binary is 100

Enter a real number (0 to exit): 5

5 in binary is 101

Enter a real number (0 to exit): 0

Exiting...

This answer is:
User Avatar

User Avatar

Wiki User

9y ago

Two examples are provided here. The first makes use of the call stack while the second makes use of a local stack. Of the two methods, the latter is the more efficient.

Example 1: Call Stack Method

#include

#include

#include

std::string dec_to_bin(unsigned dec)

{

std::string result = "";

char c = (dec & 0x1) + '0';

if( dec >>= 1)

result += dec_to_bin(dec);

return result + c;

}

int main()

{

// Print the first 256 binary numbers.

for (unsigned num=0; num<256; ++num)

{

std::cout.fill(' ');

std::cout << std::setw(10) << num << " decimal is ";

std::cout.fill('0');

std::cout << std::setw(32) << dec_to_bin(num) << " binary\n";

}

}

Example 2: Local Stack Method

#include

#include

#include

#include

std::string dec_to_bin(unsigned dec)

{

std::string result = "";

std::stack stack;

do

{

stack.push ((dec & 0x1) + '0');

} while( dec >>= 1);

while (!stack.empty())

{

result += stack.top();

stack.pop();

}

return result;

}

int main()

{

// Print the first 256 binary numbers.

for (unsigned num=0; num<256; ++num)

{

std::cout.fill(' ');

std::cout << std::setw(10) << num << " decimal is ";

std::cout.fill('0');

std::cout << std::setw(32) << dec_to_bin(num) << " binary\n";

}

}

This answer is:
User Avatar

User Avatar

Wiki User

14y ago

How do you write a c program to convert decimal no to binary no using stack in c++

This answer is:
User Avatar

Add your answer:

Earn +20 pts
Q: How do you write a c program to convert binary to decimal using stack?
Write your answer...
Submit
Still have questions?
magnify glass
imp
Continue Learning about Engineering

How do you write a C program to convert a binary value to its octal equivalent?

To convert from binary to octal, bitwise AND the binary value with 0x8 (00000111 in binary) and push the value onto a stack. Right-shift (&gt;&gt;) the binary value by 3 bits and repeat until the binary value is zero. Pop the stack to build the left-to-right digits of the octal value. Using 10110100 as an example: 10110100 &amp; 00000111 = 00000100 10110100 &gt;&gt; 3 = 00010110 00010110 &amp; 00000111 = 00000110 00010110 &gt;&gt; 3 = 00000010 00000010 &amp; 00000111 = 00000010 00000010 &gt;&gt; 3 = 00000000 Popping the values in order reveals 00000010, 00000110 and 00000100 (decimal 2, 6 and 4 respectively). Thus 10110100 binary is 0264 octal.


Write a program to convert hexadecimal to decimal numbers?

write an assembly language program to implement a stack. START: LXI SP,STACK ;initialize stack pointer LXI H,BINBYT ;point HL index to where binary number is stored MOV A,M ;transfer byte LXI H,OUTBUF ;point HL index to output-buffer memory CALL BINBCD HLT BINBCD: MVI B,100 ;load 100 into register B (power of ten holding register) CALL BCD ;call conversion for BCD3 MVI B,10 ;load 10 into register B CALL BCD ;call conversion for BCD2 MOV M,A ;store BCD1 RET


Why must the stack pointer be initialized at the beginning of every program?

Because the stack pointer marks the top of the stack. If it is not initialised, it is not possible to determine where the next stack frame will go.


Stack c language program of palindrome?

what ever you assume


What is difference between stack pointer and program counter?

Both of them are pointers, but otherwise they are completely unrelated. The former points to the current position of the stack, the latter points to the current instruction of the program.

Related questions

Convert decimal number to binary number using stack?

becomes heavy because the ang decimal number ay marami kay sa sa stack ng tsinelas


C plus plus program that convert decimal to binary using the concept of stack?

#include&lt;stdio.h&gt; #include&lt;stdlib.h&gt; main() { int number,binary[10000],b=0; printf("Enter decimal number "); scanf("%d",&amp;number); printf("\nBinary: "); for(;number;number/=2,b++) binary[b]=number%2; for(b--;b&gt;-1;b--) printf("%d ",binary[b]); }


How do you write a C program to convert a binary value to its octal equivalent?

To convert from binary to octal, bitwise AND the binary value with 0x8 (00000111 in binary) and push the value onto a stack. Right-shift (&gt;&gt;) the binary value by 3 bits and repeat until the binary value is zero. Pop the stack to build the left-to-right digits of the octal value. Using 10110100 as an example: 10110100 &amp; 00000111 = 00000100 10110100 &gt;&gt; 3 = 00010110 00010110 &amp; 00000111 = 00000110 00010110 &gt;&gt; 3 = 00000010 00000010 &amp; 00000111 = 00000010 00000010 &gt;&gt; 3 = 00000000 Popping the values in order reveals 00000010, 00000110 and 00000100 (decimal 2, 6 and 4 respectively). Thus 10110100 binary is 0264 octal.


Write a program to convert hexadecimal to decimal numbers?

write an assembly language program to implement a stack. START: LXI SP,STACK ;initialize stack pointer LXI H,BINBYT ;point HL index to where binary number is stored MOV A,M ;transfer byte LXI H,OUTBUF ;point HL index to output-buffer memory CALL BINBCD HLT BINBCD: MVI B,100 ;load 100 into register B (power of ten holding register) CALL BCD ;call conversion for BCD3 MVI B,10 ;load 10 into register B CALL BCD ;call conversion for BCD2 MOV M,A ;store BCD1 RET


Write a program to convert stack into queue using c language?

In order to write a program to convert stack into queue using c language you must be able to identify the proper program. Having a special certification in programing will be beneficial as well to make sure you recognize the proper queues for the programs.


what are the sections of c program?

Well, the source program doesn't have sections, the binary format consist of parts like: read-only executable, read-only data, writeable data, stack, etc


What are the different sections of c program?

Well, the source program doesn't have sections, the binary format consist of parts like: read-only executable, read-only data, writeable data, stack, etc


What is the difference between stack pointer and program counter?

The stack pointer keeps track of the top of the stack used by the current thread. The program counter keeps track of the next instruction in a program. Both are registers and both store a memory address.


What is Stack overflow at line?

A stack overflow is a type of buffer overflow in which an array writes memory outside of the array boundaries. The keyword here is "stack". The stack is a section in memory in which local variables and other program data are kept for future reference. When the stack gets overflown, adjacent program memory, such as variables, pointers, etc, will be overwritten and cause your program to crash.


Stack program by using recursive method in c plus plus?

#include&lt;iostream&gt; #include&lt;iomanip&gt; #include&lt;string&gt; std::string dec_to_bin(unsigned dec) { std::string result = ""; char c = (dec &amp; 0x1) + '0'; // 'push' char onto call stack if( dec &gt;&gt;= 1) result += dec_to_bin(dec); // recurse return result + c; // 'pop' char from call stack } int main() { // Print the first 256 binary numbers. for (unsigned num=0; num&lt;256; ++num) { std::cout.fill(' '); std::cout &lt;&lt; std::setw(10) &lt;&lt; num &lt;&lt; " decimal is "; std::cout.fill('0'); std::cout &lt;&lt; std::setw(32) &lt;&lt; dec_to_bin(num) &lt;&lt; " binary\n"; } }


Why must the stack pointer be initialized at the beginning of every program?

Because the stack pointer marks the top of the stack. If it is not initialised, it is not possible to determine where the next stack frame will go.


What is the importance of stack algorithm in your program?

Stack implementations allow us to easily implement backtracking algorithms.