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:
- We include the header file <stdio.h> to use printf and scanf functions.
- In the main function, we declare an integer variable N to store the user input.
- We prompt the user to enter an integer using printf.
- We read the user input using scanf and store it in the variable N.
- We use an if-else ladder to check the value of N.
- If N is greater than 0, it is positive, so we print that it is positive.
- If N is less than 0, it is negative, so we print that it is negative.
- If N is neither greater nor less than 0, it must be 0, so we print that it is zero.
- 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:
- The program starts by importing the Scanner class from the java.util package. The Scanner class allows us to read input from the user.
- The program defines a public class named CheckPositiveNegative.
- Inside the class, the program defines the main method, which serves as the entry point of the program.
- 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).
- The program prompts the user to enter an integer by printing the message “Enter an integer: ” to the console using System.out.print().
- 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.
- 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.
- 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.
- 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.
- 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.
- 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().
- Finally, the scanner object is closed using the close() method to release system resources.
Output
Enter an integer: 4
4 is positive.