Open In App

How to Build Fibonacci Perfect Square App using Android Studio?

Improve
Improve
Like Article
Like
Save
Share
Report

Fibonacci Perfect Square App built using Android Studio is used to check whether the number entered by the user is a Fibonacci number or Perfect Square number or both. The output is displayed on SnackBar. A sample video is given below to get an idea about what we are going to do in this article. Note that we are going to implement this project using the Java language.

Step by Step Implementation

Step 1: Create a New Project

To create a new project in Android Studio please refer to How to Create/Start a New Project in Android Studio. Note that select Java as the programming language.

Step 2: Working with the activity_main.xml file

Navigate to the app > res > layout > activity_main.xml and add the below code to that file. Below is the code for the activity_main.xml file. Create TextView to show text to the user, EditText to get input from the user, and Button to show whether the number is Fibonacci or Perfect Square or both.

XML




<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout 
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity">
  
    <TextView
        android:id="@+id/textView"
        android:layout_width="0dp"
        android:layout_height="0dp"
        android:layout_marginStart="4dp"
        android:layout_marginTop="108dp"
        android:layout_marginEnd="4dp"
        android:layout_marginBottom="105dp"
        android:text="Enter a number to find if it's a perfect square or a Fibonacci or both or null"
        android:textAppearance="@style/TextAppearance.AppCompat.Body2"
        app:layout_constraintBottom_toTopOf="@+id/button"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintHorizontal_bias="0.0"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent" />
  
    <EditText
        android:id="@+id/editTextTextPersonName"
        android:layout_width="205dp"
        android:layout_height="0dp"
        android:layout_marginStart="96dp"
        android:layout_marginBottom="494dp"
        android:ems="10"
        android:hint="Enter a number"
        android:inputType="textPersonName"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintStart_toStartOf="@+id/textView"
        app:layout_constraintTop_toBottomOf="@+id/textView"
        app:layout_constraintVertical_bias="1.0" />
  
    <Button
        android:id="@+id/button"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginBottom="416dp"
        android:onClick="onButtonClick"
        android:text="Submit"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="@+id/editTextTextPersonName"
        app:layout_constraintStart_toStartOf="@+id/editTextTextPersonName"
        app:layout_constraintTop_toBottomOf="@+id/textView" />
    
</androidx.constraintlayout.widget.ConstraintLayout>


After writing the code of the XML file for the app, the design looks as follows.

 

Step 3: Working with the MainActivity.java file

Go to the MainActivity.java file and refer to the following code. Below is the code for the MainActivity.java file. In this file, write a code for checking whether the number is Fibonacci or perfect square, or both. The output is displayed in Snackbar.

Java




package com.example.gfgfibonaccisquarenumber;
  
import android.os.Bundle;
import android.view.View;
import android.widget.EditText;
import androidx.appcompat.app.AppCompatActivity;
import com.google.android.material.snackbar.Snackbar;
  
public class MainActivity extends AppCompatActivity {
    class Numbers {
        int num;
  
        // Perfect Square
        public boolean isPerfectSquare()
        {
            double sqRoot = (int)Math.sqrt(num);
            return sqRoot * sqRoot == num;
        }
  
        // Print fibonacci series
        public boolean isFibonaaciNumber()
        {
            this.num = 5 * num * num + 4;
            boolean var1 = isPerfectSquare();
            this.num = 5 * num * num - 4;
            boolean var2 = isPerfectSquare();
            return (var1 || var2);
        }
    }
  
    public void onButtonClick(View view)
    {
  
        EditText editText
            = findViewById(R.id.editTextTextPersonName);
        Numbers myNum = new Numbers();
        myNum.num = Integer.parseInt(
            editText.getText().toString());
        String message = editText.getText().toString();
  
        if (myNum.isPerfectSquare()
            && myNum.isFibonaaciNumber()) {
            message += " is square and Fibonacci ";
        }
        else if (myNum.isPerfectSquare()) {
  
            message += "is square but not Fibonacci";
        }
        else if (myNum.isFibonaaciNumber()) {
            message += "is Fibonacci but not square";
        }
  
        else {
            message += "neither  Fibonacci  nor square";
        }
  
        Snackbar.make(view, message, Snackbar.LENGTH_LONG)
            .show();
    }
  
    @Override
    protected void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
    }
}


Output:

The layout of the app:

 

Output Video:



Last Updated : 23 Feb, 2023
Like Article
Save Article
Previous
Next
Share your thoughts in the comments
Similar Reads