I am not able to find the mistake in my code it is repeating the last digit

#include
using namespace std;
int Sum(int a[], int b[], int n, int m){
int sum[n];
int i = n - 1, j = m - 1, k = n - 1;
int c = 0, s = 0,x=0;
while (j >= 0) {
s = a[i] + b[j] + c;
sum[k] = (s % 10);
c = s / 10;
k–;
i–;
j–;
}
while (i >= 0) {
s = a[i] + c;
sum[k] = (s % 10);
c = s / 10;
i–;
k–;
}
for (int i = 0; i <= n-1; i++) {
cout<<sum[i]<<", ";
x=sum[i];
}

return x;
}
int main(){
int a[1000];
int b[1000];
int n;
int m;
cin>>n;
for(int i=0;i<n;i++){
cin>>a[i];
}
cin>>m;
for(int j=0;j<m;j++){
cin>>b[j];
}

if (n >= m)
cout<<Sum(a, b, n, m);
else
cout<<Sum(b, a, m, n);
cout<<" END";
return 0;
}

Hey @apoorvsin

#include<iostream>
using namespace std;
int sum_of_arrays(int a[],int b[],int n,int m){
	int c=0,s=0,x=0;
	int i=n-1;
	int j=m-1;
	int k=n-1;
	int sum[n];
  while(j>=0){
	  s=a[i]+b[j]+c;
	  sum[k]=s%10;
	  c=s/10;
	  i--;
	  j--;
	  k--;
  }
  while(i>=0){
	  s=a[i]+c;
	  sum[k]=s%10;
	  c=s/10;
	  i--;
	  k--;
  }
  if(c!=0)cout<<c<<", "; //say we add 9 + 9 then its 18  so we print 1 
  for(int i=0;i<n;i++){
	  cout<<sum[i]<<", ";
	  x=sum[i];
  }
  return x;
}
int main() {
	int n,m;
	cin>>n;
	int a[1000];
	int b[1000];
	for(int i=0;i<n;i++){
		cin>>a[i];
	}
	cin>>m;
	for(int j=0;j<m;j++){
		cin>>b[j];
	}
	if(n>m){
	 sum_of_arrays(a,b,n,m);	//corrected this
	}
   else
   	 sum_of_arrays(b,a,m,n);  	//corrected this

	 cout<<"END";//corrected this
	return 0;
}

I hope I’ve cleared your doubt. I ask you to please rate your experience here
Your feedback is very important. It helps us improve our platform and hence provide you
the learning experience you deserve.

On the off chance, you still have some questions or not find the answers satisfactory, you may reopen
the doubt.