Question

Java two dimensional arrays Create a method named curiousNumber that takes an integer named num as...

Java two dimensional arrays

Create a method named curiousNumber that takes an integer named num as a parameter and returns a boolean. You can assume the num will be a positive integer.

The curiousNumber method should determine whether num is a curious number. A curious number is a number that is equal to the sum of the factorial of each of its digits. For example, 145 is a curious number because 145 = 1! + 4! + 5!.

sample:

boolean b1 = curiousNumber(145);      return value true

boolean b2 = curiousNumber(18);        return value false

0 0
Add a comment Improve this question Transcribed image text
Answer #1
public class CuriousNumber {

    public static boolean curiousNumber(int n) {
        int sum = 0, original = n;
        while (n > 0) {
            int d = n % 10;
            n /= 10;
            int f = 1;
            for (int i = 1; i <= d; i++) {
                f *= i;
            }
            sum += f;
        }
        return sum == original;
    }

    public static void main(String[] args) {
        System.out.println(curiousNumber(145));
        System.out.println(curiousNumber(18));
    }
}

Add a comment
Know the answer?
Add Answer to:
Java two dimensional arrays Create a method named curiousNumber that takes an integer named num as...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • Java arrays Create method named repeatedN that takes two integer parameters named range and n and...

    Java arrays Create method named repeatedN that takes two integer parameters named range and n and returns an integer array Method should return an integer array that has numbers 0 - range repeated in order n times If range is less than or equal to 0; return an array that has 0 repeated n times Create a second method named printArray that takes an integer array as a parameter. The method should print out every element on the same line...

  • In Java Write a method named addDigits which accepts two inputs, num and index. The first...

    In Java Write a method named addDigits which accepts two inputs, num and index. The first input parameter num is a 4-digit positive integer and index is an int value which refers to a digit that the summation should start from. For example if the given num is 2345 and the index is 1, starting from the second digit, the result of 12 is returned which is the sum of 3, 4, and 5. If the num is not a...

  • JAVA 1.Write a static method named getMaxEven(numbers) which takes an array of positive integers as a...

    JAVA 1.Write a static method named getMaxEven(numbers) which takes an array of positive integers as a parameter. This method calculates and returns the largest even number in the list. If there are no even numbers in the array, the method should return 0. You can assume that the array is not empty. For example: Test Result int[] values = {1, 4, 5, 9}; System.out.println(getMaxEven(values)); 4 System.out.println(getMaxEven(new int[]{1, 3, 5, 9})); 0 public static int --------------------------------------------------------------------------------- 2. Write a static method...

  • Programming 5_1: Create a Java class named <YourName>5_1 In this class create a main method...

    Programming 5_1: Create a Java class named <YourName>5_1 In this class create a main method, but for now leave it empty. Then write a Java method getAverage which takes an array of integers as it’s argument. The method calculate the average of all the elements in the array, and returns that average. The method should work for an array of integers of any length greater than or equal to one. The method signature is shown below: public static double getAverage(...

  • CH Question 1. Implement a recursive method fibo that takes an integer num as a parameter...

    CH Question 1. Implement a recursive method fibo that takes an integer num as a parameter and returns the nth Fibonacci number. Fibonacci sequence: 1, 1, 2, 3, 5, 8, 13, ... is an infinite sequence, where first 2 numbers are 1 and every subsequent number is equivalent to the sum of the previous two. Question 2. Implement a recursive method mult that takes two positive integer parameters, x and y and returns their product For example mult(2,3) returns 6....

  • *Java* Create a public static method named maxRes that takes an ArrayList of type Double and...

    *Java* Create a public static method named maxRes that takes an ArrayList of type Double and returns a double. This method returns the maximum result (do not return the original value) of taking the tangent of each value from the input

  • Write a method named factorial that accepts an integer n as a parameter and returns the...

    Write a method named factorial that accepts an integer n as a parameter and returns the factorial of n, or n!. A factorial of an integer is defined as the product of all integers from 1 through that integer inclusive. For example, the call of factorial(4) should return 1 2 3 4, or 24. The factorial of 0 and 1 are defined to be 1. You may assume that the value passed is non-negative and that its factorial can fit...

  • Java: Create the skeleton. Create a new file called ‘BasicJava4.java’ Create a class in the file...

    Java: Create the skeleton. Create a new file called ‘BasicJava4.java’ Create a class in the file with the appropriate name. public class BasicJava4 { Add four methods to the class that return a default value. public static boolean isAlphabetic(char aChar) public static int round(double num) public static boolean useSameChars(String str1, String str2) public static int reverse(int num) Implement the methods. public static boolean isAlphabetic(char aChar): Returns true if the argument is an alphabetic character, return false otherwise. Do NOT use...

  • In pseudocode only Design (pseudocode) a program (name it Occurrences) to determine whether two two-dimensional arrays...

    In pseudocode only Design (pseudocode) a program (name it Occurrences) to determine whether two two-dimensional arrays are equivalent or not. Two arrays are equivalent if they contain the same values in any order. The program main method defines two two-dimensional array of size 3-by-3 of type integer, and prompts the user to enter integer values to initialize the arrays. The main method calls method isEquivalent()that takes two two-dimensional arrays of integer and returns true (boolean value) if the arrays contain...

  • I need to create a function named hailstone that takes the starting integer as a parameter...

    I need to create a function named hailstone that takes the starting integer as a parameter and returns how many steps it takes to reach 1. You will stop when you reach the first one. If the starting integer is 1, the return value should be 0. The function should just return the value not display it. You CANNOT use recursion. C++

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT