Computer scienceProgramming languagesC++Errors, debugging, and exceptions

Exception Handling

Theory

Sum of natural numbers

Report a typo

Write a C++ program that calculates the sum of natural numbers from 1 to N (inclusive), where N is a positive integer. Implement custom exception handling to ensure that negative input is not accepted. If a negative number is entered, display "Negative or zero input is not allowed. Please enter a positive integer." an error message and request a positive input.

Sample Input 1:

3

Sample Output 1:

6

Sample Input 2:

-3

Sample Output 2:

Error: Negative or zero input is not allowed. Please enter a positive integer.

Sample Input 3:

4

Sample Output 3:

10
Write a program in C++





Create a free account to access the full topic