0% found this document useful (0 votes)
258 views

Assignment No. 1 Activity Life Cycle

The document describes an Android application that demonstrates the activity lifecycle through two activities. The main activity displays a button that starts the second activity when clicked. Both activities override the lifecycle callback methods and display Toast messages to indicate when each method is called. The activities, their layout files and code are provided to show how to transition between activities and handle the lifecycle methods in each one.

Uploaded by

Ayush jain
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
258 views

Assignment No. 1 Activity Life Cycle

The document describes an Android application that demonstrates the activity lifecycle through two activities. The main activity displays a button that starts the second activity when clicked. Both activities override the lifecycle callback methods and display Toast messages to indicate when each method is called. The activities, their layout files and code are provided to show how to transition between activities and handle the lifecycle methods in each one.

Uploaded by

Ayush jain
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 24

Assignment no.

1 Activity Life cycle

Activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context=".MainActivity">

<TextView
android:id="@+id/tv"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Activity 1"
/>
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Click"
android:onClick="clickMe"/>

</LinearLayout>
MainActivity.java
package com.example.myfirstapp_btech;

import androidx.appcompat.app.AppCompatActivity;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.TextView;
import android.widget.Toast;

public class MainActivity extends AppCompatActivity {

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Toast.makeText(this,"onCreate method invoked ",Toast.LENGTH_LONG).show();
}
@Override
protected void onStop() {
super.onStop();
Toast.makeText(this,"onstop method invoked ",Toast.LENGTH_LONG).show();
}
@Override
protected void onDestroy() {
super.onDestroy();
Toast.makeText(this,"ondestroy method invoked ",Toast.LENGTH_LONG).show();
}
@Override
protected void onPause() {
super.onPause();
Toast.makeText(this,"onpause method invoked ",Toast.LENGTH_LONG).show();
}
@Override
protected void onResume() {
super.onResume();
Toast.makeText(this,"onResume method invoked ",Toast.LENGTH_LONG).show();
}
@Override
protected void onStart() {
super.onStart();
Toast.makeText(this,"onstart method invoked ",Toast.LENGTH_LONG).show();
}

@Override
protected void onRestart() {
super.onRestart();
Toast.makeText(this,"onrestart method invoked ",Toast.LENGTH_LONG).show();
}

public void clickMe(View view) {


Intent intent=new Intent(this,Main2Activity.class);
startActivity(intent);
}
}

activity_main2.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".Main2Activity">

<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Activity 2"/>
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Click"
android:onClick="clickMe"/>

</LinearLayout>

Main2Activity.java

package com.example.myfirstapp_btech;

import androidx.appcompat.app.AppCompatActivity;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Toast;

public class Main2Activity extends AppCompatActivity {

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main2);
Toast.makeText(this,"onCreate 2 method invoked ",Toast.LENGTH_LONG).show();
}
@Override
protected void onStop() {
super.onStop();
Toast.makeText(this,"onstop 2 method invoked ",Toast.LENGTH_LONG).show();
}
@Override
protected void onDestroy() {
super.onDestroy();
Toast.makeText(this,"ondestroy 2 method invoked ",Toast.LENGTH_LONG).show();
}
@Override
protected void onPause() {
super.onPause();
Toast.makeText(this,"onpause 2 method invoked ",Toast.LENGTH_LONG).show();
}
@Override
protected void onResume() {
super.onResume();
Toast.makeText(this,"onResume 2 method invoked ",Toast.LENGTH_LONG).show();
}
@Override
protected void onStart() {
super.onStart();
Toast.makeText(this,"onstart 2 method invoked ",Toast.LENGTH_LONG).show();
}

@Override
protected void onRestart() {
super.onRestart();
Toast.makeText(this,"onrestart 2 method invoked ",Toast.LENGTH_LONG).show();
}

public void clickMe(View view) {


Intent intent=new Intent(this,MainActivity.class);
startActivity(intent);
}
}

Manifest file

<?xml version="1.0" encoding="utf-8"?>


<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.myfirstapp_btech">

<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:roundIcon="@mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="@style/AppTheme">
<activity android:name=".Main2Activity"></activity>
<activity android:name=".MainActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />

<category android:name="android.intent.category.LAUNCHER" />


</intent-filter>
</activity>
</application>

</manifest>

Assisment 2 Temperature converter

XML: activity_main
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="tmu.ac.in.btechcs.Programe2.Program2Activity">
<EditText
android:textSize="16sp"
android:id="@+id/et_temp"
android:hint="Enter Temp"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
<TextView
android:textSize="16sp"
android:id="@+id/tv_reault"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
<RadioGroup
android:id="@+id/rbg"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<RadioButton
android:text="Celcius"
android:id="@+id/rb_cel"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
<RadioButton
android:text="Farahnite"
android:id="@+id/rb_far"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
</RadioGroup>
<Button
android:layout_gravity="center_horizontal"
android:textSize="16sp"
android:text="Convert"
android:id="@+id/bt_submit"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />

</LinearLayout>

JAVA: MainActivity.java
package tmu.ac.in.btechcs.Programe2;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.RadioGroup;
import android.widget.TextView;

import tmu.ac.in.btechcs.R;

public class Program2Activity extends AppCompatActivity {

//T(°C) = (T(°F) - 32) / (9/5)


//T(f)=T(c)*5/9+32
EditText value;
Button hit;
RadioGroup group;
TextView result;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_program2);
value=findViewById(R.id.et_temp);
group=findViewById(R.id.rbg);
hit=findViewById(R.id.bt_submit);
result=findViewById(R.id.tv_reault);
hit.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
String uvalue=value.getText().toString();
int value=Integer.parseInt(uvalue);
int bid=group.getCheckedRadioButtonId();
if(bid==R.id.rb_cel){
convertcel(value);
}
if(bid==R.id.rb_far){
convertfar(value);

}
});
}

private void convertcel(int value) {


float celvalue=(value-32)/(9/5);
result.setText(celvalue+"");

}
private void convertfar(int value) {
float farvalue=(value*5/9)+32;
result.setText(farvalue+"");

}
}

Assignment 3 Simple Calculator

Activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context=".MainActivity">

<LinearLayout
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<EditText
android:hint="Enter First Value"
android:id="@+id/et_value1"
android:layout_weight="1"
android:layout_width="0dp"
android:layout_height="wrap_content" />
<EditText
android:hint="Enter Second value"
android:id="@+id/et_value2"
android:layout_weight="1"
android:layout_width="0dp"
android:layout_height="wrap_content" />

</LinearLayout>

<LinearLayout
android:layout_marginTop="16dp"
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<Button
android:id="@+id/bt_plus"
android:textSize="16sp"
android:text="+"
android:layout_weight="1"
android:layout_width="0dp"
android:layout_height="wrap_content" />
<Button
android:id="@+id/bt_minus"
android:textSize="16sp"
android:text="-"
android:layout_weight="1"
android:layout_width="0dp"
android:layout_height="wrap_content" />
<Button
android:id="@+id/bt_multi"
android:textSize="16sp"
android:text="*"
android:layout_weight="1"
android:layout_width="0dp"
android:layout_height="wrap_content" />
<Button
android:id="@+id/bt_div"
android:textSize="16sp"
android:text="/"
android:layout_weight="1"
android:layout_width="0dp"
android:layout_height="wrap_content" />

</LinearLayout>

<TextView
android:layout_marginTop="16dp"
android:text="Result"
android:layout_gravity="center_horizontal"
android:textSize="16sp"
android:id="@+id/tv_result"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />

</LinearLayout>

MainActivity.java
package com.example.mycalculator;

import androidx.appcompat.app.AppCompatActivity;

import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

public class MainActivity extends AppCompatActivity implements View.OnClickListener {


EditText value1,value2;
Button add,sub,multi,div;
TextView Result;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
value1=findViewById(R.id.et_value1);
value2=findViewById(R.id.et_value2);
add=findViewById(R.id.bt_plus);
sub=findViewById(R.id.bt_minus);
multi=findViewById(R.id.bt_multi);
div=findViewById(R.id.bt_div);
Result=findViewById(R.id.tv_result);
add.setOnClickListener(this);
sub.setOnClickListener(this);
multi.setOnClickListener(this);
div.setOnClickListener(this);
}

@Override
public void onClick(View view) {
float val1=Float.parseFloat(value1.getText().toString());
float val2=Float.parseFloat(value2.getText().toString());
float finalresult;
switch (view.getId()){
case R.id.bt_plus:{
finalresult=val1+val2;
Result.setText(finalresult+"");
break;
}
case R.id.bt_minus:{
finalresult=val1-val2;
Result.setText(finalresult+"");
break;
}
case R.id.bt_multi:{
finalresult=val1*val2;
Result.setText(finalresult+"");
break;
}
case R.id.bt_div:{
finalresult=val1/val2;
Result.setText(finalresult+"");
break;
}

}
4.Create the app shown below. Users are initially presented with an "unhappy"
character with the corresponding text "I'm so hungry". After hitting the button
"EAT COOKIE", the character becomes "happy" with corresponding text "I'm so
full".

<?xml version="1.0" encoding="utf-8"?>


XML : Activity_emoji
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context="tmu.ac.in.btechcs.P4.EmojiActivity">
<ImageView
android:layout_weight="5"
android:src="@drawable/imagetwo"
android:id="@+id/iv_first"
android:layout_width="match_parent"
android:layout_height="0dp" />
<TextView
android:background="@color/emoji"
android:layout_weight="0.5"
android:textSize="20sp"
android:text="Please Be safe"
android:id="@+id/tv_emoji"
android:layout_width="match_parent"
android:layout_height="0dp" />
<Button
android:background="@color/emoji"
android:onClick="change"
android:layout_weight="1"
android:text="Please use Mask"
android:id="@+id/bt_emoji"
android:layout_width="match_parent"
android:layout_height="0dp" />
</LinearLayout>

Java :EmojiActivity.java
package tmu.ac.in.btechcs.P4;
import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;
import tmu.ac.in.btechcs.R;
public class EmojiActivity extends AppCompatActivity {
ImageView imageView;
TextView text;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_emoji);
imageView=findViewById(R.id.iv_first);
text=findViewById(R.id.tv_emoji);
}
public void change(View view) {
//imageView.setImageResource(R.drawable.imageone);
//text.setText("Now I am using Mask");
Intent intent=new Intent(EmojiActivity.this,SecondEmojiActivity.class);
startActivity(intent);
}
}
-

Second_emoji.java
package tmu.ac.in.btechcs.P4;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import tmu.ac.in.btechcs.R;
public class SecondEmojiActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_second_emoji);
}
}

5 Create the app for tip calculator

XML activity_tip
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context="tmu.ac.in.btechcs.P5.TipActivity">
<TextView
android:layout_margin="16dp"
android:textSize="15sp"
android:background="@color/tipblue"
android:text="Enter your bill Amount"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
<EditText
android:layout_margin="16dp"
android:hint="Enter bill Amount"
android:id="@+id/et_bill"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
<TextView
android:layout_margin="16dp"
android:textSize="15sp"
android:background="@color/tipgreen"
android:text="Enter tip Percentage"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
<EditText
android:layout_margin="16dp"
android:hint="Enter Tip Percentage"
android:id="@+id/et_percent"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
<Button
android:onClick="Calculate"
android:layout_margin="16dp"
android:textColor="@color/tipgreen"
android:background="@color/tipbutton"
android:text="CALCULATE"
android:id="@+id/bt_calculatebill"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
<TextView
android:layout_margin="16dp"
android:textSize="20sp"
android:id="@+id/tv_billresult"
android:text="Result:"
android:layout_width="match_parent"
android:layout_height="wrap_content" />

JAVA:TipActivity.java
package tmu.ac.in.btechcs.P5;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;
import tmu.ac.in.btechcs.R;
public class TipActivity extends AppCompatActivity {
EditText bill,percentage;
TextView result;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_tip);
bill=findViewById(R.id.et_bill);
percentage=findViewById(R.id.et_percent);
result=findViewById(R.id.tv_billresult);
}
public void Calculate(View view) {
float fbill=Float.parseFloat(bill.getText().toString());//200
float fpercentage=Float.parseFloat(percentage.getText().toString());//5
float finalbill=(fbill*fpercentage)/100;
result.setText(finalbill+"");
}
}
-

6 Create a Chronometer Android application.

Activity_chronometer
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context="tmu.ac.in.btechcs.ChronoMeterActivity">
<Chronometer
android:layout_margin="16dp"
android:layout_gravity="center_horizontal"
android:id="@+id/chrono"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<Button
android:layout_margin="16dp"
android:textSize="15sp"
android:text="Start"
android:id="@+id/bt_start"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
<Button
android:layout_margin="16dp"
android:textSize="15sp"
android:text="Stop"
android:id="@+id/bt_stop"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
<Button
android:layout_margin="16dp"
android:textSize="15sp"
android:text="ReStart"
android:id="@+id/bt_restart"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
<Button
android:layout_margin="16dp"
android:textSize="15sp"
android:text="SetFormat"
android:id="@+id/bt_setformat"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
<Button
android:layout_margin="16dp"
android:textSize="15sp"
android:text="ClearFormat"
android:id="@+id/bt_clear"
android:layout_width="match_parent"
android:layout_height="wrap_content" /

Java: chronometeractivity.java
package tmu.ac.in.btechcs;
import android.os.SystemClock;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.Chronometer;
public class ChronoMeterActivity extends AppCompatActivity implements
View.OnClickListener {
Chronometer meter;
Button start,stop,restart,setformat,clearformat;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_chrono_meter);
meter=findViewById(R.id.chrono);
start=findViewById(R.id.bt_start);
stop=findViewById(R.id.bt_stop);
restart=findViewById(R.id.bt_restart);
setformat=findViewById(R.id.bt_setformat);
clearformat=findViewById(R.id.bt_clear);
start.setOnClickListener(this);
stop.setOnClickListener(this);
restart.setOnClickListener(this);
setformat.setOnClickListener(this);
clearformat.setOnClickListener(this);
}
@Override
public void onClick(View view) {
if(view.getId()==R.id.bt_start){
meter.start();
}
if(view.getId()==R.id.bt_stop){
meter.stop();
}
if(view.getId()==R.id.bt_restart){
meter.setBase(SystemClock.elapsedRealtime());
}
if(view.getId()==R.id.bt_setformat){
meter.setFormat("Time(%s)");
}
if(view.getId()==R.id.bt_clear){
meter.setFormat(null);
}
}
}
-

Assignment No7
Automorphic number Armstrong Number Disarium Number

Activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context=".MainActivity">
<EditText
android:hint="Enter number"
android:id="@+id/et_number"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
<Button
android:text="Automorphic"
android:id="@+id/Automorphic"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
<Button
android:text="Armstrong"
android:id="@+id/Armstrong"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
<Button
android:text="Disarium"
android:id="@+id/Disarium"
android:layout_width="match_parent"
android:layout_height="wrap_content" />

</LinearLayout>

MainActivity.java
package com.example.p7btech;

import androidx.appcompat.app.AppCompatActivity;

import android.os.Bundle;

import android.view.View;

import android.widget.Button;

import android.widget.EditText;
import android.widget.Toast;

public class MainActivity extends AppCompatActivity implements View.OnClickListener {

EditText value;

Button Automorphic,Armstrong,Disarium;

@Override

protected void onCreate(Bundle savedInstanceState) {

super.onCreate(savedInstanceState);

setContentView(R.layout.activity_main);

value=findViewById(R.id.et_number);

Automorphic=findViewById(R.id.Automorphic);

Armstrong=findViewById(R.id.Armstrong);

Disarium=findViewById(R.id.Disarium);

Armstrong.setOnClickListener(this);

Automorphic.setOnClickListener(this);

Disarium.setOnClickListener(this);

@Override

public void onClick(View view) {

switch (view.getId()){

case R.id.Automorphic:{

boolean check=
checkAutomorphic(Integer.parseInt(value.getText().toString()));

if(check)

Toast.makeText(this, " Automorphic", Toast.LENGTH_SHORT).show();


else

Toast.makeText(this, "not Automorphic",


Toast.LENGTH_SHORT).show();

break;

case R.id.Armstrong:{

boolean check=
checkArmstrong(Integer.parseInt(value.getText().toString()));

if(check)

Toast.makeText(this, " Armstrong", Toast.LENGTH_SHORT).show();

else

Toast.makeText(this, "not Armstrong", Toast.LENGTH_SHORT).show();

break;

case R.id.Disarium:{

boolean
check=checkDisarium(Integer.parseInt(value.getText().toString()));

if(check)

Toast.makeText(this, " Disarium", Toast.LENGTH_SHORT).show();

else

Toast.makeText(this, "not Disarium", Toast.LENGTH_SHORT).show();

break;

private boolean checkAutomorphic(int N) {

// Store the square


int sq = N * N;

// Start Comparing digits

while (N > 0) {

// Return false, if any digit of N doesn't

// match with its square's digits from last

if (N % 10 != sq % 10)

return false;

// Reduce N and square

N /= 10;

sq /= 10;

return true;

/* private boolean checkAutomorphic(int N)

int sq_num = N*N;

String str_num = Integer.toString(N);

String square = Integer.toString(sq_num);

if(square.endsWith(str_num))

return true;

else

return false;

} */
private boolean checkArmstrong(int x) {

// Calling order function

int n = order(x);

int temp = x;

double sum = 0;

while (temp != 0) {

int r = temp % 10;

sum=sum+ Math.pow(r,n);

temp = temp / 10;

// If satisfies Armstrong condition

return ((int)sum == x);

private boolean checkDisarium(int n) {

// Count digits in n.

int count_digits = Integer.toString(n).length();

// Compute sum of terms like digit multiplied by

// power of position

int sum = 0; // Initialize sum of terms

int x = n;

while (x!=0)

{
// Get the rightmost digit

int r = x%10;

// Sum the digits by powering according to

// the positions

sum = (int) (sum + Math.pow(r, count_digits--));

x = x/10;

// If sum is same as number, then number is

return (sum == n);

public int power(int x, long y)

int result=1;

if( y == 0)

return 1;

if (y%2 == 0)

while (y != 0)

result *= x;

--y;

}
return result;

/* Function to calculate order of the number */

public int order(int x)

int n = 0;

while (x != 0)

n++;

x = x/10;

return n;

You might also like