Write a Program to create two screens. First screen will take one number and it should display factorial of the same number. Also specify which type of intent you will use in this case
Code:
Activity_main.xml:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout 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">
<EditText
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:layout_above="@id/t1"
android:layout_centerVertical="true"
android:layout_centerHorizontal="true"
android:hint="Enter any number"
android:ems="10"
android:id="@+id/e1"
/>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Factorial"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true"
android:id="@+id/t1"
/>
</RelativeLayout>
Main_Activity.java:
package com.example.myapplication;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import androidx.appcompat.app.AppCompatActivity;
public class Main4Activity extends AppCompatActivity {
EditText e1;
Button btnSubmit;
int n;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main4);
e1 = (EditText) findViewById(R.id.e1);
btnSubmit = (Button) findViewById(R.id.t1);
btnSubmit.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
n=Integer.parseInt(e1.getText().toString());
Intent intObj = new Intent(Main4Activity.this, Main5Activity.class);
intObj.putExtra("Number", n);
startActivity(intObj);
}});}}
Activity_main2.xml:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 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=".Main5Activity">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/t1"/>
</LinearLayout>
Main2_Activity.java:
package com.example.myapplication;
import androidx.appcompat.app.AppCompatActivity;
import android.content.Intent;
import android.os.Bundle;
import android.widget.TextView;
public class Main5Activity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main5);
final TextView txt = (TextView) findViewById(R.id.t1);
Intent intename = getIntent();
int un = (int) intename.getSerializableExtra("Number");
int i=0;
int f=1;
for(i=un;i>0;i--)
f=f*i;
txt.setText("Factorial of " + un+":"+f);
txt.setTextSize(30);
}
}
Output:
1]Original Layout 2] After Click on Factorial button
Comments
Post a Comment