Science, asked by Anonymous, 13 hours ago

Write a java program to check if the input number is Even or Odd, based on the condition perform following calculation in the same java program (Output is compulsory to mention) (10 marks) i If the number is even display the square of the number in if statement.​

Answers

Answered by amanraj20120
0

Answer:

import java.util.Scanner;

public class EvenOdd {

public static void main(String[] args) {

Scanner reader = new Scanner(System.in);

System.out.print("Enter a number: ");

int num = reader.nextInt();

if(num % 2 == 0)

System.out.println(num + " is even");

else

System.out.println(num + " is odd");

}

}

Output

Enter a number: 12

12 is even

In the above program, a Scanner object, reader is created to read a number from the user's keyboard. The entered number is then stored in a variable num.

Now, to check whether num is even or odd, we calculate its remainder using % operator and check if it is divisible by 2 or not.

For this, we use if...else statement in Java. If num is divisible by 2, we print num is even. Else, we print num is odd.

We can also check if num is even or odd by using ternary operator in Java.

Example 2: Check whether a number is even or odd using ternary operator

import java.util.Scanner;

public class EvenOdd {

public static void main(String[] args) {

Scanner reader = new Scanner(System.in);

System.out.print("Enter a number: ");

int num = reader.nextInt();

String evenOdd = (num % 2 == 0) ? "even" : "odd";

System.out.println(num + " is " + evenOdd);

}

}

Output

Enter a number: 13

13 is odd

In the above program, we've replaced if...else statement with ternary operator (? :).

Here, if num is divisible by 2,"even" is returned. Else, "odd" is returned. The returned value is saved in a string variable evenOdd.

Then, the result is printed on the screen using string concatenation.

Answered by oOCuteDevilOo
1

Answer:

The Milky Way is the galaxy that includes the Solar System, with the name describing the galaxy's appearance from Earth: a hazy band of light seen in the night sky formed from stars that cannot be individually distinguished by the naked eye.

Attachments:
Similar questions