Determining whether a number is positive, or negative is a fundamental task in programming. Kotlin, with its concise syntax and expressive type system, provides several ways to perform this check. In this article, we will explore three different Kotlin Program to Check Whether a Number is Positive or Negative t. Each solution will be explained and accompanied by sample outputs.
1. Using Basic Conditional Statements
1.1 Simple If-Else Approach
fun checkNumber(num: Int): String {
return if (num > 0) {
"$num is a positive number."
} else if (num < 0) {
"$num is a negative number."
} else {
"$num is neither positive nor negative."
}
}
fun main() {
val num = 10
println(checkNumber(num))
}
Output:
10 is a positive number.
Explanation: This approach uses basic if-else
statements to determine if a number is positive, negative, or zero. It checks if the number is greater than, less than, or equal to zero and returns the corresponding message.
1.2 Using When Expression
fun checkNumber(num: Int): String {
return when {
num > 0 -> "$num is a positive number."
num < 0 -> "$num is a negative number."
else -> "$num is neither positive nor negative."
}
}
fun main() {
val num = -5
println(checkNumber(num))
}
Output:
-5 is a negative number.
Explanation: The when
expression in Kotlin provides a more readable and concise way to check multiple conditions. It evaluates the conditions in sequence and returns the appropriate message.
2. Using Extension Functions
2.1 Extension Function Approach
fun Int.checkSign(): String {
return when {
this > 0 -> "$this is a positive number."
this < 0 -> "$this is a negative number."
else -> "$this is neither positive nor negative."
}
}
fun main() {
val num = 0
println(num.checkSign())
}
Output:
0 is neither positive nor negative.
Explanation: This approach extends the Int
class with a checkSign
function, making it possible to call the function directly on an integer. This enhances code readability and reusability.
2.2 Extension Function with Custom Message
fun Int.checkSign(customMessage: String): String {
return when {
this > 0 -> "$this is a positive number. $customMessage"
this < 0 -> "$this is a negative number. $customMessage"
else -> "$this is neither positive nor negative. $customMessage"
}
}
fun main() {
val num = 15
println(num.checkSign("This is a custom message."))
}
Output:
15 is a positive number. This is a custom message.
Explanation: This approach builds on the previous extension function by adding a parameter for a custom message. It demonstrates how to extend functionality while maintaining code clarity.
3. Using Java Standard Library
3.1 Using Integer.signum
Method
import java.lang.Integer
fun checkNumber(num: Int): String {
return when (Integer.signum(num)) {
1 -> "$num is a positive number."
-1 -> "$num is a negative number."
else -> "$num is neither positive nor negative."
}
}
fun main() {
val num = -20
println(checkNumber(num))
}
Output:
-20 is a negative number.
Explanation: This approach leverages the Integer.signum
method from the Java standard library to determine the sign of the number. The signum
method returns 1 for positive numbers, -1 for negative numbers, and 0 for zero.
3.2 Using compareTo
Method
fun checkNumber(num: Int): String {
return when {
num.compareTo(0) > 0 -> "$num is a positive number."
num.compareTo(0) < 0 -> "$num is a negative number."
else -> "$num is neither positive nor negative."
}
}
fun main() {
val num = -100
println(checkNumber(num))
}
Output:
-100 is a negative number.
Explanation: This approach uses the compareTo
method, which is part of Kotlin’s standard library. The compareTo
method compares the number with zero and returns a positive, negative, or zero value based on the comparison.
Conclusion
In this article, we explored various methods to check whether a number is positive or negative using Kotlin. We used basic conditional statements, extension functions, and the Java standard library. Each method has its own advantages, and the choice depends on the specific needs of your application. By understanding these different approaches, you can select the most appropriate one for your Kotlin projects.