Dzień dobry,
Program sumuje cyfry podanej liczby. Chcę wyświetlić błąd, gdy podana liczba jest ujemna lub zbyt duża, aby przechować ją w int. Jak mogę to zrobić?
Jak mogę rozwiązać ćwiczenie bez użycia string i array?
Pozdrawiam serdecznie
#include <climits>
#include <cmath>
#include <iostream>
using namespace std;
void sumOfNumber();
int main()
{
char answer;
do
{
sumOfNumber();
cout << "\nOneMoreTime (Y/N) : ";
cin >> answer;
} while (answer == 'Y' || answer == 'y');
return 0;
}
void sumOfNumber()
{
int val, num, sum = 0;
cout << "Enter the number : ";
cin >> val;
num = val;
while (num != 0 && num <= INT_MAX)
{
sum = sum + num % 10;
num = num / 10;
}
cout << "The sum of the digits of the given number is " << sum;
}