8242. Positive, negative or zero

 

One integer n is given. Print whether it is positive, negative, or equal to 0.

 

Input. One integer n, no more than 109 by absolute value.

 

Output. Print Positive, Negative or “Zero depending on the value of n.

 

Sample input 1

Sample output 1

45

Positive

 

 

Sample input 2

Sample output 2

0

Zero

 

 

SOLUTION

conditional statement

 

Algorithm analysis

Use a conditional statement to find the sign of the number.

 

Algorithm implementation

Read the input value of n.

 

scanf("%d",&n);

 

Determine the sign of the number: positive, negative or zero.

 

if (n > 0) puts("Positive"); else

if (n < 0) puts("Negative"); else

           puts("Zero");

 

Algorithm implementation – switch

 

#include <stdio.h>

 

int n;

 

int main(void)

{

  scanf("%d", &n);

  switch (n > 0)

  {

    case 1:

      puts("Positive");

      break;

    case 0:

      switch (n < 0)

      {

        case 1:

          puts("Negative");

          break;

        default:

          puts("Zero");

      }

  }

  return 0;

}

 

Java implementation

 

import java.util.*;

 

public class Main

{

  public static void main(String []args)

  {

    Scanner con = new Scanner(System.in);

    int n = con.nextInt();

 

    if (n > 0) System.out.println("Positive"); else

    if (n < 0) System.out.println("Negative"); else

               System.out.println("Zero");

 

    con.close();

  }

}

 

Python implementation

Read the input value of n.

 

n = int(input())

 

Determine the sign of the number: positive, negative or zero.

 

if n > 0: print("Positive")

elif n < 0: print("Negative")

else: print("Zero")