Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package com.company;
- import java.util.Scanner;
- public class Main {
- public static int CheckOne(int Max) {
- boolean IsInCorrect = true;
- int InPut = 0;
- do {
- try {
- Scanner num = new Scanner(System.in);
- InPut = num.nextInt();
- if ((InPut < 0) || (InPut > Max)) {
- System.out.format("Your number must belong to the range of [%d..%d]!\n",
- 0, Max);
- } else
- IsInCorrect = false;
- } catch (Exception e) {
- System.out.println("Enter a correct value!");
- }
- }
- while (IsInCorrect);
- return InPut;
- }
- public static int[] CheckTwo(int num, int den) {
- int min;
- int[] Reduct;
- Reduct = new int [2];
- if (den > num) {
- min = num;
- } else {
- min = den;
- }
- for (int l = min; l > 2; l--)
- {
- if (num % l == 0 && den % l == 0) {
- num = num / l;
- den = den / l;
- }
- }
- return Reduct;
- }
- public static int fraction(int num, int den) {
- int mult;
- mult = num * den;
- return mult;
- }
- public static void main(String[] args) {
- final int MaxInt = 46340;
- System.out.println("This program divides two irreducible fractions");
- System.out.println("Enter the value of the first numerator :");
- int m = CheckOne(MaxInt);
- System.out.println("Enter the value of the first denominator :");
- int n = CheckOne(MaxInt);
- System.out.println("Enter the value of the second numerator :");
- int p = CheckOne(MaxInt);
- System.out.println("Enter the value of the second denominator :");
- int q = CheckOne(MaxInt);
- CheckTwo(m, p);
- CheckTwo(q, n);
- int num1 = fraction(m, q);
- int den1 = fraction(n, p);
- System.out.println("Received fraction : " + num1 + " / " + den1 + ".");
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement