Write a program that uses a stack to convert a number in bas

Write a program that uses a stack to convert a number in base 10 to a number in base 2.

Solution

Answer:

Here is the program in C :

#include<stdio.h>

int stack[10],

top = 0;

int pop()

{

return stack[--top];

}

void push(int n)

{

stack[top++] = n;

}

main()

{

int a, i;

printf(\"Enter value of base 10:\");

scanf(\"%d\", &a);

for (i = 0; a > 0; i++)

{

push(a % 2);

a = a / 2;

}

printf(\"The value with base 2 is:\ \");

while (top > 0)

{

printf(\"%d\", pop());

}

}

 Write a program that uses a stack to convert a number in base 10 to a number in base 2.SolutionAnswer: Here is the program in C : #include<stdio.h> int s

Get Help Now

Submit a Take Down Notice

Tutor
Tutor: Dr Jack
Most rated tutor on our site