Thursday, January 29, 2026

C++ Read two numbers: Find Difference and Sign Check

Write a C++ program that reads two integers from the user, calculates their difference, and checks whether the result is positive, negative, or zero. 

#include <iostream>

using namespace std;

int main() {
    int num1, num2, difference;

    // Input two integers
    cout << "Enter first integer: ";
    cin >> num1;
    cout << "Enter second integer: ";
    cin >> num2;

    // Calculate difference
    difference = num1 - num2;

    // Display result
    cout << "Difference = " << difference << endl;

    // Check whether the result is positive, negative, or zero
    if (difference > 0) {
        cout << "The difference is positive." << endl;
    } else if (difference < 0) {
        cout << "The difference is negative." << endl;
    } else {
        cout << "The difference is zero." << endl;
    }

    return 0;
}

/* Sample Output

Enter first integer: 50
Enter second integer: 20
Difference = 30
The difference is positive.

*/

No comments:

Post a Comment