Decimal to binary and others using stacks
-
Upload
mutasadiq-iqbal -
Category
Education
-
view
229 -
download
1
description
Transcript of 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)
{
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]);
}
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];
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;
}
}
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;
case 13:
printf("D ");
break;
case 14:
printf("E ");
break;
case 15:
printf("F ");
break;
default:
printf("%d ",stack[i]);
break;
}
}
printf("\n");
}
}
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)
{
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]);
}
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);
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)
{
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--)
{
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)
{
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
{
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");
}
}
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