Question

Functions can return a string, not just an int or a float. Write a function called...

Functions can return a string, not just an int or a float. Write a function called everOrOdd which takes an integer parameter and returns either "Even" or "Odd" based on the value passed. In main, prompt the user for a number, called num, then pass num to evenOrOdd and display the returned value on the screen. Keep doing this until the user enters a zero. Use the following run as an example:

Enter a number: 11

Odd

Enter a number: 12

Even

Enter a number: 13

Odd

Enter a number: 0

0 0
Add a comment Improve this question Transcribed image text
Answer #1
//EverOrOdd.java
import java.util.Scanner;
public class EverOrOdd {
    public static String everOrOdd(int n){
        if(n%2==0){
            return "Even";
        }
        else{
            return "Odd";
        }
    }

    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        int n;

        System.out.print("Enter a number: ");
        n = scanner.nextInt();

        while (n!=0) {
            System.out.println(everOrOdd(n));
            System.out.print("Enter a number: ");
            n = scanner.nextInt();
        }
    }
}
Add a comment
Know the answer?
Add Answer to:
Functions can return a string, not just an int or a float. Write a function called...
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
  • I need java code for the following problem. Lab 7: Methods 1. Write a Java program called Numbers that calls the following methods and displays the returned value: Write a method called cubelt that a...

    I need java code for the following problem. Lab 7: Methods 1. Write a Java program called Numbers that calls the following methods and displays the returned value: Write a method called cubelt that accepts one integer parameter and returns the value raised to the third power as an integer. o Write a method called randominRange that accepts two integer parameters representing a range. The method returns a random integer in the specified range inclusive. 2. o Write a method...

  • In C Write a couple of functions to process arrays. Note that from the description of...

    In C Write a couple of functions to process arrays. Note that from the description of the function you have to identify what would be the return type and what would be part of the parameter. display(): The function takes an int array and it’s size and prints the data in the array. sumArray(): It takes an int array and size, and returns the sum of the elements of the array. findMax(): It takes an int array and size, and...

  • Write the definitions of the member functions of the class integerManipulation not given in Example 10-11....

    Write the definitions of the member functions of the class integerManipulation not given in Example 10-11. Also, add the following operations to this class: Split the number into blocks of n-digit numbers starting from right to left and find the sum of these n-digit numbers. (Note that the last block may not have ndigits. If needed add additional instance variables.) Determine the number of zeroes. Determine the number of even digits. Determine the number of odd digits Also, write a...

  • In C++: A. Write 3 functions that are called in the program. 1. Function readInput prompt...

    In C++: A. Write 3 functions that are called in the program. 1. Function readInput prompt user to enter an integer to store to the parameter. 2. Function isPerfectSquare take an integer parameter and checks whether it's a perfect square, that it's square root is an integer. 3. Function min3 return the mainimum value of the parameter values, it shouldn’t use any if statement to compare the parameters but call min2 to do the comparison. Extra Credit: Function printPrimeFactorization print...

  • 1.Write code for a Java method, printArray, that takes an int array as parameter and prints...

    1.Write code for a Java method, printArray, that takes an int array as parameter and prints it out, one element per line. public static void printArray (int[] values){ Methods that do NOT return a result have “void” as return type. Notice how an array parameter type is passed, int [] }// end printArray 2.Write code for a Java method that takes in as input parameter an integer number, say num, and returns a double array of size num with all...

  • *Please write in code in C* First, write a function called prime_check(int x) that takes an...

    *Please write in code in C* First, write a function called prime_check(int x) that takes an integer number as an input then return 1 if it is a prime number nd returns 0 if it is a composite number Then, Write a program that prompt the user to input two numbers: Print the multiplication of these two numbers if both of them are prime. Or Print " Sorry at least one of your number is composite" if otherwise

  • Required functions def inputWithinRange (lowValue, highValue): This function will ask for a number from the keyboard....

    Required functions def inputWithinRange (lowValue, highValue): This function will ask for a number from the keyboard. The function will only return a value that is within the inclusive range of lowValue to highValue. If the entered value is not within the provided range, the function will ask again until the user has entered an acceptable value. The function will return the accepted value. def doContinue (prompt): This function will ask for a string using the prompt parameter. The function will...

  • #include <iostream> //write preprocessor file for string datatype here using namespace std; //write your function prototypes...

    #include <iostream> //write preprocessor file for string datatype here using namespace std; //write your function prototypes here: int main() {     cout << "Welcome to Mad Lib.\n\n";     cout << "Answer the following questions to help create a new story.\n";     string name = askText("Please enter a name: ");          string noun = askText("Please enter a plural noun: ");          int number = askNumber("Please enter a number: ");          string bodyPart = askText("Please enter a body part: ");          string...

  • In C++ Write a function int * return_matches(int a[], int & size,TEST t) which returns an...

    In C++ Write a function int * return_matches(int a[], int & size,TEST t) which returns an array (allocated off of the heap in the function) containing only the elements of array a that pass the “test” t. The function iterates through all the elements of a, and constructs a new array containing all the elements that pass the test. When the parameter corresponding to “size” is passed in (by reference), it contains the size of the array a. In the...

  • Lisp Scheme Write a procedure in Lisp Scheme called charflip that takes a string as a...

    Lisp Scheme Write a procedure in Lisp Scheme called charflip that takes a string as a parameter and returns a copy of this string, flipping the case of every character in an odd position (index 1, 3, 5, etc..). I suggest that you do this recursively. This means cons, car and cdr will come in handy, as will these functions: • char-upper-case? • char-downcase • char-upcase • list->string • string->list Here’s this function in action. scheme@(guile-user)> (charflip "hatburg") $16 =...

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
ADVERTISEMENT