-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsumofarrayelements.java
59 lines (53 loc) · 1.17 KB
/
sumofarrayelements.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
//PRINTING SUM OF TWO ARRAY ELEMENTS
//arr1[0]+arr2[0]. , ..... and also taking carry for the next array from previous one if any
import java.util.*;
public class Program
{
public static void main(String[] args)
{
Scanner scn=new Scanner (System.in);
//receiving both the arrays
int n1=scn.nextInt();
int [] a1=new int[n1];
for(int i=0;i<a1.length;i++)
{
a1[i]=scn.nextInt();
}
int n2=scn.nextInt();
int [] a2=new int[n2];
for(int i=0;i<a2.length;i++)
{
a2[i]=scn.nextInt();
}
int []sum=new int[n1>n2?n1:n2];
int c=0;
int i=a1.length-1;
int j=a2.length-1;
int k=sum.length-1;
while(k>=0)
{
int d=c;
if(i>=0){
d+=a1[i];
}
if(j>=0)
{
d+=a2[j];
}
c=d/10;
d=d%10;
sum[k]=d;
i--;
j--;
k--;
}
if(c!=0)
{
System.out.println(c);
}
for(int val:sum)
{
System.out.println(val);
}
}
}