31 lines
676 B
C
31 lines
676 B
C
/* HW6A - Nicholas Pease
|
|
Calculation of Sum of All Even Numbers
|
|
*/
|
|
|
|
#include <stdio.h>
|
|
#include <stdlib.h>
|
|
#include "PEASE.h"
|
|
// IMPORTANT - SOME CODE IN ABOVE HEADER FILE
|
|
|
|
// Recursively add all even numbers
|
|
int recurseSum(int n) {
|
|
if (n <= 1)
|
|
return 0;
|
|
if (n % 2 == 0) {
|
|
return n + recurseSum(n-2);
|
|
} else {
|
|
return recurseSum(n-1);
|
|
}
|
|
}
|
|
|
|
int main() {
|
|
printf("Input a number larger than 1: ");
|
|
char input[20];
|
|
scanf("%s",input);
|
|
if (isValidNum(input)) {
|
|
printf("Sum of even numbers between 1 and %s: %d\n",input,recurseSum(atoi(input)));
|
|
} else {
|
|
printf("Invalid Input\n");
|
|
}
|
|
return 0;
|
|
} |