Advertisement
Shailrshah

[Android] Parity, Factorial and Prime Test of a number

Nov 4th, 2014
224
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 3.78 KB | None | 0 0
  1. ///////////////////////
  2. // MainActivity.java //
  3. //////////////////////
  4.  
  5. package com.example.mythreeprograms;
  6.  
  7. import android.support.v7.app.ActionBarActivity;
  8. import android.widget.*;
  9. import android.os.Bundle;
  10. import android.view.Menu;
  11. import android.view.MenuItem;
  12. import android.view.View;
  13.  
  14.  
  15. public class MainActivity extends ActionBarActivity {
  16.  
  17.     //Ratta Maro
  18.     TextView op;
  19.     EditText ip;
  20.     Button b;
  21.     //Ratta Khatam
  22.    
  23.     @Override
  24.     protected void onCreate(Bundle savedInstanceState) {
  25.         super.onCreate(savedInstanceState);
  26.         setContentView(R.layout.activity_main);
  27.        
  28.         //Ratta Maro
  29.        
  30.         op = (TextView) findViewById(R.id.output1);
  31.         ip = (EditText) findViewById(R.id.input1);
  32.         b = (Button) findViewById(R.id.button1);
  33.        
  34.         b.setOnClickListener(new View.OnClickListener() {
  35.            
  36.             @Override
  37.             public void onClick(View v) {
  38.                 // TODO Auto-generated method stub
  39.                 int n = Integer.parseInt(ip.getText().toString());
  40.                 int fact = 1;
  41.                 String parity, prime="Yes";
  42.                
  43.                 if(n%2==0) parity = "Even";
  44.                 else parity = "Odd";
  45.                
  46.                 for(int i = n; i > 1; i--){
  47.                     fact *= i;
  48.                 }
  49.                
  50.                 for(int i=2; i<=n/2; i++)
  51.                      if(n%i == 0){prime="No"; break;}
  52.                
  53.                 op.setText("paarity: "+parity+"\nFactorial: "+fact+"\nPrime?: "+prime);
  54.                
  55.             }
  56.         });
  57.         //ratta khatam
  58.     }
  59.  
  60.  
  61.     @Override
  62.     public boolean onCreateOptionsMenu(Menu menu) {
  63.         // Inflate the menu; this adds items to the action bar if it is present.
  64.         getMenuInflater().inflate(R.menu.main, menu);
  65.         return true;
  66.     }
  67.  
  68.     @Override
  69.     public boolean onOptionsItemSelected(MenuItem item) {
  70.         // Handle action bar item clicks here. The action bar will
  71.         // automatically handle clicks on the Home/Up button, so long
  72.         // as you specify a parent activity in AndroidManifest.xml.
  73.         int id = item.getItemId();
  74.         if (id == R.id.action_settings) {
  75.             return true;
  76.         }
  77.         return super.onOptionsItemSelected(item);
  78.     }
  79. }
  80.  
  81.  
  82.  
  83. ///////////////////////
  84. // activity_main.xml //
  85. //////////////////////
  86. <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
  87.     xmlns:tools="http://schemas.android.com/tools"
  88.     android:layout_width="match_parent"
  89.     android:layout_height="match_parent"
  90.     android:paddingBottom="@dimen/activity_vertical_margin"
  91.     android:paddingLeft="@dimen/activity_horizontal_margin"
  92.     android:paddingRight="@dimen/activity_horizontal_margin"
  93.     android:paddingTop="@dimen/activity_vertical_margin"
  94.     tools:context="com.example.mythreeprograms.MainActivity" >
  95.  
  96.     <Button
  97.         android:id="@+id/button1"
  98.         android:layout_width="wrap_content"
  99.         android:layout_height="wrap_content"
  100.         android:layout_alignParentBottom="true"
  101.         android:layout_centerHorizontal="true"
  102.         android:layout_marginBottom="72dp"
  103.         android:text="@string/abc_searchview_description_submit" />
  104.  
  105.     <TextView
  106.         android:id="@+id/output1"
  107.         android:layout_width="wrap_content"
  108.         android:layout_height="wrap_content"
  109.         android:layout_alignLeft="@+id/input1"
  110.         android:layout_alignParentTop="true"
  111.         android:layout_marginLeft="16dp"
  112.         android:layout_marginTop="43dp"
  113.         android:text="@string/app_name" />
  114.  
  115.     <EditText
  116.         android:id="@+id/input1"
  117.         android:layout_width="wrap_content"
  118.         android:layout_height="wrap_content"
  119.         android:layout_above="@+id/button1"
  120.         android:layout_centerHorizontal="true"
  121.         android:layout_marginBottom="90dp"
  122.         android:ems="10"
  123.         android:hint="@string/inputstr"
  124.         android:inputType="number" >
  125.  
  126.         <requestFocus />
  127.     </EditText>
  128.  
  129. </RelativeLayout>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement