Write a Java program to input a number, check it is Negative, Positive or Zero.
(using if else if statement).
Input: 67 Output: Positive
Input: -357 Output : Negative
Answers
Answer:
import java.util.Scanner;
class TypeOfNumber
{
public static void main(String[] args)
{
//Declaration
Scanner input = new Scanner(System.in);
int number;
//Prompt and accept an integer from user
System.out.println("Enter an integer: ");
number = input.nextInt();
//Display the type of integer
if (number > 0)
{
System.out.println(number + " : Positive");
}
if (number < 0)
{
System.out.println(number + " : Negative");
}
{
System.out.println(number + " : Zero");
}
}
}
The given problem is solved using language - Java.
import java.util.Scanner;
public class Number{
public static void main(String args[]){
System.out.print("Enter a number: ");
int a=(new Scanner(System.in)).nextInt();
if(a>0)
System.out.print("Number is positive.");
else if(a<0)
System.out.print("Number is negative.");
else
System.out.print("Number is zero.");
}
}
- Accept the number from the user, say a.
- Check if a > 0. If true, number is positive. If false, check if a < 0 or not. If true, number is negative or else, its zero.
See attachments for output.