e1 - if(carry==1 z.data[i]=1 z.top=i return z void main int...

Info icon This preview shows pages 1–4. Sign up to view the full content.

View Full Document Right Arrow Icon
#include<stdio.h> #include<conio.h> #include<math.h> struct stack {  int data[10];  int top; }; void dectobin(int z,struct stack *p) {  int x;  x=z;  p->top=-1;  do  {   (p->top)++;   p->data[p->top]=x%2;   x=x/2;  }while(x>0); } void display(struct stack *p) {  int z;  z=p->top;  while(z>=0)  {   printf("%d",p->data[z]);   z--;  } } int bintodec(struct stack *p) {  int i,x=0;  for(i=0;i<=p->top;i++)
Image of page 1

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full Document Right Arrow Icon
 {   x=x+(p->data[i]*pow(2,i));  }  return x; } struct stack add(struct stack *p,struct stack *q) {  struct stack z;  int carry=0,i,max,t1,t2;  z.top=-1;  if(p->top>q->top)  max=p->top;  else  max=q->top;  for(i=0;i<=max;i++)  {   if(i<=p->top)   t1=p->data[i];   else   t1=0;   if(i<=q->top)   t2=q->data[i];   else   t2=0;   z.data[i]=(t1+t2+carry)%2;   (z.top)++;   if(t1+t2+carry<2)   carry=0;
Image of page 2
  else   carry=1;  }
Image of page 3

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full Document Right Arrow Icon
Image of page 4
This is the end of the preview. Sign up to access the rest of the document.

Unformatted text preview: if(carry==1) { z.data[i]=1; z.top=i; } return z; } void main() { int a,b,c,x; struct stack a1,b1,c1; clrscr(); printf("Enter the two decimal nos"); scanf("%d%d",&a,&b); dectobin(a,&a1); dectobin(b,&b1); printf("The first no in binary is "); display(&a1); printf("\nThe second no in binary is "); display(&b1); c1=add(&a1,&b1); printf("\nThe Addition of the Binary nos is"); display(&c1); c=bintodec(&c1); printf("\nThe addition of the decimal nos is %d",c); getch(); } OUTPUT: Enter the two decimal nos 512 511 The first no in binary is 1000000000 The second no in binary is 111111111 The Addition of the Binary nos is1111111111 The addition of the decimal nos is 1023...
View Full Document

{[ snackBarMessage ]}

What students are saying

  • Left Quote Icon

    As a current student on this bumpy collegiate pathway, I stumbled upon Course Hero, where I can find study resources for nearly all my courses, get online help from tutors 24/7, and even share my old projects, papers, and lecture notes with other students.

    Student Picture

    Kiran Temple University Fox School of Business ‘17, Course Hero Intern

  • Left Quote Icon

    I cannot even describe how much Course Hero helped me this summer. It’s truly become something I can always rely on and help me. In the end, I was not only able to survive summer classes, but I was able to thrive thanks to Course Hero.

    Student Picture

    Dana University of Pennsylvania ‘17, Course Hero Intern

  • Left Quote Icon

    The ability to access any university’s resources through Course Hero proved invaluable in my case. I was behind on Tulane coursework and actually used UCLA’s materials to help me move forward and get everything together on time.

    Student Picture

    Jill Tulane University ‘16, Course Hero Intern