Skip to content

Commit b31d0e7

Browse files
committed
Finished the exercise but no invalid input and no tests - goind to do them later
1 parent 954f85b commit b31d0e7

File tree

1 file changed

+38
-15
lines changed

1 file changed

+38
-15
lines changed

For loop/49. Find the sum of an A.P. series.c

Lines changed: 38 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -9,36 +9,59 @@ Expected Output :
99
The Sum of the A.P. series are :
1010
1 + 5 + 9 + 13 + 17 + 21 + 25 + 29 + 33 + 37 = 190
1111
12-
1. Take input
13-
1.5 Invalid input
14-
2. Write a function to find elements
15-
3. Output sum
12+
+ 1. Take input
13+
1.5 Invalid input - To be done later
14+
+ 2. Write a function to find elements
15+
+ 3. Output sum
1616
4. Test
17-
5. Cpplint test
18-
6. Add and push
17+
+ 5. Cpplint test
18+
+ 6. Add and push
1919
*/
2020

2121
#include <stdio.h>
2222

2323
int input_start();
24-
int input_end();
25-
void print_invalid_input();
24+
int input_number_of_items();
25+
int input_difference();
26+
void find_and_print_result(int start, int number_of_items, int difference);
2627

2728
int main() {
2829
int start = input_start();
29-
int end = input_end();
30-
printf()
30+
int number_of_items = input_number_of_items();
31+
int difference = input_difference();
32+
find_and_print_result(start, number_of_items, difference);
3133
return 0;
3234
}
3335

34-
void print_invalid_input() {
35-
printf("n/a");
36+
void find_and_print_result(int start, int number_of_items, int difference) {
37+
printf("The Sum of the A.P. series are:\n");
38+
int sum = start, element = start;
39+
printf("%d ", element);
40+
for (int i = 0; i < number_of_items - 1; ++i) {
41+
element += difference;
42+
printf("+ %d ", element);
43+
sum += element;
44+
}
45+
printf("= %d", sum);
3646
}
3747

3848
int input_start() {
39-
49+
printf("Input the starting number of the A.P. series:\n");
50+
int start;
51+
scanf("%d", &start);
52+
return start;
4053
}
4154

42-
int input_end() {
55+
int input_number_of_items() {
56+
printf("Input the number of items for the A.P. series:\n");
57+
int number_of_items;
58+
scanf("%d", &number_of_items);
59+
return number_of_items;
60+
}
4361

44-
}
62+
int input_difference() {
63+
printf("Input the common difference of A.P. series:\n");
64+
int difference;
65+
scanf("%d", &difference);
66+
return difference;
67+
}

0 commit comments

Comments
 (0)