Certainly! In C++, overflow and underflow occur when the result of an arithmetic operation exceeds the range that can be represented by a data type. Here's an example program demonstrating overflow and underflow using integers:



#include <iostream> using namespace std; int main() { // Overflow example int maxValue = INT_MAX; // Maximum value for an int cout << "Max int value: " << maxValue << endl; int overflowedValue = maxValue + 1; // This will cause overflow cout << "Overflowed value: " << overflowedValue << endl; // Underflow example int minValue = INT_MIN; // Minimum value for an int cout << "Min int value: " << minValue << endl; int underflowedValue = minValue - 1; // This will cause underflow cout << "Underflowed value: " << underflowedValue << endl; return 0; }



In this program, we're using INT_MAX and INT_MIN constants from the <climits> header, which represent the maximum and minimum values an int data type can hold.


Overflow occurs when we try to store a value larger than the maximum possible value for the data type (maxValue + 1).

Underflow occurs when we try to store a value smaller than the minimum possible value for the data type (minValue - 1).

When you run this program, you'll observe that the overflowed value will wrap around and display a negative number, and the underflowed value will wrap around and display a large positive number, due to the way integer overflow and underflow work in two's complement representation used by most systems.