usingMaths.com
From Theory to Practice - Math You Can Use.







<< PreviousNext >>

Java Program to Calculate HCF (GCD) with User Input - Kids Fun Project



Understanding the HCF Algorithm in Java

This Java program calculates the HCF (Highest Common Factor) using user input and the Fast HCF Java Code from the previous lesson.
Let's add some input mechanism so our user enters the set of numbers whose H.C.F. we are to find.


Combining the H.C.F. (G.C.D.) Java Code and Collecting User Input

All we need is a main code that asks the user for input.
For this purpose, we'll import and use the System.in and Scanner library classes.

How to use the HCF Code in Java

The user gets to enter his/her choice of numbers for the HCF Java algorithm.
After entering each number, the user presses <Enter>.
After the user has entered every of his/her numbers, the user enters the word done to see the result.


Note: You can comment out the Java code for the main class from the previous lesson if you have been following.



So! Java Fun Practice Exercise - Find HCF with User Input

As a fun practice exercise, feel free to try out your own numbers, and see how the Java code finds the HCF of your input numbers.







Java Code for HCF with User Input - Main Class.

package arithmetic;

import java.awt.HeadlessException;
import java.util.Scanner;
import java.util.ArrayList;
import java.util.List;
import javax.swing.JOptionPane;

public class Arithmetic {

    public static void main(String[] args) {
        System.out.println("Welcome to our demonstration sequels");
        System.out.println("Hope you enjoy (and follow) the lessons.");
        System.out.println("");

        // Collect input
        String collect_input; // For collecting user input

        /* Scanner takes care of collecting input using the System.in object. */
        Scanner user_choice = new Scanner(System.in);

        FastHCF h_c_f;
        List<Integer> set;
        set = new ArrayList<>();

        System.out.println("Welcome to our Find HCF program.");
        System.out.println("Enter your series of number for HCF.");
        System.out.println("Type 'done' when you are through with entering your numbers.\n");
        System.out.print("Enter First Number:  ");
        try {
            do {
                collect_input = user_choice.next();
                if (collect_input.matches("[0-9]+")) {
                    if (Integer.parseInt(collect_input) != 0) {
                        set.add(Integer.parseInt(collect_input));
                        System.out.print("Enter Next Number:  ");
                    } else {
                        JOptionPane.showMessageDialog(null,
                                "You cannot enter zero. Repeat that!\nType 'done' when you're finished.");
                    }
                } else if (!"DONE".equals(collect_input.toUpperCase())) {
                    JOptionPane.showMessageDialog(null,
                            "Wrong Input. Repeat that!\nType 'done' when you're finished.");
                }
            } while (!"DONE".equals(collect_input.toUpperCase()));
        } catch (NumberFormatException | HeadlessException e) {
            System.out.println(e.getMessage());
        }
        
        String render_result = "The HCF of ";
        for (int number : set) {
            render_result += number + "; ";
        }
        render_result += "is:  ";

        h_c_f = new FastHCF(set);
        System.out.println(render_result + h_c_f.getHCF());
    }

}



<< PreviousNext >>