Adding large no using stack....

Hi again i am trying to make a program that will take 2 nos in stack and add them up no matter how much big they are till now i am successful in making a program that adds 2 numbers having same number of digits and after adding two nos it will not generate any carry now i am trying to overcome that the program that worked in adding nos without carry and same digits the prior program is
#include<conio.h>
#include<stdio.h>
#define max 5

char stack[max],stack2[max],stack3[max],top=-1,top2=-1;
char b[max];
int i;

void push()
{
	++top;
	stack[top]=b[i];	
}
void push2()
{	
	++top2;
	stack2[top2]=b[i];	
}

void displayst()
{
	int l;
	for(l=0;l<max;l++)
	{
		printf("%c",stack[l]);
			
	}
	printf("\n");
	for(l=0;l<max;l++)
	{
		printf("%c",stack2[l]);
		if (l==max-1)
		{
			printf("\n    +\n-----\n");
		}
		
	}
	

}
void add()
{
	int i,s1,s2,car;
	for(i=0;i<max;i++)
		{
		    s1=stack[i]-48;
			s2=stack2[i]-48;
			stack3[i]=s1+s2;
		}
	int p;
	for(p=0;p<max;p++)
	{
		printf("%c",stack3[p]+48);
		
	}
	printf("\n-----");	
	
}

int main()
{
	int ch;
	do{
	printf("\nEnter choice \n1.Input Numbers\n2.Add\n3.exit\n");
	scanf("%d",&ch);
	if (ch==1)
	{
		
		fflush(stdin);
		printf("Enter 5 Digit number to be added\n");
		for (i=0;i<max;i++)
		{
  		 scanf("%c",&b[i]);
		   push();
		   
		}
		printf("Enter second 5 Digit number to be added\n");
		fflush(stdin);
		for (i=0;i<max;i++)
		{
  		 scanf("%c",&b[i]);
		   push2();
		   
		}
		
	}
	else if(ch==2)
	{
		displayst();
		add();
		top=-1;
		top2=-1;
	}
	else
	printf("\nExiting");
	}while(ch!=3);
	printf("\n");
}




Now the program i am working right now
#include<conio.h>
#include<stdio.h>
#include<string.h>
#define max 50

char stack[max],stack2[max],stack3[max],top=-1,top2=-1;
char b[max];
int i,s1ch,s2ch,s3ch;
void push()
{
	top++;
	stack[top]=b[i];	
}
void push2()
{	
	top2++;
	stack2[top2]=b[i];	
}

void displayst()
{
	int l;
	for(l=0;l<s1ch;l++)
	{
		printf("%c",stack[l]);
			
	}
	printf("\n");
	for(l=0;l<s1ch;l++)
	{
		printf("%c",stack2[l]);
	}
	

}
void add()
{
	displayst();
	int i,s1,s2,car;
	for(i=0;i<5;i++)
		{
		    s1=stack[i]-48;
			s2=stack2[i]-48;
			stack3[i]=s1+s2;
		}
	int p;
	for(p=0;p<5;p++)
	{
		printf("%d",stack3[p]+48);
		
	}
	printf("\n-----");	
	
}

int main()
{
	int ch;
	do{
	printf("\nEnter choice \n1.Input Numbers\n2.Add\n3.exit\n");
	scanf("%d",&ch);
	if (ch==1)
	{
		
		fflush(stdin);
		printf("Enter 1st number to be added\n");
		gets(stack);
		s1ch=strlen(stack);
		for (i=0;i<s1ch;i++)
		{
		   push();
		   
		}
		fflush(stdin);
		printf("Enter 2nd Digit number to be added\n");
		
		gets(stack2);
		for (i=0;i<s1ch;i++)
		{
		   push2();
		   
		}
		displayst();
		
	}
	else if(ch==2)
	{
		displayst();
		add();
		top=-1;
		top2=-1;
	}
	else
	printf("\nExiting");
	}while(ch!=3);
	printf("\n");
}

it is on the principle of the first one but it is printing garbage i dont know why any suggestions and help will be appreciated thank you all in advance for being such helpful members......

