WAP in Java to input a number and deduce if the
entered number is positive or negative.
☞note: I want program not explanation
Answers
Required Answer:-
Question:
- Write a program in Java to input a number and check if the entered number is positive or negative.
Solution:
Here comes the códe.
import java.util.*;
public class Number {
public static void main(String[] args) {
int n;
Scanner sc=new Scanner(System.in);
System.out.print("Enter an integer: ");
n=sc.nextInt();
if(n>0)
System.out.println("Number is positive.");
else if(n<0)
System.out.println("Number is negative.");
else
System.out.println("Its zero.");
sc.close();
}
}
Logic:
- Take the number as input.
- Check if the number is greater than 0. If true, the number is positive. If not true, check if the number is less than 0 or not. If true, the number is negative or else, the number is zero.
Refer to the attachment for output ☑.
WAP in Java to input a number and deduce if the entered number is positive or negative.
Public Class CheckPositiveOrNegative
{
public static void main(String[] args)
{
//number to be check
int num=(Enter Any Number);
//checks the number is greater than 0 or not
if(num>0)
{
System.out.println("The number is positive.");
}
//checks the number is less than 0 or not
else if(num<0)
{
System.out.println("The number is negative.");
}
//executes when the above two conditions return false
else
{
System.out.println("The number is zero.");
}
}
}
꧁Hopes so that this will help you,꧂
♥️With Love♥️
✌︎Thanking you, your one of the Brother of your 130 Crore Indian Brothers and Sisters.✌︎