Generic selectors
Exact matches only
Search in title
Search in content
Post Type Selectors

Program determines integer is positive, negative, or zero

Write a program that takes an integer N as input and determines whether it is positive, negative, or zero.

Program in C

C
#include <stdio.h>

int main() {
    int N;

    printf("Enter an integer: ");
    scanf("%d", &N);

    if (N > 0) {
        printf("%d is positive.\n", N);
    } else if (N < 0) {
        printf("%d is negative.\n", N);
    } else {
        printf("The number is zero.\n");
    }

    return 0;
}

Explanation:

  1. We include the header file <stdio.h> to use printf and scanf functions.
  2. In the main function, we declare an integer variable N to store the user input.
  3. We prompt the user to enter an integer using printf.
  4. We read the user input using scanf and store it in the variable N.
  5. We use an if-else ladder to check the value of N.
  6. If N is greater than 0, it is positive, so we print that it is positive.
  7. If N is less than 0, it is negative, so we print that it is negative.
  8. If N is neither greater nor less than 0, it must be 0, so we print that it is zero.
  9. Finally, we return 0 to indicate successful execution of the program.

Output:

C
Enter an integer: 4
4 is positive.

Program in Java

Java
import java.util.Scanner;

public class CheckPositiveNegative {
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        System.out.print("Enter an integer: ");
        int N = scanner.nextInt();

        if (N > 0) {
            System.out.printf("%d is positive.\n", N);
        } else if (N < 0) {
            System.out.printf("%d is negative.\n", N);
        } else {
            System.out.println("The number is zero.");
        }

        scanner.close();
    }
}

Explanation:

  1. The program starts by importing the Scanner class from the java.util package. The Scanner class allows us to read input from the user.
  2. The program defines a public class named CheckPositiveNegative.
  3. Inside the class, the program defines the main method, which serves as the entry point of the program.
  4. Within the main method, an instance of the Scanner class is created by initializing it with System.in, which represents the standard input stream (keyboard input).
  5. The program prompts the user to enter an integer by printing the message “Enter an integer: ” to the console using System.out.print().
  6. The entered integer is read from the user using the nextInt() method of the Scanner class. It reads the next integer from the input stream and stores it in the variable N.
  7. The program then checks if the entered integer N is greater than 0 using the greater-than operator >. If N is greater than 0, it means the number is positive.
  8. If N is greater than 0, the program prints the message “%d is positive.\n”, where %d is a placeholder for the integer value N. The printf() method is used to format the output.
  9. If the first condition is not true (i.e., N is not greater than 0), the program moves to the else if condition. It checks if N is less than 0 using the less-than operator <. If N is less than 0, it means the number is negative.
  10. If N is less than 0, the program prints the message “%d is negative.\n”, where %d is a placeholder for the integer value N.
  11. If both the conditions are false, it means N must be equal to 0. In this case, the program executes the else block and prints “The number is zero.” using System.out.println().
  12. Finally, the scanner object is closed using the close() method to release system resources.
Output
Enter an integer: 4
4 is positive.