Positive, Negative or Zero: flowchart, algorithm and program

This program reads one integer n and prints whether it is Positive, Negative or Zero. It compares n with 0 using an if / else if / else chain, so exactly one message is printed.

▶ Watch the dry run step by step →

Algorithm

  1. Step 1: Start.
  2. Step 2: Read n.
  3. Step 3: If n > 0, go to step 4; otherwise go to step 5.
  4. Step 4: Print "Positive". Go to step 8.
  5. Step 5: If n < 0, go to step 6; otherwise go to step 7.
  6. Step 6: Print "Negative". Go to step 8.
  7. Step 7: Print "Zero".
  8. Step 8: Stop.

Pseudocode

BEGIN
    READ n
    IF n > 0 THEN
        PRINT "Positive"
    ELSE
        IF n < 0 THEN
            PRINT "Negative"
        ELSE
            PRINT "Zero"
        END IF
    END IF
END

Program in C, Python and Java

Positive, Negative or Zero program in C

#include <stdio.h>

int main() {
    int n;
    printf("Enter a number: ");
    scanf("%d", &n);
    if (n > 0)
        printf("Positive\n");
    else if (n < 0)
        printf("Negative\n");
    else
        printf("Zero\n");
    return 0;
}

Positive, Negative or Zero program in Python

n = int(input("Enter a number: "))
if n > 0:
    print("Positive")
elif n < 0:
    print("Negative")
else:
    print("Zero")

Positive, Negative or Zero program in Java

import java.util.Scanner;

public class Main {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        System.out.print("Enter a number: ");
        int n = sc.nextInt();
        if (n > 0)
            System.out.println("Positive");
        else if (n < 0)
            System.out.println("Negative");
        else
            System.out.println("Zero");
    }
}

Sample input: n = -5
Output: Enter a number: Negative

How it works

  • scanf reads the number the user types and stores it in n.
  • First check: if n > 0, the program prints Positive and skips the rest.
  • If that fails, else if n < 0 runs and prints Negative.
  • If both checks fail, n can only be 0, so the else part prints Zero.
  • For n = -5: n > 0 is false, n < 0 is true, so the output is Negative.

Dry run (trace table)

StepLineBoxnOutput
13Start
26Read n-5Enter a number:
37n > 0?-5
49n < 0?-5
510Print "Negative"-5Negative
613Stop-5

Time and space complexity

Time: O(1) · Space: O(1). Only one input is read and at most two comparisons are made, with no loops.

Common mistakes

  • Writing n = 0 instead of n == 0 when comparing, which assigns instead of checking (this program avoids it by using else for zero).
  • Using separate if statements instead of else if, which can print more than one message in other versions of the code.
  • Forgetting the & in scanf("%d", &n), so the number is not stored in n.

Viva questions

Q1. What does this program do?

It reads one integer from the user and prints whether it is Positive, Negative or Zero using an if / else-if / else chain.

Q2. Why do we write &n in scanf and not just n?

scanf needs the address of the variable so it can store the typed value into n's memory location. Passing n would pass its value, not where to store it.

Q3. Why do we need the final else, and why don't we check n == 0 explicitly?

If n is not greater than 0 and not less than 0, the only possibility left is that it is 0, so a plain else covers it. An explicit n == 0 check would be redundant.

Q4. What is printed for input 0, and which line handles it?

It prints Zero. Both n > 0 and n < 0 are false, so control falls to the else on line 11-12.

Q5. What happens if the user enters a negative number like -5?

The first condition n > 0 is false, the second n < 0 is true, so it prints Negative and skips the else.

Q6. If n is 10, is the condition n < 0 ever checked? Why?

No. In an if / else-if chain, once the first true condition runs its block, the remaining branches are skipped.

Q7. What happens if the user types a letter like 'a' instead of a number?

scanf fails to read an integer and leaves n uninitialised, so the output is unpredictable. We could check scanf's return value (it should be 1) to handle this.

Q8. What is the time complexity of this program?

O(1), constant time, because it does a fixed number of comparisons and one print regardless of the input value.

Q9. What would change if you wanted to also report whether the number is even or odd?

Add a check such as n % 2 == 0 inside or after the chain, for example a separate if/else that prints Even or Odd. For zero it would print Even, and n % 2 also works for negative numbers since the result is only compared to 0.

Frequently asked questions

Why is there no condition for zero in the C code?

If n is not greater than 0 and not less than 0, it must be 0, so the final else handles it without another check.

Is 0 positive or negative?

0 is neither positive nor negative, which is why the program prints Zero for it.

Can I use only if statements without else?

Yes, you can write three separate ifs (n > 0, n < 0, n == 0), but if / else if / else is cleaner and avoids extra comparisons.

More programs

← All programs

Try AI Flowchart free →