1answer.
Ask question
Login Signup
Ask question
All categories
  • English
  • Mathematics
  • Social Studies
  • Business
  • History
  • Health
  • Geography
  • Biology
  • Physics
  • Chemistry
  • Computers and Technology
  • Arts
  • World Languages
  • Spanish
  • French
  • German
  • Advanced Placement (AP)
  • SAT
  • Medicine
  • Law
  • Engineering
blagie [28]
3 years ago
9

In Java, write a AllDigitsOdd program that has a method called allDigitsOdd that takes an integer and returns whether every digi

t of an integer is odd (boolean).
returns true if the integer consists entirely of odd digits (1,3,5,7,9)
returns false if any of its digits are even (0,2,4,6,8)
Consider the use of mod (%).
You should complete the method with integers only. You should not convert the integer to a String
Method call examples:
allDigitsOdd(135319) would return true
allDigitsOdd(-9145293) would return false
Your main method should:

//Prompt the user for an integer.
//You can assume the user always types an integer.
//Then print whether or not the number has all odd digits.
Computers and Technology
1 answer:
yan [13]3 years ago
8 0

Answer:

Hi there! The solution for this problem can be implemented a number of ways. Please find my solution below along with the explanation.

Explanation:

The prompts are fairly easily implemented using the Scanner class in Java for user input. In the allDigitsOdd function, we can perform a Math operation on the number being passed into the function to determine the number of digits in the number. We use the log10 function to do this and convert the double value returned to int. This is calculated in the code below as:  "(int)(Math.log10(number) + 1); ". Once we have the number of digits, we write a simple loop to iterate over each digit and perform a modulus function on it to determine if the digit is odd or even. I have declared 2 variables call odd and even that get incremented if the mod function (%) returns a 1 or a 0 respectively. Finally, the result is displayed on the screen.

AllDigitsOdd.java

import java.util.Scanner;

import java.lang.Math;

public class AllDigitsOdd {

 public static void main(String args[]) {

   // print main menu for user selection

   System.out.println("Please enter a number");

   Scanner scan = new Scanner(System.in);

   int selection = scan.nextInt();

   if (allDigitsOdd(selection)) {

     System.out.println("All numbers are odd");

   } else {

     System.out.println("All numbers are not odd");

   }

 }

 public static boolean allDigitsOdd(int number) {

   int digits = (int)(Math.log10(number) + 1);

   int even = 0;

   int odd = 0;

   for (int index = 1; index <= digits; index++) {

       if ((int)(number / Math.pow(10, index - 1)) % 10 % 2 == 0) {

         even += 1;

       } else {

         odd += 1;

       }

   }

   if (even == 0 && odd > 0) {

     return true;

   } else {

     return false;

     //if (odd > 0 && even == 0) {

     //  return true;

     //}

   }

 }

}

You might be interested in
What menu allows you to look up a command and learn how to use it?
e-lub [12.9K]
The answer is help because it helps you<span />
5 0
2 years ago
Which of the following is true about parallel computing performance?
Ierofanga [76]

Answer:

Computations use multiple processors. There is an increase in speed.

Explanation:

The increase in speed is loosely tied to the number of processor or computers used.

Mark me a brainliest answer

4 0
2 years ago
A client-server relationship is the basic form of a ____?
adell [148]
<span>
A client-server relationship is the basic form of a computer network. 

-hope this helps.</span>
4 0
3 years ago
Can anybody do Algorithm 2 for me (with Python).<br> Answer = 25 points.
Nostrana [21]

Answer:

age=int(input("Enter age"))

if age>=18:

    print("You are Young")

else

   print("You are child")

Explanation:

if you have any query or any problem kindly ask in comment

5 0
3 years ago
A(n) ____ consists of a series of related instructions, organized for a common purpose, that tells the computer what tasks to pe
yarga [219]

Answer:

The correct answer is "Program".  

Explanation:

Program is the collection of statement or instruction which is developed for creating any software or any purpose. The program is implemented or executed by a computer to perform a particular task.The particular programmer always writes an instruction to develop a program.

Program are always organized for the common purpose, that specifies the computer what tasks to perform as well as how to perform that particular task for example if programmer develops a program of calculator then it should be only used for the calculation purpose.

7 0
3 years ago
Other questions:
  • To insert a new slide in an existing presentation, what menu should you select?
    5·2 answers
  • Up to 10 people is a good guideline for the size of your study group.
    15·1 answer
  • Is a router on the local network that is used to deliver packets to a remote network?
    15·1 answer
  • What power brake uses vacuum from the engine to aid in brake application?
    6·2 answers
  • Which of the following is NOT a file format used for word processing documents? A. .ppt B. .rtf C. .doc D. .odt
    5·1 answer
  • Which of the following statements is/are correct? a. At the network layer, entitlement can map identity and/or attributes to fun
    8·1 answer
  • What type of error occurred with the code below??
    11·1 answer
  • Which are the 2 main elements that’s make up computer architecture?
    6·2 answers
  • Difference between a port and a connector
    10·1 answer
  • Use the drop-down menus to describe the customize ribbon dialog box.
    11·1 answer
Add answer
Login
Not registered? Fast signup
Signup
Login Signup
Ask question!