All submissions

Divide by Zero Handling

#include <iostream>
#include <stdexcept>
using namespace std;

// your code here: define divide(int a, int b)
int divide(int a, int b) {
    if (b == 0) {
        throw std::runtime_error("Division by zero");
    }
    return a/b;
}

int main() {
   int a, b;
   cin >> a >> b;
   try {
       int result = divide(a, b);
       cout << "Result: " << result << "\n";
   } catch (const runtime_error& e) {
       cout << "Error: " << e.what() << "\n";
   }
   return 0;
}
Loading...

Input

10 2

Expected Output

Result: 5