Q:

Kotlin program to check whether given number is positive, negative or zero

belongs to collection: Kotlin Basic Programs

0

A positive number is a number which is greater than 0, a negative number is a number which is less than 0, else the number is Zero.

Given a number num, we have to check whether it is positive, negative, or zero.

Example:

    Input:
    num = 12321

    Output:
    12321 is positive

All Answers

need an explanation for this answer? contact us directly to get an explanation for this answer

Program to check number is positive, negative or zero in Kotlin

/*
Kotlin Program to Check a Integer Number is Positive or Negative or Zero
if       number > 0 = Positive Number
else if  number < 0 = Negative Number
else     Number is 0 (Zero)
*/

package com.includehelp.basic

import java.util.*

//Main Function, Entry Point of Program
fun main(args: Array<String>) {
    // InputStream to get Input
    val reader = Scanner(System.`in`)
    
    //Input Integer number
    print("Enter Integer Number : ")
    val num = reader.nextInt()
    
    // using if else 
    println("Using if else...")
    if (num > 0)
        println("$num is a Positive Number")
    else if (num < 0)
        println("$num is a Negative Number")
    else
        println("Entered Number is Zero")
    
    // using when statement (similar to switch case)
    println("Using when statement...")        
    when{
        num> 0 -> println("$num is a Positive Number")
        num< 0 -> println("$num is a Negative Number")
        else  -> println("Entered Number is Zero")
    }
}

Output

Run 1:
Enter Integer Number : -10
Using if else...
-10 is a Negative Number
Using when statement...
-10 is a Negative Number

----

Run 2:
Enter Integer Number : 10
Using if else...
10 is a Positive Number
Using when statement...
10 is a Positive Number

----
Run 2:
Enter Integer Number : 0
Using if else...
Entered Number is Zero
Using when statement...
Entered Number is Zero

need an explanation for this answer? contact us directly to get an explanation for this answer

total answers (1)

Kotlin Basic Programs

This question belongs to these collections

Similar questions


need a help?


find thousands of online teachers now
Kotlin program to check leap year... >>
<< Kotlin program to check whether a number is EVEN o...