Add two stack by array type by R4R Team


program-

#include< stdio.h>
#include< conio.h>
#define max 50
int stack1[max],top1=-1;
int stack2[max],top2=-1;

void create1()
{
char ch;
do
{
top1++;
printf("Enter Number\n");
scanf("%d",&stack1[top1]);
printf("Continue(y/n)\n");
ch=getch();
}while(ch=='y');
}

void create2()
{
char ch;
do
{
top2++;
printf("Enter Number\n");
scanf("%d",&stack2[top2]);
printf("Continue(y/n)\n");
ch=getch();
}while(ch=='y');
}

void traverse1()
{
int i;
printf("First Stack is :\n");
for(i=top1;i>=0;i--){
printf("%d ",stack1[i]);
}
}

void traverse2()
{
int i;
printf("Second Stack is :\n");
for(i=top2;i>=0;i--)
{
printf("%d ",stack2[i]);
}
}

void sum()
{
int i;
if(top1!=top2)
{
printf("Length of stack is not same\n");
return;
}
printf("Sum is :\n");
for(i=top1;i>=0;i--)
{
printf("%d ",stack1[i]+stack2[i]);
}
}

void main()
{
clrscr();
printf("---Input in First Stack---\n");
create1();
printf("---Input in Second Stack---\n");
create2();
traverse1();
traverse2();
sum();
getch();
}


output-

---Input in First Stack---
Enter Number
2
Continue(y/n)
Enter Number
1
Continue(y/n)
Enter Number
6
Continue(y/n)
Enter Number
3
Continue(y/n)
---Input in Second Stack---
Enter Number
1
Continue(y/n)
Enter Number
3
Continue(y/n)
Enter Number
2
Continue(y/n)
Enter Number
1
Continue(y/n)
First stack is :
3 6 1 2
Second Stack is :
1 2 3 1
Sum is :
4 8 4 3




Leave a Comment:
Search
R4R Team
R4Rin Top Tutorials are Core Java,Hibernate ,Spring,Sturts.The content on R4R.in website is done by expert team not only with the help of books but along with the strong professional knowledge in all context like coding,designing, marketing,etc!