Owais Shaikh
TYCS B 8881
Practical 9
1) write an android program to determine if the enter the number is prime or not.
Activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="ve
rtical"
android:padding="16dp
">
<EditText
android:id="@+id/input_number
"
android:layout_width="match_p
arent"
android:layout_height="wrap_co
ntent"android:hint="Enter a
number"
android:inputType="number"/>
<Button
android:id="@+id/check_button
"
android:layout_width="wrap_co
ntent"
android:layout_height="wrap_co
ntent"android:text="Check
Prime"/>
<TextView
android:id="@+id/result_text"
android:layout_width="wrap_co
ntent"
android:layout_height="wrap_co
ntent"android:textSize="18sp"
android:layout_marginTop="16d
p"/>
</LinearLayout>
MainActivity.java
package
com.android.myapplication;
import android.os.Bundle;
import android.view.View;
import
android.widget.Button;
Owais Shaikh
TYCS B 8881
import
android.widget.EditText;
import
android.widget.TextView;
import
androidx.appcompat.app.AppCompatActivity;
public class MainActivity extends
AppCompatActivity {
EditText
inputNumber;
Button
checkButton;
TextView
resultText;
@Override
protected void onCreate(Bundle
savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
inputNumber =
findViewById(R.id.input_number);
checkButton =
findViewById(R.id.check_button);
resultText = findViewById(R.id.result_text);
checkButton.setOnClickListener(new
View.OnClickListener() {@Override
Integer.parseInt(numberString);
if (isPrime(number)) {
resultText.setText(number + " is a prime number.");
} else {
resultText.setText(number + " is not a prime number.");
}
}
})
;
}
private boolean isPrime(int
num) {if (num <= 1)
return
false;if
(num
<= 3)
return true;
if (num % 2 == 0 || num % 3
== 0)return false;
int i = 5;
while (i * i <= num) {
Owais Shaikh
TYCS B 8881
if (num % i == 0 || num % (i + 2)
== 0)return false;
i += 6;
}
return true;
}
}
Owais Shaikh
TYCS B 8881
2) Design the simple android app to calculate the factorial of a given number
Activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<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="match_parent"
android:padding="16dp"
tools:context=".MainActi
vity">
<EditText
android:id="@+id/input_number
"
android:layout_width="match_p
arent"
android:layout_height="wrap_co
ntent"android:hint="Enter a
number"
android:inputType="number"/>
<Button
android:id="@+id/calculate_button"
android:layout_width="match_parent
"
android:layout_height="wrap_content
"
android:layout_below="@id/input_nu
mber"android:text="Calculate
Factorial"/>
<TextView
android:id="@+id/result_text"
android:layout_width="match_p
arent"
android:layout_height="wrap_co
ntent"
android:layout_below="@id/calculate_b
utton"
android:layout_marginTop="16dp"/>
</RelativeLayout>
MainActivity.java
package com.android.myapplication;
import
androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
Owais Shaikh
TYCS B 8881
import
android.widget.Button;
import
android.widget.EditText;
import
android.widget.TextView;
public class MainActivity extends
AppCompatActivity {EditText inputNumber;
Button
calculateButton;
TextView
resultText;
@Override
protected void onCreate(Bundle
savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
inputNumber =
findViewById(R.id.input_number);
calculateButton =
findViewById(R.id.calculate_button);resultText =
findViewById(R.id.result_text);
calculateButton.setOnClickListener(new
View.OnClickListener() {@Override
public void
onClick(View v) {
calculateFactorial();
}
})
;
}
private void calculateFactorial() {
int number =
Integer.parseInt(inputNumber.getText().toString());long
factorial = 1;
for (int i = 1; i <= number;
i++) {factorial *= i;
}
resultText.setText("Factorial of " + number + " is: " + factorial);
}
Owais Shaikh
TYCS B 8881
calculateButton.setOnClickListener(new
View.OnClickListener() {@Override
public void
onClick(View v) {
calculateFactorial();
}
})
;
}
}
Owais Shaikh
TYCS B 8881
3) Design an android app to check weather the entered no. is even or not.
Activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<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="match_parent"
tools:context=".MainActivity">
<EditText
android:id="@+id/number_input"
android:layout_width="wrap_con
tent"
android:layout_height="wrap_co
ntent"
android:layout_centerHorizontal=
"true"
android:layout_marginTop="50d
p" android:hint="Enter a number"
/>
<Button
android:id="@+id/check_button"
android:layout_width="wrap_content
"
android:layout_height="wrap_content
"
android:layout_below="@id/number_
input"
android:layout_centerHorizontal="tru
e" android:layout_marginTop="20dp"
android:text="Check" />
<TextView
android:id="@+id/result_text"
android:layout_width="wrap_co
ntent"
android:layout_height="wrap_co
ntent"
android:layout_below="@id/check_b
utton"
android:layout_centerHorizontal="tru
e"
android:layout_marginTop="20dp"
android:textSize="20sp" />
</RelativeLayout>
Owais Shaikh
TYCS B 8881
MainActivity.java
package
com.android.myapplication;
import android.os.Bundle;
import android.view.View;
import
android.widget.Button;
import
android.widget.EditText;
import
android.widget.TextView;
import
androidx.appcompat.app.AppCompatActivity;
public class MainActivity extends
AppCompatActivity {
EditText
numberInput;
Button
checkButton;
TextView
resultText;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
numberInput =
findViewById(R.id.number_input);
checkButton =
findViewById(R.id.check_button);
resultText = findViewById(R.id.result_text);
checkButton.setOnClickListener(new
View.OnClickListener() {@Override
public void
onClick(View v) {
checkNumber();
}
})
;
}
private void checkNumber() {
Owais Shaikh
TYCS B 8881
String inputStr =
numberInput.getText().toString();int number
= Integer.parseInt(inputStr);
if (number % 2 == 0) {
resultText.setText(number + " is
even.");
} else {
resultText.setText(number + " is odd.");
}
}
Owais Shaikh
TYCS B 8881
4) Create an android app to check entered year is leap year or not
Activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<EditText
android:id="@+id/year_input"
android:layout_width="wrap_co
ntent"
android:layout_height="wrap_co
ntent"
android:layout_centerHorizontal=
"true"
android:layout_marginTop="50d
p" android:hint="Enter year"
android:inputType="number" />
<Button
android:id="@+id/check_button"
android:layout_width="wrap_cont
ent"
android:layout_height="wrap_cont
ent"
android:layout_below="@id/year_
input"
android:layout_centerHorizontal="
true"
android:layout_marginTop="20dp
" android:text="Check" />
<TextView
android:id="@+id/result_text"
android:layout_width="wrap_co
ntent"
android:layout_height="wrap_co
ntent"
android:layout_below="@id/check_b
utton"
android:layout_centerHorizontal="tru
e"
android:layout_marginTop="20dp"
android:text=""
android:textSize="20sp" />
</RelativeLayout>
Owais Shaikh
TYCS B 8881
MainActivity.java
package
com.android.myapplication;
import android.os.Bundle;
import android.view.View;
import
android.widget.Button;
import
android.widget.EditText;
import
android.widget.TextView;
import
androidx.appcompat.app.AppCompatActivity;
public class MainActivity extends
AppCompatActivity {
EditText
yearInput;
Button
checkButton;
TextView
resultText;
@Override
protected void onCreate(Bundle
savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
yearInput =
findViewById(R.id.year_input);
checkButton =
findViewById(R.id.check_button);
resultText =
findViewById(R.id.result_text);
checkButton.setOnClickListener(new
View.OnClickListener() {@Override
public void onClick(View v) {
String yearStr =
yearInput.getText().toString();if
(!yearStr.isEmpty()) {
int year =
Integer.parseInt(yearStr);if
(isLeapYear(year)) {
resultText.setText(year + " is a leap year.");
Owais Shaikh
TYCS B 8881
} else {
resultText.setText(year + " is not a leap year.");
}
} else {
resultText.setText("Please enter a year.");
}
}
})
;
}
private boolean isLeapYear(int year) {
return (year % 4 == 0 && year % 100 != 0) || (year % 400 == 0);
}
}
Owais Shaikh
TYCS B 8881
5) Determine with an android app weather the entered number is armstrong or not.
Activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<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="match_parent"
tools:context=".MainActivity">
<EditText
android:id="@+id/editTextNumb
er"
android:layout_width="wrap_con
tent"
android:layout_height="wrap_co
ntent"
android:layout_centerHorizontal=
"true"
android:layout_marginTop="32d
p" android:hint="Enter a number"
android:inputType="number"/>
<Button
android:id="@+id/buttonCheck"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@id/editTextNumber"
android:layout_centerHorizontal="true"
android:layout_marginTop="16dp"
android:text="Check"/>
<TextView
android:id="@+id/textViewResult"
android:layout_width="wrap_conten
t"
android:layout_height="wrap_conte
nt"
android:layout_below="@id/button
Check"
android:layout_centerHorizontal="tr
ue"
android:layout_marginTop="16dp"
android:text=""
android:textSize="24sp"/>
</RelativeLayout>
Owais Shaikh
TYCS B 8881
MainActivity.java
package
com.android.myapplication;
import android.os.Bundle;
import android.view.View;
import
android.widget.Button;
import
android.widget.EditText;
import
android.widget.TextView;
import androidx.appcompat.app.AppCompatActivity;
public class MainActivity extends
AppCompatActivity {@Override
protected void onCreate(Bundle
savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
EditText editTextNumber = findViewById(R.id.editTextNumber);
Button buttonCheck = findViewById(R.id.buttonCheck);
TextView textViewResult =
findViewById(R.id.textViewResult);
buttonCheck.setOnClickListener(new
View.OnClickListener() {@Override
public void onClick(View v) {
String input =
editTextNumber.getText().toString();if
(!input.isEmpty()) {
int number =
Integer.parseInt(input);if
(isArmstrong(number)) {
textViewResult.setText(number + " is an Armstrong number.");
} else {
textViewResult.setText(number + " is not an Armstrong number.");
}
} else {
textViewResult.setText("Please enter a number.");
}
}
})
;
}
private boolean isArmstrong(int number) {
int originalNumber, remainder, result = 0,
n = 0;originalNumber = number;
Owais Shaikh
TYCS B 8881
for (;originalNumber != 0; originalNumber /= 10,
++n);originalNumber = number;
for (;originalNumber != 0; originalNumber
/= 10) {remainder = originalNumber % 10;
result += Math.pow(remainder, n);
}
return result == number;
}
}