I always do coding in mobile, today I am going to explain how you can easily write a java programming to check whether a number is positive or negative.
👉 To check whether a number is positive or negative, first of all we have to be clear about how to know a number is positive or negative
Then see...
Source code of the program
package CodeYourslf.BasicProgram;
import java.util.Scanner;
public class checkForPositiveOrNegative {
public static void main(String args[]) {
int num;
Scanner sc = new Scanner(System.in);
System.out.println("Enter the number to check positive or negative");
num = sc.nextInt();
if (num > 0) {
System.out.println("The number is positive");
} else {
System.out.println("The number is negative");
}
}
}
Output:-
If a number is greater than 0 (num > 0), then we can say the number is positive.
Otherwise the number will be negative, I mean if the given number is less than 0 it will called as negative number(num < 0).
How to code in mobile to check positive or negative of a number in java :-
As I have mentioned above that if the number is greater than 0, then the number is positive. Now if we can able to put a condition that will show the number is greater or lesser than 0, then we can easily determine whether the number is positive or negative.
- Now in java we have greater operator (>) by using that operator we can easily determine, a given number is positive or not.
see...
num > 0 return positive.
num < 0 return negative
- To perform this method in java, we have if else statement. By the help of if else we can easily check whether a number is positive or negative.
Also Read:-
- next() vs nextLine() in java :- click here
- Top 5 programming language in 2021 :- click here
Which app I use to code in mobile :-
👉 To code every java problem in mobile, I use 'Java N-IDE' , here also I use this android IDE to code in mobile for the java programming problem i.e how to check whether a number is positive or negative.
Here is the code that I have written in mobile by using 'Java N-IDE' :-
Code Explanation that I have written in mobile phone :-
Step-1
- First I create a class i.e
- public class checkForPositiveOrNegativs
Step-2
- Now I will create the main method i.e
- public static void main(String args[]);
Step-3
- Inside the main method I have to declare a variable of interger type to store the number that will give the user i.e
- int num;
Step-4
- Now time to receive user input of interger type, so we have to import Scanner class and have to create a object to that Scanner class i.e
- import java.util.Scanner;
- Scanner sc = new Scanner(System.in);
Step-5
- Now inside if block we have to check the condition i.s " num > 0 ", if this condition satisfy, then we have to show this number is positive or otherwise negative.
if (num > 0) {
System.out.println("The number is positive");
}
else{
System.out.println("The number is negative");
}
Also Read:-
- Plankalkül : World first programming language :- click here
- How receive user input in java :- click here
👉 We everyone knows that in if else condition if one condition satisfy then the program doesn't enter to another block, I mean if the here if condition satisfy then the program will execute the part " The number is positive". Otherwise it will execute the part "The number is negative" .