Download as rtf, pdf, or txt
Download as rtf, pdf, or txt
You are on page 1of 5

Experiment - 9

Problem Definition: Create an android application to calculate the factorial.

Procedure: Step 1: Create a New Android Project.

Step 2: Configure the Project Settings.

Step 3: Add an App Launcher Icon.

Step 4: Select the App Activity Type.

Step 5: Specify Activity Details.

Step 6: Review Your Simple App.

XML – Code and Design:

<?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"

        android:background="#DBB9F6CA"

        android:backgroundTint="#DBB9F6CA"

        tools:context=".MainActivity">

        <EditText

                android:id="@+id/editTextNumber"

                android:layout_width="wrap_content"

                android:layout_height="wrap_content"

                android:layout_marginTop="40dp"

                android:ems="10"

                android:hint="Enter a number"

                android:inputType="number"
                app:layout_constraintEnd_toEndOf="parent"

                app:layout_constraintHorizontal_bias="0.497"

                app:layout_constraintStart_toStartOf="parent"

                app:layout_constraintTop_toTopOf="parent" />

        <Button

                android:id="@+id/button"

                android:layout_width="wrap_content"

                android:layout_height="wrap_content"

                android:layout_marginTop="24dp"

                android:onClick="calculate"

                android:text="Calculate"

                app:layout_constraintEnd_toEndOf="parent"

                app:layout_constraintStart_toStartOf="parent"

                app:layout_constraintTop_toBottomOf="@+id/editTextNumber" />

        <TextView

                android:id="@+id/textView"

                android:layout_width="wrap_content"

                android:layout_height="wrap_content"

                android:layout_marginTop="24dp"

                android:textColor="@color/black"

                android:textSize="16sp"

                app:layout_constraintEnd_toEndOf="parent"

                app:layout_constraintStart_toStartOf="parent"

                app:layout_constraintTop_toBottomOf="@+id/button" />

</androidx.constraintlayout.widget.ConstraintLayout>

Java Code:
package com.example.factorial;

import androidx.appcompat.app.AppCompatActivity;

import android.os.Bundle;

import android.view.View;

import android.widget.EditText;

import android.widget.TextView;

public class MainActivity extends AppCompatActivity {

        @Override

        protected void onCreate(Bundle savedInstanceState) {

                super.onCreate(savedInstanceState);

                setContentView(R.layout.activity_main);

    }

        public void calculate(View view) {

                EditText ed1 = (EditText) findViewById(R.id.editTextNumber);

                String s1 = ed1.getText().toString();

                int num = Integer.parseInt(s1);

                int i, fact=1;

                for(i=1; i<=num; i++) {

                        fact=fact*i;

        }

                TextView tv1 = findViewById(R.id.textView);

                tv1.setText(Integer.toString(fact));

    }

}
Manifests:

<?xml version="1.0" encoding="utf-8"?>

<manifest xmlns:android="http://schemas.android.com/apk/res/android"

        xmlns:tools="http://schemas.android.com/tools"

        package="com.example.myapplication">

        <application

                android:allowBackup="true"

                android:dataExtractionRules="@xml/data_extraction_rules"

                android:fullBackupContent="@xml/backup_rules"

                android:icon="@mipmap/ic_launcher"

                android:label="@string/app_name"

                android:roundIcon="@mipmap/ic_launcher_round"

                android:supportsRtl="true"

                android:theme="@style/Theme.MyApplication"

                tools:targetApi="31">

                <activity

                        android:name=".MainActivity"

                        android:exported="true">

                        <intent-filter>

                                <action android:name="android.intent.action.MAIN" />

                                <category android:name="android.intent.category.LAUNCHER" />

                        </intent-filter>

                </activity>

        </application>

</manifest>
Output:

You might also like