Advertisement
Shishu

prime number java code

Oct 18th, 2019
254
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.32 KB | None | 0 0
  1. package com.example.user.myapp;
  2.  
  3. import android.content.Intent;
  4. import android.support.v7.app.AppCompatActivity;
  5. import android.os.Bundle;
  6. import android.view.View;
  7. import android.widget.Button;
  8. import android.widget.EditText;
  9. import android.widget.Toast;
  10.  
  11. public class SecondActivity extends AppCompatActivity {
  12.  
  13.     Button bprime;
  14.     EditText editText;
  15.  
  16.     @Override
  17.     protected void onCreate(Bundle savedInstanceState) {
  18.         super.onCreate(savedInstanceState);
  19.         setContentView(R.layout.activity_second);
  20.  
  21.         bprime = (Button) findViewById(R.id.b2);
  22.         editText = (EditText) findViewById(R.id.e1);
  23.  
  24.         bprime.setOnClickListener(new View.OnClickListener() {
  25.             @Override
  26.             public void onClick(View v) {
  27.                 String temp = editText.getText().toString();
  28.                 int num = Integer.parseInt(temp);
  29.  
  30.                 int flag = 0;
  31.                 for (int i = 2; i < num; i++) {
  32.                     if (num % i == 0) flag = 1;
  33.                 }
  34.  
  35.                 if (flag == 0)
  36.                     Toast.makeText(getApplicationContext(), "PRIME NUMBER!!!", Toast.LENGTH_LONG).show();
  37.                 else
  38.                     Toast.makeText(getApplicationContext(), "NOT A PRIME NUMBER!!!", Toast.LENGTH_LONG).show();
  39.             }
  40.         });
  41.     }
  42. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement