-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
105a9c7
commit c02f8c5
Showing
1 changed file
with
59 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,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); | ||
} | ||
} | ||
} |