coolbud012

HCFLCM

Feb 2nd, 2014
259
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.25 KB | None | 0 0
  1. package com.droidacid.apticalc.aptitudes;
  2.  
  3. import com.droidacid.apticalc.R;
  4. import android.app.Activity;
  5. import android.app.AlertDialog;
  6. import android.content.DialogInterface;
  7. import android.os.Bundle;
  8. import android.view.View;
  9. import android.view.View.OnClickListener;
  10. import android.widget.Button;
  11. import android.widget.EditText;
  12. import android.widget.TextView;
  13.  
  14. /**
  15. * Created by ShivamD on 5/18/13.
  16. */
  17. public class AptiLcmHcf extends Activity implements OnClickListener {
  18.  
  19. Button bCalc, bClear;
  20. EditText etNumber1, etNumber2;
  21. TextView tvResult;
  22. int numb1, numb2;
  23.  
  24. @Override
  25. protected void onCreate(Bundle savedInstanceState) {
  26. super.onCreate(savedInstanceState);
  27. setContentView(R.layout.aptilcmhcf);
  28.  
  29. }
  30.  
  31. protected void initialize() {
  32.  
  33. etNumber1 = (EditText) findViewById(R.id.et_apti_Num1);
  34. etNumber2 = (EditText) findViewById(R.id.et_apti_Num2);
  35.  
  36. tvResult = (TextView) findViewById(R.id.tv_apti_LcmResult);
  37. bCalc = (Button) findViewById(R.id.b_apti_Lcm);
  38. bClear = (Button) findViewById(R.id.bClear);
  39. bCalc.setOnClickListener(this);
  40. bClear.setOnClickListener(this);
  41.  
  42. }
  43.  
  44. @Override
  45. public void onClick(View v) {
  46. switch (v.getId()) {
  47. case R.id.b_apti_Lcm:
  48. try {
  49. numb1 = Integer.parseInt(etNumber1.getText().toString());
  50. numb2 = Integer.parseInt(etNumber2.getText().toString());
  51.  
  52. int hcf = 1;
  53. int p = numb1 * numb2;
  54. for (int i = 2; i < p; i++) {
  55. if ((numb1 % i == 0) && (numb2 % i == 0)) {
  56. hcf = i;
  57. }
  58. }
  59. int lcm = p / hcf;
  60. String result = "LCM is : " + lcm + " & HCF is : " + hcf;
  61. tvResult.setText(result);
  62. } catch (NumberFormatException e) {
  63. // A Dialog Box here to display to enter an input
  64. tvResult.setText("");
  65. AlertDialog.Builder d = new AlertDialog.Builder(this);
  66. d.setTitle("No value entered");
  67. d.setMessage("Fields cannot be left blank");
  68. d.setNegativeButton("Ok",
  69. new DialogInterface.OnClickListener() {
  70.  
  71. @Override
  72. public void onClick(DialogInterface dialog,
  73. int which) {
  74. dialog.cancel();
  75. }
  76. });
  77.  
  78. d.show();
  79. }
  80. break;
  81. case R.id.bClear:
  82. etNumber1.setText("");
  83. etNumber2.setText("");
  84. tvResult.setText("");
  85. break;
  86. }
  87.  
  88. }
  89. }
Advertisement
Add Comment
Please, Sign In to add comment