Advertisement
klasscho

Untitled

Nov 3rd, 2019
117
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.08 KB | None | 0 0
  1. package com.company;
  2. import java.util.Scanner;
  3. public class Main {
  4. public static int CheckOne(int Max) {
  5. boolean IsInCorrect = true;
  6. int InPut = 0;
  7. do {
  8. try {
  9. Scanner num = new Scanner(System.in);
  10. InPut = num.nextInt();
  11. if ((InPut < 0) || (InPut > Max)) {
  12. System.out.format("Your number must belong to the range of [%d..%d]!\n",
  13. 0, Max);
  14. } else
  15. IsInCorrect = false;
  16. } catch (Exception e) {
  17. System.out.println("Enter a correct value!");
  18. }
  19. }
  20. while (IsInCorrect);
  21. return InPut;
  22. }
  23.  
  24. public static int[] CheckTwo(int num, int den) {
  25. int min;
  26. int[] Reduct;
  27. Reduct = new int [2];
  28. if (den > num) {
  29. min = num;
  30. } else {
  31. min = den;
  32. }
  33. for (int l = min; l > 2; l--)
  34. {
  35. if (num % l == 0 && den % l == 0) {
  36. num = num / l;
  37. den = den / l;
  38. }
  39. }
  40. return Reduct;
  41. }
  42.  
  43. public static int fraction(int num, int den) {
  44. int mult;
  45. mult = num * den;
  46. return mult;
  47. }
  48.  
  49. public static void main(String[] args) {
  50. final int MaxInt = 46340;
  51. System.out.println("This program divides two irreducible fractions");
  52. System.out.println("Enter the value of the first numerator :");
  53. int m = CheckOne(MaxInt);
  54. System.out.println("Enter the value of the first denominator :");
  55. int n = CheckOne(MaxInt);
  56. System.out.println("Enter the value of the second numerator :");
  57. int p = CheckOne(MaxInt);
  58. System.out.println("Enter the value of the second denominator :");
  59. int q = CheckOne(MaxInt);
  60. CheckTwo(m, p);
  61. CheckTwo(q, n);
  62. int num1 = fraction(m, q);
  63. int den1 = fraction(n, p);
  64. System.out.println("Received fraction : " + num1 + " / " + den1 + ".");
  65. }
  66. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement