Factorial program in android

0 votes

Hey guys, I am aware that there are numerous Java apps available for computing a number's factorial, however I am having trouble with Android. My code is as follows. Thanks

package com.droidacid.apticalc.aptitudes;

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

import com.droidacid.apticalc.R;

public class AptiFactorial extends Activity implements android.view.View.OnClickListener{
EditText number;
TextView answer;
Button calculate;

long factorial = 1;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.apti_factorial);
    initialize();
    calcFactorial();
}
private void initialize() {
    number = (EditText) findViewById(R.id.et_apti_number);
    answer = (TextView) findViewById(R.id.tv_apti_answer);
    calculate = (Button) findViewById(R.id.b_apti_calc);
    calculate.setOnClickListener(this);
}
private void calcFactorial() {
    if (number.getText().toString().equals("")) number.setText("0");
    int num = Integer.parseInt(number.getText().toString());
    for(int i = 1; i<=num; i++){
        factorial = i * factorial;
    }

}


@Override
public void onClick(View v) {
    calcFactorial();
    answer.setText("Factorial of " + number.getText().toString() + " is : " +     factorial);

}

}

This is my code, and I need to know how to set the hint to something other than 0. However, removing if (number.getText (). number = toString().equals("")) setText("0");

receiving a NullPointerException after that Additionally, the calculation is correct the first time, but if I recalculate, the result is incorrect. Because I am utilising the factorial value directly, I believe there is a looping issue.

Sep 21, 2022 in Android by Edureka
• 13,620 points
4,817 views

No answer to this question. Be the first to respond.

Your answer

Your name to display (optional):
Privacy: Your email address will only be used for sending these notifications.

Related Questions In Android

0 votes
0 answers

How to make grid view scroll horizontally not vertically in android?

I use a flexible Grid View. means ...READ MORE

Sep 21, 2022 in Android by Edureka
• 13,620 points
520 views
0 votes
0 answers

Unable to detect adb version, adb output in Android Studio 3.3.2

I have done following task for solutions; Download ...READ MORE

Sep 21, 2022 in Android by Edureka
• 13,620 points
1,338 views
0 votes
0 answers

What does LayoutInflater in Android do?

What is the use of LayoutInflater in ...READ MORE

Sep 22, 2022 in Android by Edureka
• 13,620 points
426 views
0 votes
1 answer

Open Contact information in Contact List using Phone Number of Contact Android Studio

You must first obtain the contact's CONTACT ...READ MORE

answered Nov 7, 2022 in Android by Edureka
• 12,690 points
1,050 views
0 votes
1 answer

How to turn on front flash light programmatically in Android?

For this problem you should: Check whether the ...READ MORE

answered Nov 8, 2022 in Android by Edureka
• 12,690 points
1,610 views
0 votes
1 answer

How to display Toast in Android?

Use the show() function of the Toast ...READ MORE

answered Nov 8, 2022 in Android by Edureka
• 12,690 points
1,433 views
0 votes
1 answer

Running docker on Android

According to the documentation, the Android kernel is ...READ MORE

answered Aug 1, 2018 in Docker by Kalgi
• 52,350 points
3,763 views
0 votes
1 answer

Task Canceled Exception while invoking AWS Lambda

I'm guessing either the TaskCanceledException instance is ...READ MORE

answered Sep 19, 2018 in AWS by Priyaj
• 58,020 points
2,460 views
0 votes
1 answer

Is there a way to run Python on Android?

YES! here’s a barcode scanner written in six ...READ MORE

answered Sep 19, 2018 in Python by Priyaj
• 58,020 points
1,018 views
0 votes
1 answer

How can we get the current location in Android?

First you need to define a LocationListener to handle ...READ MORE

answered Sep 25, 2018 in Java by Parth
• 4,640 points
921 views
webinar REGISTER FOR FREE WEBINAR X
REGISTER NOW
webinar_success Thank you for registering Join Edureka Meetup community for 100+ Free Webinars each month JOIN MEETUP GROUP