1/3+3/5+5/7 java
时间: 2023-12-12 09:29:05 浏览: 74
b659交通管理在线服务系统-springboot+vue.zip
To add the fractions 1/3, 3/5, and 5/7 in Java, you can use the following code:
```
int num1 = 1;
int den1 = 3;
int num2 = 3;
int den2 = 5;
int num3 = 5;
int den3 = 7;
int lcm1 = den1 * den2 / gcd(den1, den2);
int lcm2 = lcm1 * den3 / gcd(lcm1, den3);
int newNum1 = num1 * lcm1 / den1;
int newNum2 = num2 * lcm1 / den2;
int newNum3 = num3 * lcm2 / den3;
int sumNum = newNum1 + newNum2 + newNum3;
int sumDen = lcm2;
int gcd = gcd(sumNum, sumDen);
int finalNum = sumNum / gcd;
int finalDen = sumDen / gcd;
System.out.println(finalNum + "/" + finalDen);
```
This code first defines the fractions as separate numerators and denominators. It then calculates the least common multiple (LCM) of the denominators using a helper function called `gcd` to calculate the greatest common divisor (GCD) between two numbers. It then converts each fraction to have a common denominator and adds them together. Finally, it simplifies the resulting fraction by dividing both the numerator and denominator by their GCD and prints the result.
阅读全文