'Subtracting two arrays (variables with decimals) [closed]
How to subtract the below two arrays in java?
(09.34, 09.56, 09.00, 08.55 )
(17.25, 18.06, 17.55, 16.00)
Solution 1:[1]
public class Substract{
public static void main(String args[]){
double a[]={09.34, 09.56, 09.00, 08.55 };
double b[]={17.25, 18.06, 17.55, 16.00};
double c[]=new double[a.length];
int i;
for(i=0;i<a.length;i++) {
c[i]=a[i]-b[i];
System.out.print(c[i] + " ");
}
}
}
Solution 2:[2]
class MainClass {
public static void main(String[] args) {
double[] d1 = {09.34, 09.56, 09.00, 08.55};
double[] d2 = {17.25, 18.06, 17.55, 16.00};
int operations = d1.length>d2.length? d2.length: d1.length;
int noOfElements = d1.length<d2.length? d2.length: d1.length;
double[] result = new double[noOfElements];
for (int x=0; x<operations; x++){
result[x] = d1[x] - d2[x];
System.out.println(result[x]);
}
}
}
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
Solution | Source |
---|---|
Solution 1 | ilw |
Solution 2 | D. Mateescu |