Write a program that will allow a user to multiply and divide pairs of integer f
ID: 3887597 • Letter: W
Question
Write a program that will allow a user to multiply and divide pairs of integer fractions and provide the resulting fraction and the decimal representation as a double with 2 decimal places. Complete the SADITM prior to coding.
I N P U T
Please enter a numerator: 2 Please enter a denominator (zero is not allowed): 3 Please enter a numerator: 1 Please enter a denominator (zero is not allowed): 2
Output
2/3 * 1/2: The resulting fraction is 2/6.
The decimal representation of 2/6 is 0.33
2/3 / 1/2: The resulting is fraction is 4/3.
The decimal representation of 4/3 is 1.33
pls can you solve this problem.
Explanation / Answer
Program in JAVA:-
import java.text.DecimalFormat;
import java.text.NumberFormat;
import java.util.Scanner;
public class DivisionDouble {
public static void main(String[] args) {
// TODO Auto-generated method stub
Scanner scanner=new Scanner(System.in);
System.out.println("Please enter a numerator:");
double num1=scanner.nextDouble();
System.out.println("Please enter a denominator (zero is not allowed):");
double denum1=scanner.nextDouble();
System.out.println("Please enter a numerator:");
double num2=scanner.nextDouble();
System.out.println("Please enter a denominator (zero is not allowed):");
double denum2=scanner.nextDouble();
double mulAns= (num1/denum1) * (num2/denum2);
double divAns=(num1/denum1) / (num2/denum2);
//Format the number for 2 decimal values.
NumberFormat formatter = new DecimalFormat("#0.00");
System.out.println(formatter.format(mulAns));
System.out.println(formatter.format(divAns));
}
}
OUTPUT:-
Please enter a numerator:
2
Please enter a denominator (zero is not allowed):
3
Please enter a numerator:
1
Please enter a denominator (zero is not allowed):
2
0.33
1.33
Note:- If you want this solution in another language let me know.
Thank you!
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.