w3resource

C#: Check two given integers and return true if one is negative and one is positive


Check Positive and Negative Pair

Write a C# program to check a pair of integers and return true if one is negative and one is positive.

C# Sharp Exercises: Check two given integers and return true if one is negative and one is positive

Sample Solution:

C# Sharp Code:

using System;
using System.Collections.Generic;

// This is the beginning of the Exercise18 class
public class Exercise18 {
    // This is the main method where the program execution starts
    static void Main(string[] args)
    {
        // Prompting the user to input the first integer
        Console.WriteLine("\nInput first integer:");
        // Reading the first integer input provided by the user and converting it to an integer
        int x = Convert.ToInt32(Console.ReadLine());

        // Prompting the user to input the second integer
        Console.WriteLine("Input second integer:");
        // Reading the second integer input provided by the user and converting it to an integer
        int y = Convert.ToInt32(Console.ReadLine());

        // Displaying a message to check if one integer is negative and the other is positive
        Console.WriteLine("Check if one is negative and one is positive:");

        // Checking if one integer is negative and the other is positive, or vice versa, and displaying the result
        Console.WriteLine((x < 0 && y > 0) || (x > 0 && y < 0));
    }
}

Sample Output:

Input first integer:                                                   
-5                                                                     
Input second integer:                                                  
25                                                                     
Check if one is negative and one is positive:                          
True 

Flowchart:

Flowchart: C# Sharp Exercises - Check two given integers and return true if one is negative and one is positive

For more Practice: Solve these Related Problems:

  • Write a C# program to check whether the product of two integers is negative, positive, or zero.
  • Write a C# program that returns true if exactly one of two numbers is a negative even number.
  • Write a C# program to return true if the signs of two numbers are opposite using bitwise XOR.
  • Write a C# program to test if one number is negative and the other is divisible by it.

Go to:


PREV : Add First Character to Front and Back.
NEXT : Sum or Triple Sum of Integers.

C# Sharp Code Editor:



What is the difficulty level of this exercise?

Test your Programming skills with w3resource's quiz.



Follow us on Facebook and Twitter for latest update.