Decimal to binary and others using stacks

18
DECIMAL TO BINARY #include<stdio.h> #include<conio.h> void push (int stack[], int item); void display(int stack[]); int top=1; int check; #define size 50 int main() { int decimal, i=0, bit; int stack[size]; printf ("Enter a number in decimal\n"); scanf ("%d", &decimal); while(decimal!=0) { push(stack , decimal%2); decimal = decimal / 2; } display(stack); getch (); return 0;

description

Data Structure Programs for Conversion

Transcript of Decimal to binary and others using stacks

Page 1: Decimal to binary and others using stacks

DECIMAL TO BINARY

#include<stdio.h>

#include<conio.h>

void push (int stack[], int item);

void display(int stack[]);

int top=1;

int check;

#define size 50

int main()

{

int decimal, i=0, bit;

int stack[size];

printf ("Enter a number in decimal\n");

scanf ("%d", &decimal);

while(decimal!=0)

{

push(stack , decimal%2);

decimal = decimal / 2;

}

display(stack);

getch ();

return 0;

}

void push (int stack[], int item)

{

Page 2: Decimal to binary and others using stacks

if(top==4)

{

check = 0;

}

else

{

check= 1;

top++;

stack[top] = item;

}

}

void display(int stack[])

{

int i;

printf ("\n the stack is: \n");

if (top==1)

{

printf("empty.\n");

}

else

{

for(i=top; i>=0; i--)

{

if(stack[i]==0 || stack[i]==1)

printf("%d ",stack[i]);

Page 3: Decimal to binary and others using stacks

}

printf("\n");

}

}

DECIMAL TO HEXADECIMAL

#include<stdio.h>

#include<conio.h>

void push (int stack[], int item);

void display(int stack[]);

int top=1;

int status;

#define size 30

int main()

{

int decimal;

int j=0, bit;

int stack[size];

Page 4: Decimal to binary and others using stacks

printf("Enter a number in decimal\n");

scanf("%d", &decimal);

while(decimal!=0)

{

push(stack , decimal%16);

decimal = decimal / 16;

}

display(stack);

getch();

return 0;

}

void push (int stack[], int item)

{

if(top==4)

{

status = 0;

}

else

{

status = 1;

top++;

stack[top] = item;

}

}

Page 5: Decimal to binary and others using stacks

void display(int stack[])

{

int j;

printf("\nthe stack is: \n");

if (top==1)

{

printf("empty.\n");

}

else

{

for(j=top; j>=0; j--)

{

switch(stack[j])

{

case 10:

printf("A ");

break;

case 11:

printf("B ");

break;

case 12:

printf("C ");

break;

Page 6: Decimal to binary and others using stacks

case 13:

printf("D ");

break;

case 14:

printf("E ");

break;

case 15:

printf("F ");

break;

default:

printf("%d ",stack[i]);

break;

}

}

printf("\n");

}

}

Page 7: Decimal to binary and others using stacks

DECIMAL TO OCTAL

#include<stdio.h>

#include<conio.h>

void push (int stack[], int item);

void display(int stack[]);

int top=1, check;

#define size 50

int main()

{

int decimal, k=0, bit;

int stack[size];

printf("Enter a number in decimal\n");

scanf("%d", &decimal);

while(decimal!=0)

{

push(stack , decimal%8);

decimal = decimal / 8;

}

display(stack);

getch();

return 0;

}

void push (int stack[], int item)

{

Page 8: Decimal to binary and others using stacks

if(top==4)

{

check = 0;

}

else

{

check= 1;

top++;

stack[top] = item;

}

}

void display(int stack[])

{

int k;

printf("\nthe stack is: \n");

if (top==1)

{

printf("empty.\n");

}

else

{

for(k=top; k>=0; k--)

{

printf("%d ",stack[k]);

Page 9: Decimal to binary and others using stacks

}

printf("\n");

}

}

BINARY TO DECIMAL

#include<stdio.h>

#include<conio.h>

#include<math.h>

void push (int stack[], int item);

void display(int stack[]);

int top=1, status;

#define size 100

int main()

{

int binary;

int i=0, sum= 0, bit;

int stack[size];

printf("enter a binary number: ");

scanf("%d", &binary);

Page 10: Decimal to binary and others using stacks

while(binary>0)

{

bit= binary%10;

if(bit!=0 || bit!=1)

{

printf("invalid values entered.");

break;

}

binary= binary/10;

sum= sum + bit * pow(2,i);

i++;

}

int temp;

while(sum!=0)

{

temp= sum % 10;

sum= sum/10;

push(stack, temp);

}

display(stack);

getch();

return 0;

}

void push (int stack[], int item)

{

Page 11: Decimal to binary and others using stacks

if(top==4)

{

status = 0;

}

else

{

status = 1;

top++;

stack[top] = item;

}

}

void display(int stack[])

{

int i;

printf("\nthe stack is: \n");

if (top==1)

{

printf("empty.\n");

}

else

{

for(i=top; i>=0; i--)

{

Page 12: Decimal to binary and others using stacks

printf("%d",stack[i]);

}

printf("\n");

}

}

OCTAL TO DECIMAL

#include<stdio.h>

#include<conio.h>

#include<math.h>

void push (int stack[], int item);

void display(int stack[]);

int top=1, check;

#define size 100

int main()

{

int binary;

int j=0, sum= 0, bit;

int stack[size];

printf("enter an octal number: ");

scanf("%d", &binary);

while(binary>0)

Page 13: Decimal to binary and others using stacks

{

bit= binary%10;

bin= binary/10;

sum= sum + bit * pow(8,j);

j++;

}

int temp;

while(sum!=0)

{

temp= sum % 10;

sum= sum/10;

push(stack, temp);

}

display(stack);

getch();

return 0;

}

void push (int stack[], int item)

{

if(top==4)

{

check = 0;

}

else

Page 14: Decimal to binary and others using stacks

{

check = 1;

top++;

stack[top] = item;

}

}

void display(int stack[])

{

int i;

printf("\nthe stack is: \n");

if (top==1)

{

printf("empty.\n");

}

else

{

for(i=top; i>=0; i--)

{

printf("%d",stack[i]);

}

printf("\n");

}

}

Page 15: Decimal to binary and others using stacks

COMSATS Institute of Information Technology Lahore

AssignmentSubject: Data Structure & Algorithms Submitted To: Sana Rizwan Submitted By: M Mutasadiq Iqbal

Registration No: DDp-Fa12-Bse-057Section: C