|
| 1 | +// Copyright 2022 finchren |
| 2 | +/* |
| 3 | +Write a program in C to find the sum of the series [ 1-X^2/2!+X^4/4!- .........] |
| 4 | +Test Data : |
| 5 | +Input the Value of x :2 |
| 6 | +Input the number of terms : 5 |
| 7 | +Expected Output : |
| 8 | +the sum = -0.415873 |
| 9 | +Number of terms = 5 |
| 10 | +value of x = 2.000000 |
| 11 | ++ 1. Take the input of x and number of terms |
| 12 | ++ 2. Write factorial function |
| 13 | ++ 2.1 Write pow function |
| 14 | +2.3 Write function for counting the sum |
| 15 | ++ 2.5 Display the sum, number of term and value of x |
| 16 | ++ 3. Work around invalid input |
| 17 | ++ 4. Test |
| 18 | ++ 5. Cpplint test |
| 19 | ++ 6. Add and push |
| 20 | ++ 7. Rework considering the Misha's comments |
| 21 | ++ 8. Fix the error in the code |
| 22 | ++ 9. Add and push fixed and reworked version |
| 23 | +*/ |
| 24 | + |
| 25 | +#include <stdio.h> |
| 26 | + |
| 27 | +int input_value_of_x(); |
| 28 | +int input_number_of_terms(); |
| 29 | +int factorial(int number); |
| 30 | +int power(int base, int exponent); |
| 31 | +double count_sum(int x, int number_of_terms); |
| 32 | +void print_sum(double sum); |
| 33 | +void print_number_of_terms(int number_of_terms); |
| 34 | +void print_value_of_x(int x); |
| 35 | +void print_invalid_input(); |
| 36 | + |
| 37 | +int main() { |
| 38 | + int x = input_value_of_x(); |
| 39 | + if (x > 0) { |
| 40 | + int number_of_terms = input_number_of_terms(); |
| 41 | + if (number_of_terms > 0) { |
| 42 | + print_sum(count_sum(x, number_of_terms)); |
| 43 | + print_number_of_terms(number_of_terms); |
| 44 | + print_value_of_x(x); |
| 45 | + } else { |
| 46 | + print_invalid_input(); |
| 47 | + } |
| 48 | + } else { |
| 49 | + print_invalid_input(); |
| 50 | + } |
| 51 | + return 0; |
| 52 | +} |
| 53 | + |
| 54 | +void print_invalid_input() { |
| 55 | + printf("n/a"); |
| 56 | +} |
| 57 | + |
| 58 | +void print_value_of_x(int x) { |
| 59 | + printf("Value of x = %f\n", (double)x); |
| 60 | +} |
| 61 | + |
| 62 | +void print_number_of_terms(int number_of_terms) { |
| 63 | + printf("Number of terms = %d\n", number_of_terms); |
| 64 | +} |
| 65 | + |
| 66 | +void print_sum(double sum) { |
| 67 | + printf("The sum = %f\n", sum); |
| 68 | +} |
| 69 | + |
| 70 | +double count_sum(int x, int number_of_terms) { |
| 71 | + double sum = 1.0; |
| 72 | + int multiplier = 2, sign = -1; |
| 73 | + for (int i = 1; i < number_of_terms; ++i) { |
| 74 | + sum += sign * power(x, multiplier) / (double)factorial(multiplier); |
| 75 | + multiplier += 2; |
| 76 | + sign *= (-1); |
| 77 | + } |
| 78 | + return sum; |
| 79 | +} |
| 80 | + |
| 81 | +int power(int base, int exponent) { |
| 82 | + int result = base; |
| 83 | + for (int i = 2; i <= exponent; ++i) { |
| 84 | + result *= base; |
| 85 | + } |
| 86 | + return result; |
| 87 | +} |
| 88 | + |
| 89 | +int input_number_of_terms() { |
| 90 | + int number_of_terms; |
| 91 | + char endline; |
| 92 | + printf("Input the number of terms:\n"); |
| 93 | + if (!scanf("%d%c", &number_of_terms, &endline) || (endline != '\n') || (number_of_terms <= 0)) { |
| 94 | + number_of_terms = 0; |
| 95 | + } |
| 96 | + return number_of_terms; |
| 97 | +} |
| 98 | + |
| 99 | +int input_value_of_x() { |
| 100 | + int x; |
| 101 | + char endline; |
| 102 | + printf("Input the value of x:\n"); |
| 103 | + if (!scanf("%d%c", &x, &endline) || endline != '\n') { |
| 104 | + x = 0; |
| 105 | + } |
| 106 | + return x; |
| 107 | +} |
| 108 | + |
| 109 | +int factorial(int number) { |
| 110 | + int result = 1; |
| 111 | + for (int i = 1; i <= number; ++i) { |
| 112 | + result *= i; |
| 113 | + } |
| 114 | + return result; |
| 115 | +} |
0 commit comments