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
Develop a c program to display the following input output interphase enter a number 5
aniked [119]

Answer:

int main()

{

int number;

printf("Enter a number: ");

scanf_s("%d", &number, sizeof(number));

for (int i = 1; i <= 2; i++) {

 printf("%d*%d=%d\n", number, i, number * i);

}

}

Explanation:

I used the safe scanf_s() that takes a third parameter to indicate the size of the buffer. In this case it is the size of an integer.

7 0
3 years ago
What is the "key" to a Caesar Cipher that someone needs to know (or discover) to decrypt the message? a) A secret word only know
Tpy6a [65]

Answer:

The number of characters to shift each letter in the alphabet.

Explanation:

Caeser Cipher is the technique of encryption of data, to make it secure by adding characters between alphabets. These are the special characters that make the message secure while transmitting.

According to the standards, For Decryption, we remove these special characters between alphabets to make message understandable.

<em>So, we can say that,to de-crypt the message, the number of characters to shift each letter in the alphabet.</em>

3 0
3 years ago
How will technology affect us in the future
Ivahew [28]

Answer:

In the future, technology will most likely take over. There will be new devices that probably predict natural disasters 10 minutes before they happen. And newer, smarter artificial inteligience. Or maybe even robots. MAYBE we don't even have to worry about making money, we have robots to do it. But, that will lose many jobs. Which is the only downside.

Explanation:

8 0
3 years ago
Is the XS or the XR better for gaming?
r-ruslan [8.4K]
In my opinion I think that the iPhone XR is better for gaming.
7 0
3 years ago
Read 2 more answers
Employee X receives phone calls from his boss during dinner.
Novay_Z [31]

Answer:

Because his boss is not fair

Explanation:

8 0
3 years ago
Read 2 more answers
Other questions:
  • Help plz
    5·1 answer
  • What is a main cause of a virus on a computer
    6·1 answer
  • Define a method pyramidVolume with double parameters baseLength, baseWidth, and pyramidHeight, that returns as a double the volu
    11·2 answers
  • (a) Explain the term formatting a disk for storage.​
    11·1 answer
  • Explain what a wiki is and list its advantages.
    5·1 answer
  • Do people answer questions more on this site or be on social more ??? no right or wrong answer your opinion
    6·1 answer
  • A coffee shop is considering accepting orders and payments through their phone app and have decided to use public key encryption
    10·1 answer
  • Jasmine took many pictures at a photo shoot. She wants to transfer these pictures from her camera to her laptop for image enhanc
    6·1 answer
  • How dose society use computer in government?​
    5·1 answer
  • What programs are most likely affected by a macro virus?
    11·1 answer
Add answer
Login
Not registered? Fast signup
Signup
Login Signup
Ask question!