klasscho

Untitled

Nov 3rd, 2019
63
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.14 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. Reduct [0] = num;
  41. Reduct [1] = den;
  42. return Reduct;
  43. 2
  44. }
  45.  
  46. public static int fraction(int num, int den) {
  47. int mult;
  48. mult = num * den;
  49. return mult;
  50. }
  51.  
  52. public static void main(String[] args) {
  53. final int MaxInt = 46340;
  54. System.out.println("This program divides two irreducible fractions");
  55. System.out.println("Enter the value of the first numerator :");
  56. int m = CheckOne(MaxInt);
  57. System.out.println("Enter the value of the first denominator :");
  58. int n = CheckOne(MaxInt);
  59. System.out.println("Enter the value of the second numerator :");
  60. int p = CheckOne(MaxInt);
  61. System.out.println("Enter the value of the second denominator :");
  62. int q = CheckOne(MaxInt);
  63. CheckTwo(m, p);
  64. CheckTwo(q, n);
  65. int num1 = fraction(m, q);
  66. int den1 = fraction(n, p);
  67. System.out.println("Received fraction : " + num1 + " / " + den1 + ".");
  68. }
  69. }
Add Comment
Please, Sign In to add comment