Created
March 8, 2022 13:10
-
-
Save shivam-gupta007/36c32130de81e066226fbcbf2af960a4 to your computer and use it in GitHub Desktop.
Create an app that find Automorphic, Armstrong, Disarium Number
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.bca.b1.tca1901074; | |
import android.os.Bundle; | |
import android.widget.Button; | |
import android.widget.EditText; | |
import android.widget.TextView; | |
import androidx.appcompat.app.AppCompatActivity; | |
public class SecondActivity extends AppCompatActivity { | |
EditText inputNo; | |
Button submitButton; | |
TextView resultText; | |
@Override | |
protected void onCreate(Bundle savedInstanceState) { | |
super.onCreate(savedInstanceState); | |
setContentView(R.layout.activity_second); | |
inputNo = findViewById(R.id.inputNumber); | |
submitButton = findViewById(R.id.submit_button); | |
resultText = findViewById(R.id.text); | |
submitButton.setOnClickListener(view -> { | |
int num = Integer.parseInt(inputNo.getText().toString()); | |
String type = getIntent().getStringExtra("TYPE").toString(); | |
String msg; | |
if (type.equals("AutomorphicNo")) | |
msg = findAutomorphicNo(num); | |
else if (type.equals("ArmstrongNo")) | |
msg = findArmstrongNo(num); | |
else | |
msg = findDisariumNo(num); | |
resultText.setText(msg); | |
}); | |
} | |
String findAutomorphicNo(int num) { | |
int count = 0; | |
int square = num * num; | |
int temp = num; | |
while (temp > 0) { | |
count++; | |
temp = temp / 10; | |
} | |
int lastDigit = (int) (square % (Math.pow(10, count))); | |
if (num == lastDigit) | |
return "Result: Yes"; | |
else | |
return "Result: No"; | |
} | |
String findArmstrongNo(int num) { | |
int r, sum = 0, temp; | |
temp = num; | |
while (num > 0) { | |
r = num % 10; | |
sum = sum + (r * r * r); | |
num = num / 10; | |
} | |
if (temp == sum) | |
return "Result: Yes"; | |
else | |
return "Result: No"; | |
} | |
String findDisariumNo(int num) { | |
int copy = num, d = 0, sum = 0; | |
String s = Integer.toString(num); | |
int len = s.length(); | |
while (copy > 0) { | |
d = copy % 10; | |
sum = sum + (int) Math.pow(d, len); | |
len--; | |
copy = copy / 10; | |
} | |
if (sum == num) | |
return "Result: Yes"; | |
else | |
return "Result: No"; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment