Upcoming SlideShare
×

Thanks for flagging this SlideShare!

Oops! An error has occurred.

×
Saving this for later? Get the SlideShare app to save on your phone or tablet. Read anywhere, anytime – even offline.
Standard text messaging rates apply

# Decimal to binary and others using stacks

271

Published on

Data Structure Programs for Conversion

Data Structure Programs for Conversion

Published in: Education, Technology
0 Likes
Statistics
Notes
• Full Name
Comment goes here.

Are you sure you want to Yes No
• Be the first to comment

• Be the first to like this

Views
Total Views
271
On Slideshare
0
From Embeds
0
Number of Embeds
0
Actions
Shares
0
11
0
Likes
0
Embeds 0
No embeds

No notes for slide

### Transcript

• 1. 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 decimaln"); scanf("%d", &decimal); while(decimal!=0) { push(stack , decimal%2); decimal = decimal / 2; } display(stack); getch(); return 0; } void push (int stack[], int item) {
• 2. if(top==4) { check = 0; } else { check= 1; top++; stack[top] = item; } } void display(int stack[]) { inti; printf("nthe 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]);
• 3. } 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];
• 4. printf("Enter a number in decimaln"); 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; } }
• 5. 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;
• 6. case 13: printf("D "); break; case 14: printf("E "); break; case 15: printf("F "); break; default: printf("%d ",stack[i]); break; } } printf("n"); } }
• 7. 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 decimaln"); scanf("%d", &decimal); while(decimal!=0) { push(stack , decimal%8); decimal = decimal / 8; } display(stack); getch(); return 0; } void push (int stack[], int item) {
• 8. 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]);
• 9. } 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; inti=0, sum= 0, bit; int stack[size]; printf("enter a binary number: "); scanf("%d", &binary);
• 10. 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) {
• 11. if(top==4) { status = 0; } else { status = 1; top++; stack[top] = item; } } void display(int stack[]) { inti; printf("nthe stack is: n"); if (top==1) { printf("empty.n"); } else { for(i=top; i>=0; i--) {
• 12. 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)
• 13. { 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
• 14. { check = 1; top++; stack[top] = item; } } void display(int stack[]) { inti; printf("nthe stack is: n"); if (top==1) { printf("empty.n"); } else { for(i=top; i>=0; i--) { printf("%d",stack[i]); } printf("n"); } }
• 15. COMSATS Institute of Information Technology Lahore Assignment Subject: Data Structure & Algorithms Submitted To: Sana Rizwan Submitted By: M Mutasadiq Iqbal Registration No: DDp-Fa12-Bse-057 Section: C