Learning by doing: Reading books and trying to understand the (code) examples
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 

47 lines
603 B

#include <stdio.h>
#include <limits.h>
int getNumber(void);
int main(void) {
int count = 0;
int sum = 0;
int end = getNumber();
if (end >= 0) {
while (count++ < end) {
if (INT_MAX / count < count || (INT_MAX - sum) < count * count) {
printf("Integer overflow\n");
return 1;
} else {
sum = sum + count * count;
}
}
printf("sum = %d\n", sum);
}
return 0;
}
int getNumber(void) {
int number;
printf("How far should we add: ");
if (!scanf("%d", &number) || number < 0) {
number = -1;
}
return number;
}