Comments

  • The first program seems to work fine for the most part, but when you mention the word nos what are you talking about. I have Google it and all I get is some kind of energy drink. That is not what you are talking about am I right. As for the second program yes that is garbage. Now how to fix it is the real question.
  • The first program seems to work fine for the most part, but when you mention the word nos what are you talking about. I have Google it and all I get is some kind of energy drink. That is not what you are talking about am I right. As for the second program yes that is garbage. Now how to fix it is the real question.
    actually i have solved that issue and right now 70% program is completed....
    i will be completing it tonight

    this one can add two numbers of unequal length but first should be greater and carry is enabled to very much extent......
    #include<conio.h>
    #include<stdio.h>
    #include<string.h>
    #define max 50
    
    char stack[max],stack2[max],stack3[max],top=-1,top2=-1;
    int space,i,chst1,chst2,chst3;
    
    void push()
    {
    	++top;
    		
    }
    void push2()
    {	
    	++top2;
    		
    }
    
    void displayst()
    {
    	int l;
    	for(l=0;l<chst1;l++)
    	{
    		printf("%c",stack[l]);
    			
    	}
    	printf("\n");
    	printf("    ");
    	for(l=0;l<chst2;l++)
    	{
    		printf("%c",stack2[l]);
    		
    		
    	}
    	
    
    }
    void add()
    {
    	int onetime,a,b,ones,tens,p,diff,temp,i,s1,s2,car,put,twodigit;
    	printf("\n                                   %d%d\n",chst1,chst2);
    	if (chst1>chst2)
    	{
    		diff=chst1-chst2;
    		temp=diff;
    		space=diff;
    		a=chst1;
    		b=chst2;
    				
    		for(car=0,i=0;i<=chst1;i++)
    			{
    			    s1=stack[a-1]-48;
    				s2=stack2[b-1]-48;
    				if(s1+s2>9)
    				{
    					
    					temp=s1+s2+car;
    					twodigit=temp;
    				        ones=twodigit%10;
    					twodigit=twodigit/10;
    					tens=twodigit;
    					stack3[a-1]=ones;
    					car=tens;
    					a--;
    					b--;
    					
    				}
    				else
    				{
    					if (s1+s2+car>9)
    					{
    						temp=s1+s2+car;
    						twodigit=temp;
    				    	        ones=twodigit%10;
    						twodigit=twodigit/10;
    						tens=twodigit;
    						stack3[a-1]=ones;
    						a--;
    						b--;
    						car=tens;
    					}
    					else
    					{
    						stack3[a-1]=s1+s2+car;
    						a--;
    						b--;
    						car=0;
    					}
    				}
    			}
    		for(i=0;i<diff;i++)
    		{
    			s1=stack[i]-48;
    			stack3[i]=s1;
    			
    		}
    		
    	
    	//else if
    	//for(i=0;i<chst1;i++)
    	//		{
    	//		    s1=stack[i]-48;
    	//			s2=stack2[i]-48;
    	//			stack3[i]=s1+s2;
    	//		}
    	
    	printf("\n-----\n");
    	for(p=0;p<chst1;p++)
    	{
    		printf("%*c",diff,stack3[p]+48);
    		
    	}
    	printf("\n-----");	
    	}
    }
    
    int main()
    {
    	int ch;
    	do{
    	printf("\nEnter choice \n1.Input Numbers\n2.Add\n3.exit\n");
    	scanf("%d",&ch);
    	if (ch==1)
    	{
    		
    		fflush(stdin);
    		printf("Enter 5 Digit number to be added\n");
    		gets(stack);
    		chst1=strlen(stack);
    		
    		for (i=0;i<chst1;i++)
    		{
      		 
    		   push();
    		   
    		}
    		fflush(stdin);
    		printf("Enter second 5 Digit number to be added\n");
    		
    		gets(stack2);
    		chst2=strlen(stack2);
    		for (i=0;i<chst2;i++)
    		{
      		 
    		   push2();
    		   
    		}
    		
    	}
    	else if(ch==2)
    	{
    		displayst();
    		add();
    		top=-1;
    		top2=-1;
    	}
    	else
    	printf("\nExiting");
    	}while(ch!=3);
    	printf("\n");
    }
    


    any suggestions on this one....

    once i complete i will post it here.....
    thanks for the help.......
  • #include<conio.h>
    #include<stdio.h>
    #include<string.h>
    int big;
    char a[40],b[40],ak[40],bk[40],ck[40];
    void add()
    {
    	int i,twodigit,ones,tens,temp,s1,s2,car,a,b;
    	a=big;
    	b=big;
    	
    	for(car=0,i=0;i<big;i++)
    			{
    			    s1=ak[a-1]-48;
    				s2=bk[a-1]-48;
    				if(s1+s2>9)
    				{
    					
    					temp=s1+s2+car;
    					twodigit=temp;
    				    ones=twodigit%10;
    					twodigit=twodigit/10;
    					tens=twodigit;
    					ck[a-1]=ones;
    					car=tens;
    					a--;
    					
    					
    				}
    				else
    				{
    					if (s1+s2+car>9)
    					{
    						temp=s1+s2+car;
    						twodigit=temp;
    				    	ones=twodigit%10;
    						twodigit=twodigit/10;
    						tens=twodigit;
    						ck[a-1]=ones;
    						a--;
    						
    						car=tens;
    					}
    					else
    					{
    						ck[a-1]=s1+s2+car;
    						a--;
    					
    						car=0;
    					}
    				}
    			}
    			printf("");
    			for(i=0;i<big;i++)
    			{
    				
    				printf("%c",ck[i]+48);
    			}
    	
    
    }
    int main()
    {
    	
    	int i,a1,b1,diff,temp1,temp2;
    	printf("Input A press enter\nInput B press enter");
    	gets(a);
    	gets(b);
    	puts(a);
    	puts(b);
    	a1=strlen(a);
    	b1=strlen(b);
    	if(a1>b1)
    	big=a1;
    	else
    	big=b1;
    	//printf("                   %d  %d\n",a1,b1);
    	if(a1>b1)
    	{
    		diff=a1-b1;
    		temp1=a1;
    		temp2=b1;
    		for(i=0;i<b1;i++)
    		{
    			b[temp1-1]=b[temp2-1];
    			temp1--;
    			temp2--;
    		}	
    		for(i=0;i<diff;i++)
    		{
    			b[i]=0+48;	
    		}
    		printf("\n\n");
    		for(i=0;i<a1;i++)
    		{
    			printf("%c",a[i]);
    		}
    		printf("\n");
    		for(i=0;i<a1;i++)
    		{
    			
    			printf("%c",b[i]);
    		}
    	}
    	else if(b1>a1)
    	{
    		diff=b1-a1;
    		temp1=b1;
    		temp2=a1;
    		for(i=0;i<a1;i++)
    		{
    			a[temp1-1]=a[temp2-1];
    			temp1--;
    			temp2--;
    		}	
    		for(i=0;i<diff;i++)
    		{
    			a[i]=0+48;	
    		}
    		printf("\n\n");
    		for(i=0;i<b1;i++)
    		{
    			printf("%c",b[i]);
    		}
    		printf("\n");
    		for(i=0;i<b1;i++)
    		{
    			
    			printf("%c",a[i]);
    		}
    		
    	}
    	else
    	{
    		printf("\n\n");
    		for(i=0;i<b1;i++)
    		{
    			printf("%c",b[i]);
    		}
    		printf("\n");
    		for(i=0;i<b1;i++)
    		{
    			
    			printf("%c",a[i]);
    		}
    		
    	}
    	
    	for (i=1;i<big+1;i++)
    	{
    		ak[i]=a[i-1];
    		bk[i]=b[i-1];
    	}
    	ak[0]=0+48;
    	bk[0]=0+48;
    	printf("\n\n\n");
    	for(i=0;i<=big;i++)
    		{
    			printf("%c",bk[i]);
    		}
    		printf("\n");
    		for(i=0;i<=big;i++)
    		{
    			
    			printf("%c",ak[i]);
    		}
    		big=big+1;
    	printf("\nbig is =%d\n",big);
    	printf("ak=%c\nbk=%c  lastvalues",ak[big-1],bk[big-1]);
    	printf("\n\n\n\n\n\n\n");
    	add();
    	
    }
    
    i did it finally i did it try it @smj....
  • What is it?

    program for adding two numbers outside limit in C you have ran the first one and still saying what is this ?????? didn't you read the title before replying my first post????
    anyways its done ........
  • Here is the command prompt after I run your latest version of your adding program. I realize it adds, but it is a bit confusing to look at. Why do you insist on using conio.h if you are not using any functions from the header file?
  • Here is the command prompt after I run your latest version of your adding program. I realize it adds, but it is a bit confusing to look at. Why do you insist on using conio.h if you are not using any functions from the header file?

    actually i intend to use getch() but i did not made my program presentable right now yeah its a bit confusing becoz i was continuously checking what values are assigning to the char variables i am using otherwise i would be playing bow and arrow in the dark :):)........it can add any amount of numbers i was also planning to assign pointer values to a and b so that i can free them later to make my program a less consumer of memory.........i also think i should check the add function again as if used extra times .......


    big is the length of the two char variables...... ak and bk are the final char variables that are holding the values that are to be added with a zero before them so that if one new character is generated it can also be placed there......
Sign In or Register to comment.