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

Program to determines product is positive or negative

Write a program that takes three numbers as input and determines whether their product is positive or negative.

Program in C

C
#include <stdio.h>

int main() {
    int num1, num2, num3;
    int product;

    // Getting input from the user
    printf("Enter three numbers: ");
    scanf("%d %d %d", &num1, &num2, &num3);

    // Calculating the product
    product = num1 * num2 * num3;

    // Checking the sign of the product
    if (product > 0) {
        printf("The product is positive.\n");
    } else if (product < 0) {
        printf("The product is negative.\n");
    } else {
        printf("The product is zero.\n");
    }

    return 0;
}

Explanation:

  1. The program starts by including the necessary header file stdio.h, which provides input/output functions like printf and scanf.
  2. In the main function, we declare the variables num1, num2, and num3 to store the three input numbers, and the product variable to store the result of the multiplication.
  3. The printf function is used to prompt the user to enter three numbers.
  4. The scanf function is used to read the three numbers entered by the user and store them in the corresponding variables.
  5. Next, we calculate the product of the three numbers by multiplying them together and assigning the result to the product variable.
  6. We then use an if-else statement to check the sign of the product. If the product is greater than 0, we print that it is positive. If the product is less than 0, we print that it is negative. If the product is 0, we print that it is zero.
  7. Finally, we return 0 to indicate successful execution of the program.

Output:

Output
Enter three numbers: 4
2
6
The product is positive.

Program in Java

Java
import java.util.Scanner;

public class ProductSignChecker {
    public static void main(String[] args) {
        int num1, num2, num3;
        int product;

        Scanner scanner = new Scanner(System.in);

        System.out.print("Enter three numbers: ");
        num1 = scanner.nextInt();
        num2 = scanner.nextInt();
        num3 = scanner.nextInt();

        product = num1 * num2 * num3;

        if (product > 0) {
            System.out.println("The product is positive.");
        } else if (product < 0) {
            System.out.println("The product is negative.");
        } else {
            System.out.println("The product is zero.");
        }

        scanner.close();
    }
}

Explanation:

  1. The program starts by importing the Scanner class from the java.util package, which allows us to read input from the user.
  2. The ProductSignChecker class is defined, which contains the main method where the program execution begins.
  3. Inside the main method, integer variables num1, num2, and num3 are declared to store the three numbers entered by the user.
  4. A Scanner object named scanner is created to read input from the user.
  5. The program prompts the user to enter three numbers using the System.out.print() method.
  6. The nextInt() method of the Scanner class is used to read integer values entered by the user and assign them to the respective variables num1, num2, and num3.
  7. The program then calculates the product of the three numbers by multiplying them together and assigns the result to the product variable.
  8. The program checks the sign of the product using the if statement:
    • If the product is greater than 0, the program prints “The product is positive.” using the System.out.println() method.
    • If the product is less than 0, the program prints “The product is negative.”
    • If the product is 0, the program prints “The product is zero.”
  9. Finally, the scanner.close() method is called to release any resources associated with the Scanner object and close the input stream.
Java Output
Enter three numbers: 4
-2
6
The product is negative.