@ -0,0 +1,47 @@ | |||
#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 - sum) < count) { | |||
printf("Integer overflow\n"); | |||
return 1; | |||
} else { | |||
sum = sum + 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; | |||
} |
@ -0,0 +1,47 @@ | |||
#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; | |||
} |
@ -0,0 +1,50 @@ | |||
#include <stdio.h> | |||
#include <math.h> | |||
#include <fenv.h> | |||
double cubeNumber(double number); | |||
int main (void) { | |||
double number; | |||
double cube_number; | |||
int error = 0; | |||
printf("Give a number: "); | |||
if (scanf("%lf", &number) != 1 || isnan(number)) { | |||
printf("Not a number!\n"); | |||
error = 1; | |||
} else { | |||
cube_number = cubeNumber(number); | |||
if (cube_number != NAN) { | |||
printf("%lf\n", cube_number); | |||
} else { | |||
printf("Floating point exception occured!\n"); | |||
error = 1; | |||
} | |||
} | |||
return error; | |||
} | |||
double cubeNumber (double number) { | |||
double i; | |||
feclearexcept(FE_ALL_EXCEPT); | |||
i = pow(number, 3); | |||
if (fetestexcept(FE_ALL_EXCEPT) == 0) { | |||
i = NAN; | |||
} | |||
return i; | |||
} |
@ -0,0 +1,46 @@ | |||
#include <stdio.h> | |||
#include <limits.h> | |||
#include <stdbool.h> | |||
int getNumber(char * s, bool *error); | |||
int main(void) { | |||
int first_operand; | |||
int second_operand; | |||
bool first_error; | |||
bool second_error; | |||
printf("This program computes moduli.\n"); | |||
second_operand = getNumber("Enter an integer to serve as the second operand: ", &second_error); | |||
first_operand = getNumber("Now enter the first operand: ", &first_error); | |||
do { | |||
printf("%d %% %d is %d\n", first_operand, second_operand, first_operand % second_operand); | |||
first_operand = getNumber("Now enter the first operand (<= 0 to quit): ", &first_error); | |||
} while (!first_error && !second_error); | |||
return 0; | |||
} | |||
int getNumber(char * s, bool *error) { | |||
int number; | |||
*error = false; | |||
printf("%s", s); | |||
if (!scanf("%d", &number) || number < 0) { | |||
*error = true; | |||
} | |||
return number; | |||
} |
@ -0,0 +1,55 @@ | |||
#include <stdio.h> | |||
#include <limits.h> | |||
#include <math.h> | |||
#include <stdbool.h> | |||
double getNumber(char * s); | |||
void calcTemperatures(double fahrenheit); | |||
int main(void) { | |||
double fahrenheit; | |||
printf("This program calculates Celsius & Kelvin from Fahrenheit.\n"); | |||
fahrenheit = getNumber("Now enter the temp in Fahrenheit: "); | |||
while (!isnan(fahrenheit)) { | |||
calcTemperatures(fahrenheit); | |||
fahrenheit = getNumber("Now enter the first operand (q to quit): "); | |||
}; | |||
return 0; | |||
} | |||
double getNumber(char * s) { | |||
double number; | |||
printf("%s", s); | |||
if (!scanf("%lf", &number) || isnan(number)) { | |||
number = NAN; | |||
} | |||
return number; | |||
} | |||
void calcTemperatures(double fahrenheit) { | |||
const float celsius_div = 5.0 / 9.0; | |||
const float celsius_offset = 32.0; | |||
const float kelvin_offset = 273.16; | |||
const double celsius = celsius_div *(fahrenheit - celsius_offset); | |||
const double kelvin = celsius + kelvin_offset; | |||
printf("%.2lf Fahrenheit are %.2lf Celsius or %.2lf Kelvin.\n", fahrenheit, celsius, kelvin); | |||
} |