• + 2 comments

    i wrote it in c..its giving correct output in devcpp,but here it shows wrong output..

    #include<stdio.h>
    #include<string.h>
    void stringmul(char*,char*,char*);
    void stringadd(char*,char*,char*);
    void swap(char*,char*);
    void strdrev(char*);
    int main()
    {
    	char a[150],b[150];
    	char mul[150]="",add[150]="";
    	int l1,l2,i,l,n,j;
    	scanf("%s",a);
    	scanf("%s",b);
    	scanf("%d",&n);
    	for(i=2;i<n;i++)
    	{
    		memset(mul,'\0',150);
    		memset(add,'\0',150);
    	stringmul(b,b,mul);
    	stringadd(mul,a,add);
    	l1=strlen(mul);
    	//printf("a=%s b=%s mul=%s add=%s\n",a,b,mul,add);
    	for(j=0;j<l1;j++)
    	{
    		a[j]=b[j];
    		b[j]=add[j];
    	}
    	}
    printf("%s",add);
    	
    	
    	
    }
    void stringmul(char *a,char *b,char *mul)
    {
    	int i,j,l1,l2,c,c1,temp=0,k,temp1;
    		l1=strlen(a);
    	l2=strlen(b);
    	//printf("(%s,%s)",a,b);
    	strdrev(a);
    	//strdrev(b);
    	//printf("(%s,%s)",a,b);
    	for(i=0;i<l1;i++)
    	{
    		c=0;
    		k=0;
    		c1=0;
    		for(j=0;j<l2;j++)
    		{
    			
    			temp=(a[i]-'0')*(b[j]-'0')+c;
    			if(mul[i+j+k]==0)
    			temp1=(mul[i+j+k])+(temp%10)+c1;
    			else
    			temp1=(mul[i+j+k]-'0')+(temp%10)+c1;
    			mul[i+j+k]=(temp1%10)+'0';
    			c1=temp1/10;
    			c=temp/10;
    	
    			
    			
    		}
    	if(c1!=0||c!=0)
    	mul[i+j+k]=c1+c+'0';
    	k++;
    }
    strdrev(mul);
    	strdrev(a);
    }
    void stringadd(char *a,char *b,char *add)
    {
    	int i,l1,l2,c,temp=0,p,q;
    	l1=strlen(a);
    	l2=strlen(b);
    	strdrev(a);
    	strdrev(b);	
    	int l=l1>l2?l1:l2;
    	c=0;
    	for(i=0;i<l;i++)
    	{
    		temp=0;
    		if(i<l1)
    		temp=a[i]-'0';
    		if(i<l2)
    		temp=temp+b[i]-'0';
    		temp=temp+c;
    		add[i]=(temp%10)+'0';
    			c=temp/10;
    			
    			
    		}
    	if(c!=0)
    	add[i]=c+'0';
    strdrev(add);
    strdrev(a);
    strdrev(b);
    }
    void strdrev(char *a)
    {
    	int l=strlen(a),i=0;
    	l--;
    	while(i<l)
    	{
    		swap(&a[i],&a[l]);
    		i++;
    		l--;
    	}
    }
    void swap(char *a,char *b)
    {
    	char temp;
    	temp=*a;
    	*a=*b;
    	*b=temp;
    }
    
    • + 1 comment

      programing with the combination of codes and notes can always help. what is the meaning of the sentence like "a[i]-'0'"?

      • + 0 comments

        converting char a[i] to its decimal equivalent

    • + 1 comment

      same issue getting correct output in my IDE but segmentation fault while submittin... any suggestion?

      • + 0 comments

        Can you tell us which testcase is giving seg fault? You can just download that testcase and check yourself too. It must be an edge case or might be using enormous memory.