How to compute GCD (Greatest Common Divisor) or HCF (Highest Common Factor) in your Android App? Android Studio complete source code

In this video it shows how the steps to develop a very simple algorithm to compute GCD – Greatest Common Divisor – also known as HCF – highest common factor. In this video initially it shows some quick algorithms to compute the GCD. Then it creates very simple layout design for the App.

Then the following algorithm is implemented in the Android App’s java code.
– First all the numbers are sorted in increasing using Array.sort API.
– Then GCD is initialized as 1 as that is the minimum value of GCD.
– Further, it iterates for all the numbers starting from 2 and up to the minimum number entered (which is the 1st element of the sorted array)
– Then for each number taken from above iteration, it checks for each element whether they are perfectly divisible from the above number leaving a 0 remainder. If true then the GCD is update to the new number else it continues with the next number in the iteration.

We hope you like this video. For any query, suggestions or appreciations we will be glad to hear from you at: programmerworld1990@gmail.com.

Source Code:

package com.example.mygcdapplication;

import androidx.appcompat.app.AppCompatActivity;

import android.os.Bundle;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;

import java.util.Arrays;

public class MainActivity extends AppCompatActivity {

    private EditText editText;
    private TextView textView;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        editText = findViewById(R.id.editText);
        textView = findViewById(R.id.textView);
    }

    public void calculateButton(View view){

        String[] stringsNumbers = editText.getText().toString().split(", ");
        Integer[] integersNumbers = new Integer[stringsNumbers.length];

        for (int i=0; i<stringsNumbers.length; i++){
            integersNumbers[i] = Integer.parseInt(stringsNumbers[i]);
        }

        Arrays.sort(integersNumbers);

        int GCD = 1;
        int intRemainder;

        for (int i = 2; i<=integersNumbers[0]; i++){
            intRemainder = 0;

            for(int j=0;j<integersNumbers.length; j++){
                if(integersNumbers[j]%i != 0){
                    intRemainder = 1;
                    break;
                }
            }

            if (intRemainder == 0){
                GCD = i;
            }
        }
        textView.setText(String.valueOf(GCD));
    }
}
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity">

    <Button
        android:id="@+id/button"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginStart="156dp"
        android:layout_marginTop="24dp"
        android:onClick="calculateButton"
        android:text="@string/calculate_gcd"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/editText" />

    <EditText
        android:id="@+id/editText"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginStart="102dp"
        android:layout_marginTop="52dp"
        android:ems="10"
        android:hint="@string/enter_the_number_here"
        android:inputType="textPersonName"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        android:autofillHints="" />

    <TextView
        android:id="@+id/textView"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginStart="176dp"
        android:layout_marginTop="36dp"
        android:text="@string/result"
        android:textSize="24sp"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/button" />
</androidx.constraintlayout.widget.ConstraintLayout>
<resources>
    <string name="app_name">My GCD Application</string>
    <string name="enter_the_number_here">Enter the Number here ...</string>
    <string name="calculate_gcd">Calculate GCD</string>
    <string name="result">Result ....</string>
</resources>
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.example.mygcdapplication">

    <application
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:roundIcon="@mipmap/ic_launcher_round"
        android:supportsRtl="true"
        android:theme="@style/AppTheme">
        <activity android:name=".MainActivity">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
    </application>

</manifest>

Leave a Reply

%d bloggers like this: