Question

8) Write the Java code to Declare an Integer Number and Initialize it to value 10. Then use Ternary Operator to Check if the
0 0
Add a comment Improve this question Transcribed image text
Answer #1

import java.lang.*;

class ODDorEVEN
{
   public static void main(String[] args)
       {
       Integer number;//declaring Integer number
           // Here Integer is a class in java.lang package
           number=10;//initializing number to 10
          
           System.out.println(((number%2)==0)?"even": "odd");
       }
   }

/*ternary operator (condition? a:b)  

here if the condition is true it returns a else it returns b

above program :

  • Integer is a class in java.lang package
  • Integer (10) is constructor used to initialize number to 10
  • System.out.println is use to print output in console
  • 10%2 is 0 so the condition is true , gives the output as even
  • number which gives reminder 0 by dividing with 2 is even number
  • number which gives reminder 1 by dividing with 2 is odd number*/E:ljavapgms javac ODDorEVEN.java E ljavapgms>java ODDorEVEN even E:javapgms>
Add a comment
Know the answer?
Add Answer to:
8) Write the Java code to Declare an Integer Number and Initialize it to value 10....
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 Eclipse program Write statements to declare and initialize eight constants, one for each of the...

    Java Eclipse program Write statements to declare and initialize eight constants, one for each of the primitive types in Java. boolean, byte, char, short, int, long, float and double. Add statements to print the name of the constant and its value to your VariableDefinitions.javaprogram and compile the program. Assign a new value to one of the constants and compile. Comment out the lines of code that cause compilation errors but do not delete them and recompile your program.

  • [C++] Declare three Boolean flags and initialize it as with false value. Ask the user to...

    [C++] Declare three Boolean flags and initialize it as with false value. Ask the user to enter a number, if the number is even make the first flag true, if the number is odd make the second flag true, and if the value entered by the user was not a number make the third flag true. Print out the values of the three flags along with a proper message telling the user about what he/she entered.

  • In C language 1. Write a program to declare and initialize an array of size 5...

    In C language 1. Write a program to declare and initialize an array of size 5 and place odd numbers 3, 5, 7,9 and 11 in it. Declare and initialize another array of size 5 and place even numbers 4, 6, 8, 10 and 12 in it. Write a for loop to add each element and place it in a third array of the same dimensions. Display each array.

  • Write a complete C program that does the following: . o Declare a and b as...

    Write a complete C program that does the following: . o Declare a and b as character variables. Initialize a to this value: 127 o Declare c as an unsigned character variable o Multiply a by 2 and store the result in variable b. o Multiple a by 2 and store the result in variable c. Print the values of a, b, and c. Use the %d specifier in each case. Declare d as an integer variable. Initialize d to...

  • Write a C program to do the following 1) request user to enter 10 integer into...

    Write a C program to do the following 1) request user to enter 10 integer into an array 2) sort the array in ascending order 3) display the sorted array 4) count the number of odd and even number in the array and print out the result 5) add the value of the odd number and even number and calculate the average of the odd and even number. display the result 6) write function addNumber() to add all the number...

  • Write a Java program that does the following. a. Declare an integer 2D array with 5...

    Write a Java program that does the following. a. Declare an integer 2D array with 5 rows and 5 columns. b. Initialize the array's elements to random integers between 1 and 10 (inclusive). c. Display all the elements in the 2D array as a table of rows and columns. d. Display the row index and column index of all the even integers in the 2D array. e. Display the sum of first row's elements.

  • Write code that declares an array of integer values that will represent the first five even...

    Write code that declares an array of integer values that will represent the first five even numbers: 2, 4, 6, 8, 10. Add code to initialize each element of the array with the even number values shown above. Add code to calculate the product of the array elements, and store the result in a variable named product. You must access the array elements to accomplish this. Do not write product = 2 * 4 * 6 * 8 * 10;...

  • Evaluate Exponent: Declare a double variable called result and initialize it to 1.0; Ask the user...

    Evaluate Exponent: Declare a double variable called result and initialize it to 1.0; Ask the user for two values - one for the base, and the other for the exponent. If the exponent parameter is 0, give the user an error. You do not need to handle a negative exponent value. Otherwise, declare an integer variable called loop and initialize it to 0 Write a do while loop as follows: Inside the loop: Multiply the results times the number Increment...

  • Write code to repeatedly ask the user for a number. If the number is positive and...

    Write code to repeatedly ask the user for a number. If the number is positive and even, print out a happy message. If the number is positive and odd, print out a sad message. If the number is negative, print out an angry message If they enter zero, stop asking If they enter something that isn't an integer, print out a message telling them they are an idiot, but keep looping

  • [In x86 Assembly Language] Declare a double word array of size 10 and initialize it with...

    [In x86 Assembly Language] Declare a double word array of size 10 and initialize it with some numbers. Find the number of even numbers and the number of odd numbers in this array and save them in locations called oddCount and evenCount.

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