In This Tutorial We Will Create Simple Prime Number Finder Application.
1. Create New Android Application.
2. Name It As PrimeNumber.
3. Open activity_main.xml File.
4. Drag And Drop One EditText And One Button
Or Replace Your activity_main.xml with Below Code.
activity_main.xml
5. Now Open Your MainActivity.java File And Replace it With Below Code.
MainActivity.java
6. Now Run It As Android Application And Enter Any Number And Check Whether Prime Or Not.
My Output Screen's.
All The Best :)
1. Create New Android Application.
2. Name It As PrimeNumber.
3. Open activity_main.xml File.
4. Drag And Drop One EditText And One Button
Or Replace Your activity_main.xml with Below Code.
activity_main.xml
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context=".MainActivity" >
<EditText
android:id="@+id/editText1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:layout_marginLeft="18dp"
android:layout_marginTop="46dp"
android:ems="10"
android:hint="Enter Number"
android:inputType="number" />
<Button
android:id="@+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="@+id/editText1"
android:layout_below="@+id/editText1"
android:layout_marginTop="34dp"
android:text="Check" />
</RelativeLayout>
5. Now Open Your MainActivity.java File And Replace it With Below Code.
MainActivity.java
package arshad.primenumber;
import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;
public class MainActivity extends Activity {
EditText input;
Button Check;
@Override
protected
void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
input=(EditText)findViewById(R.id.editText1);
Check=(Button)findViewById(R.id.button1);
Check.setOnClickListener(new
OnClickListener()
{
@Override
public
void onClick(View arg0) {
double n =
Double.parseDouble(input.getText().toString());
int f = 0;
for (int i = 2; i < n; i++)
{
if (n % i == 0)
f = 1;
}
if (f == 0)
{
Toast
t = Toast.makeText(getApplicationContext(),
"Given Number is
Prime Number",
Toast.LENGTH_SHORT);
t.show();
}else{
Toast
t = Toast.makeText(getApplicationContext(),
"Given Number is
Not a Prime Number",
Toast.LENGTH_SHORT);
t.show();
}
}});
}
@Override
public
boolean onCreateOptionsMenu(Menu menu) {
//
Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main,
menu);
return
true;
}
}
6. Now Run It As Android Application And Enter Any Number And Check Whether Prime Or Not.
My Output Screen's.
All The Best :)
No comments:
Post a Comment