Function/Methods

Method merupakan perwujudan aksi atau tindakan dari dunia nyata di dalam pemrograman komputer.

Function/Methods
Java Program to Check whether a Number is Positive, Negative or Zero
public class PositiveOrNegative 
{
    public void check(int number) 
{
if (number > 0) 
{
System.out.println(number + " is a positive number");
else if (number < 0) 
{
System.out.println(number + " is a negative number");
else 
{
System.out.println(number + " is neither positive nor negative");
}
}
}

The method takes an integer 'number' as an argument. 
We use if-else-if decision making statements to decide whether the number is positive or negative. 
The condition 'number < 0' takes care of negative number 
while the condition 'number > 0' takes care of positive numbers. 
If both the above conditions are false, the number is zero.

sample outputs :

Input:
number = 7

Output :
7 is a positive number

Input:
number = -34

Output :
-34 is a negative number

Input:
number = 0

Output :
0 is neither positive nor negative

Comments

Popular Posts