KEMBAR78
Factorial Example | PDF
0% found this document useful (0 votes)
24 views2 pages

Factorial Example

The document provides an example of an Android application that calculates the factorial of a number using Java and XML. It includes the MainActivity.java file, which handles user input and displays the result, and the activity_main.xml layout file, which defines the user interface elements. The app allows users to input a number and calculate its factorial by pressing a button.

Uploaded by

akshayapamul7
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
24 views2 pages

Factorial Example

The document provides an example of an Android application that calculates the factorial of a number using Java and XML. It includes the MainActivity.java file, which handles user input and displays the result, and the activity_main.xml layout file, which defines the user interface elements. The app allows users to input a number and calculate its factorial by pressing a button.

Uploaded by

akshayapamul7
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 2

Android Factorial Example (Java + XML)

MainActivity.java

package com.example.factorialapp;

import android.os.Bundle;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;
import androidx.appcompat.app.AppCompatActivity;

public class MainActivity extends AppCompatActivity {

EditText inputNumber;
TextView resultText;

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

inputNumber = findViewById(R.id.number_input);
resultText = findViewById(R.id.result_view);
}

public void calculateFactorial(View view) {


int num = Integer.parseInt(inputNumber.getText().toString());
long fact = 1;
for (int i = 1; i <= num; i++) {
fact *= i;
}
resultText.setText("Factorial: " + fact);
}
}
activity_main.xml

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


<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:padding="16dp">

<EditText
android:id="@+id/number_input"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="Enter a number"
android:inputType="number" />

<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Calculate Factorial"
android:onClick="calculateFactorial" />

<TextView
android:id="@+id/result_view"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Result"
android:textSize="18sp"
android:paddingTop="10dp" />
</LinearLayout>

You might also like