I am having some trouble with a gcd program that I have created. My problem is that my output is not formatted correctly, and I believe that I need to use arrays to print it out correctly. THe format for the output is:
Num1 Num2 GCD
Num1 Num2 GCD
for how ever many sets the user specifies.
How would I impliment this?

import java.util.Scanner;

public class GCDcalc{

    private static int theGCD(int num1, int num2){
        while(num2 >0){
            int gcd = num1 % num2;
            num1 = num2;
            num2 = gcd;
            }
        return num1;
    }

    public static void main(String args[]){

        Scanner input = new Scanner(System.in);
        System.out.println("How many sets would you like to compute? ");
        int sets = input.nextInt();
        for(int i=0; i<sets; i++){
            System.out.println("Please enter the first number: ");
            int num1 = input.nextInt();
            System.out.println("Please enter the second number: ");
            int num2 = input.nextInt();
        System.out.println("Number 1: " + num1 + " Number 2: " + num2 + " GCD: " + theGCD(num1, num2));
            }
        }
    }

Though the above link is helpful in formatting the output, it is not quite what I am looking for. I am looking to store the values from each iteration of sets (num1, num2, gcd) and print them all out at the same time after the last num value is entered.
Currently the output that I am getting is:

How many sets would you like to compute?
2
Please enter the first number:
5
Please enter the second number:
10
Number 1: 5 Number 2: 10 GCD: 5
Please enter the first number:
9
Please enter the second number:
27
Number 1: 9 Number 2: 27 GCD: 9

Then you may simply create 3 arrays for each set? That's the easiest way to store all values first, then display all of them at the same time later.

This article has been dead for over six months. Start a new discussion instead.