Check Prime Number - XML CODE
<?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=".MainActivity3"
android:orientation="vertical"
android:background="#E4EEED"
>
<androidx.appcompat.widget.Toolbar
android:id="@+id/toolbar12"
android:layout_width="match_parent"
android:layout_height="50dp"
android:background="@drawable/bottom_curved"
app:title="Check Prime Number"
android:elevation="4dp"
app:titleTextColor="@color/white"
/>
<com.google.android.material.textfield.TextInputLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginRight="10dp"
android:layout_marginLeft="10dp"
android:layout_marginTop="10dp"
app:errorTextColor="#FF0000"
app:hintTextColor="#0038FF"
android:hint="Input a number"
app:boxStrokeColor="#000DFF"
android:textColorHint="@color/black"
app:endIconMode="clear_text"
app:endIconTint="#FF0000"
>
<com.google.android.material.textfield.TextInputEditText
android:id="@+id/EdPrime1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:inputType="numberDecimal"
android:textColor="@color/black"
/>
</com.google.android.material.textfield.TextInputLayout>
<com.google.android.material.textfield.TextInputLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginRight="10dp"
android:layout_marginLeft="10dp"
android:layout_marginTop="10dp"
app:errorTextColor="#FF0000"
app:hintTextColor="#0038FF"
android:hint="Input a number"
app:boxStrokeColor="#000DFF"
android:textColorHint="@color/black"
app:endIconMode="clear_text"
app:endIconTint="#FF0000"
>
<com.google.android.material.textfield.TextInputEditText
android:id="@+id/EdPrime2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:inputType="numberDecimal"
android:textColor="@color/black"
/>
</com.google.android.material.textfield.TextInputLayout>
<Button
android:id="@+id/ButtonPrime1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:backgroundTint="#1100FF"
android:text="Check Result"
android:textColor="#FFFFFF"
android:textSize="20sp"
android:layout_marginRight="80dp"
android:layout_marginLeft="80dp"
android:layout_marginTop="10dp"
/>
<ScrollView
android:layout_width="match_parent"
android:layout_height="500dp"
>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:layout_margin="20dp"
>
<TextView
android:id="@+id/TVPrime1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textSize="25sp"
android:textColor="@color/black"
/>
</LinearLayout>
</ScrollView>
</LinearLayout>
Check Prime Number - JAVA CODE
package com.arah.jobayerahmed;
import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;
import androidx.appcompat.widget.Toolbar;
import android.os.Bundle;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.TextView;
import com.google.android.material.textfield.TextInputEditText;
public class MainActivity3 extends AppCompatActivity {
TextView TVPrime1;
TextInputEditText EdPrime1, EdPrime2;
Button ButtonPrime1;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main3);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar12);
setSupportActionBar(toolbar);
if (getSupportActionBar() != null) {
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
getSupportActionBar().setDisplayShowHomeEnabled(true);
}
EdPrime1 = findViewById(R.id.EdPrime1);
EdPrime2 = findViewById(R.id.EdPrime2);
ButtonPrime1 = findViewById(R.id.ButtonPrime1);
TVPrime1 = findViewById(R.id.TVPrime1);
ButtonPrime1.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
String Hello1 = EdPrime1.getText().toString();
String Hello2 = EdPrime2.getText().toString();
if (Hello1.isEmpty()){
EdPrime1.setError("Enter Number");
}else if (Hello2.isEmpty()){
EdPrime2.setError("Enter Number");
}else {
int GH1 = Integer.parseInt(Hello1);
int GH2 = Integer.parseInt(Hello2);
List<Integer> primes1 = getPrimeNumbers(GH2,GH1);
StringBuilder primeNumbers = new StringBuilder();
for (Integer prime : primes1) {
primeNumbers.append(prime).append(", ");
}
TVPrime1.setText("Prime Number "+primeNumbers);
}
}
});
}
private List<Integer> getPrimeNumbers(int n, int m) {
List<Integer> primes1 = new ArrayList<>();
for (int i = m; i <= n; i++) {
if (isPrime(i)) {
primes1.add(i);
}
}
return primes1;
}
private boolean isPrime(int num) {
if (num <= 1) {
return false;
}
for (int i = 2; i <= Math.sqrt(num); i++) {
if (num % i == 0) {
return false;
}
}
return true;
}
@Override
public boolean onOptionsItemSelected(@NonNull MenuItem item) {
if (item.getItemId() == android.R.id.home) {
finish();
}
return super.onOptionsItemSelected(item);
}
}
Tags