Question

Using Java, how would you test if a int number contains the digit 8 using recursive...

Using Java, how would you test if a int number contains the digit 8 using recursive functions?

public static boolean isLucky(int number)

0 0
Add a comment Improve this question Transcribed image text
Answer #1

Here is the answer for your question in Java Programming Language.

CODE :

import java.util.*;

import java.io.*;

public class Main

{

                public static void main(String[] args) {   

                    //Prompting user for input

                                System.out.println("Please enter an integer");

                                Scanner s = new Scanner(System.in);

                                int number = s.nextInt();   

            //Calling isLucky method with the user input as parameter

                                boolean result = isLucky(number);

                     //If the method returns true the number contains 8

                                if(result == true)

                                {

                                    System.out.println("Lucky number");

                                }

                                else //Else the number doesnot contain 8

                                {

                                    System.out.println("Not a Lucky number");

                                }

                }

                //Implementing isLucky() method

                public static boolean isLucky(int number)

                {

//Variables to hold user input and each digit to check for 8

                    int n = number,rem;

                    while(n>0) //Loop iterates through all the digits of the number

                    {

                        //Returns last digit as remainder

                    rem = n%10;

                    //Returns the number removing last digit

                    n = n/10;

                    if(rem!=8) //If the remainder does not contain digit 8

                    {

                        return isLucky(n);

//Recursively calling the same function

                    }

                    else if(rem == 8)

                    {

                        return true ; // Else returns true

                    }

                    }

                    return false;

//If there is no digit 8 in number returns false

                }

}

SCREENSHOTS:

Please see the screenshots of the code below for indentations of the code.

OUTPUT:

Any doubts regarding this can be explained with pleasure:)

Add a comment
Know the answer?
Add Answer to:
Using Java, how would you test if a int number contains the digit 8 using recursive...
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
  • C - Language Create a recursive function to print a multi-digit number vertically. For example, 2378...

    C - Language Create a recursive function to print a multi-digit number vertically. For example, 2378 should be printed as 2 3 7 8 Be sure to test your program with numbers of different length. The recursive function should return an int and take an int as a parameter. The function should have a base case where the parameter is 0 and this should return 0 Define a temp variable using the remainder operator where temp is equal to the...

  • on 2 Consider the following recursive method test: public static int test(String s, int last) if...

    on 2 Consider the following recursive method test: public static int test(String s, int last) if (last < 0) { return 0; if (s.charAt(last) == 0) { return 2 - test(s, last - 1); return 1 + 2 test(s, last - 1); What is the output of: test("11001", 4). TTT Arial 3 (12pt) - TEE 25

  • using RECURSIVE Functions in Java, create a public static String doubleLetters (String word) For ex) that...

    using RECURSIVE Functions in Java, create a public static String doubleLetters (String word) For ex) that returns “one” as “oonnee”

  • Java BNF How to write pseudocode for this snippet? Java code for finding a prime number...

    Java BNF How to write pseudocode for this snippet? Java code for finding a prime number public class Prime { public static void main(String[] args) { int num = 29; boolean flag = false; for(int i = 2; i <= num/2; ++i) { // condition for nonprime number if(num % i == 0) { flag = true; break; } } if (!flag) System.out.println(num + " is a prime number."); else System.out.println(num + " is not a prime number."); } }

  • How would you translate the following from Java to RISC-V? static int Y1[] = {13, 101,...

    How would you translate the following from Java to RISC-V? static int Y1[] = {13, 101, 79, 23, 154, 4, 11, 38, 88, 45, 17, 94, 62, 1}; static int lenY1 = 14; public static void main(String[] args) { quickSort(Y1,lenY1); } public static void quickSort(int[] x, int n) { qSort(x,0,n-1); }

  • Write a recursive method in java to find GCD of two integers using Euclid's method. Integers...

    Write a recursive method in java to find GCD of two integers using Euclid's method. Integers can be positive or negative. public class Recursion {                 public static void main(String[] args) {                                 Recursion r = new Recursion();                                 System.out.println(“The GCD of 24 and 54 is “+r.findGCD(24,54)); //6                 }                 public int findGCD(int num1, int num2){                                 return -1;                 } }

  • Java, how would i do this public static void main(String[] args) { int n = 3;...

    Java, how would i do this public static void main(String[] args) { int n = 3; int result; result = factorial(n); + public static int factorial(int n) public static int factorial(int n) n = 3 { returns 3* 2 * 1 { if (n == 1) return n; else return (n * factorial(n-1)); if (n == 1) return n; else return (3 * factorial(3-1)); ܢܟ } public static int factorial(int n) n = 2 public static int factorial(int n) returns...

  • MUST BE IN JAVA AND USE RECURSION Write a recursive method that returns the number of...

    MUST BE IN JAVA AND USE RECURSION Write a recursive method that returns the number of all occurrences of a given word in all the files under a directory. Write a test program. Use the following header: public static long findInFile(File file, String word)

  • Write a Java application that implements the recursive Binary Search alogrithm below: Write a Java application...

    Write a Java application that implements the recursive Binary Search alogrithm below: Write a Java application that implements the recursive Binary Search alogrithm below:/** * Performs the standard binary search using two comparisons * per level. This is a driver that calls the recursive method * @return index where item is found or NOT FOUND if not found */public static <AnyType extends Comparable<? super AnyType>> int binarySearch(AnyType [] a, AnyType x) {return binarySearch(a, x, 0, a.length -1);}/** * Hidden recursive...

  • Please code this in Java, thank you! (a) Implement a sublinear running time complexity recursive function...

    Please code this in Java, thank you! (a) Implement a sublinear running time complexity recursive function in Java public static long long x, int n) to calculate X Note: In your function you can use only the basic arithmetic operators (+, -,, %, and /) (b) What is the running time complexity of your function? Justify (c) Give a number of multiplications used by your function to calculate x63.

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