From ac09aac05aafd1b3014b3a4c95cb29b018e8f800 Mon Sep 17 00:00:00 2001 From: CMENG92 Date: Thu, 19 Oct 2023 11:06:14 -0700 Subject: [PATCH] Assignment 007 completed! GCF with a while loop. --- .idea/misc.xml | 2 +- src/MyMath.java | 38 +++++++++++++++++++++++++++++++++++--- 2 files changed, 36 insertions(+), 4 deletions(-) diff --git a/.idea/misc.xml b/.idea/misc.xml index d15472f..a346fd7 100644 --- a/.idea/misc.xml +++ b/.idea/misc.xml @@ -1,6 +1,6 @@ - + \ No newline at end of file diff --git a/src/MyMath.java b/src/MyMath.java index 7da9a29..5c83810 100644 --- a/src/MyMath.java +++ b/src/MyMath.java @@ -1,7 +1,39 @@ +/** + * + * @author Trevor Hartman + * @author Cameron Meng + * + * @since Version 1.0 + * + */ import java.util.Scanner; - public class MyMath { - + public static int gcf(int a, int b) { + while(b != 0){ + if(a > b){ + int c = b; + b = a % b; + a = c; + } else { + b = b % a; + } + } + return a; + } public static void main(String[] args) { + Scanner s = new Scanner(System.in); + String input1, input2 = null; + String message1 = "Give me a %snumber please (Q to quit): ", message2 = "Give me a second %snumber please (Q to quit): "; + System.out.print(String.format(message1, "")); + while(!(input1 = s.nextLine()).equals("Q")) { + if(input2 == null){ + input2 = input1; + System.out.print(String.format(message2, "")); + continue; + } + System.out.printf("GCF of %s and %s is %d%n", input1, input2, gcf(Integer.parseInt(input1), Integer.parseInt(input2))); + System.out.print(String.format(message1, "")); + input2 = null; + } } -} +} \ No newline at end of file