Advertisement
klasscho

Untitled

Nov 2nd, 2019
121
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.00 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 void CheckTwo(int num, int den) {
  25. int min;
  26. if (den > num) {
  27. min = num;
  28. } else {
  29. min = den;
  30. }
  31. for (int l = min; l > 2; l--)
  32. {
  33. if (num % l == 0 && den % l == 0) {
  34. num = num / l;
  35. den = den / l;
  36. }
  37. }
  38. }
  39.  
  40. public static int fraction(int num, int den) {
  41. int mult;
  42. mult = num * den;
  43. return mult;
  44. }
  45.  
  46. public static void main(String[] args) {
  47. final int MaxInt = 46340;
  48. System.out.println("This program divides two irreducible fractions");
  49. System.out.println("Enter the value of the first numerator :");
  50. int m = CheckOne(MaxInt);
  51. System.out.println("Enter the value of the first denominator :");
  52. int n = CheckOne(MaxInt);
  53. System.out.println("Enter the value of the second numerator :");
  54. int p = CheckOne(MaxInt);
  55. System.out.println("Enter the value of the second denominator :");
  56. int q = CheckOne(MaxInt);
  57. CheckTwo(m, p);
  58. CheckTwo(q, n);
  59. int num1 = fraction(m, q);
  60. int den1 = fraction(n, p);
  61. System.out.println("Received fraction : " + num1 + " / " + den1 + ".");
  62. }
  63. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement