Skip to content

Commit 7ece44e

Browse files
committed
Upload all the previously solved exercises
0 parents  commit 7ece44e

28 files changed

+2620
-0
lines changed

For loop/06. Multiplication table.c

+34
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
// Copyright 2022 finchren
2+
/*
3+
Write a program in C to display the multiplication table of a given integer. Go to the editor
4+
Test Data :
5+
Input the number (Table to be calculated) : 15
6+
Expected Output :
7+
15 X 1 = 15
8+
...
9+
...
10+
15 X 10 = 150
11+
12+
+ 1. Take the input
13+
+ 2. Display the multiplication table
14+
+ 3. Work around invalid input
15+
+ 4. Test
16+
+ 5. Cpplint test
17+
+ 6. Add and push
18+
*/
19+
20+
#include <stdio.h>
21+
22+
int main() {
23+
int number, result;
24+
char endline;
25+
if (scanf("%d%c", &number, &endline) && ((endline == ' ') || (endline == '\n'))) {
26+
for (int i = 1; i <= 10; i++) {
27+
result = number * i;
28+
printf("%d x %d = %d\n", number, i, result);
29+
}
30+
} else {
31+
printf("n/a");
32+
}
33+
return 0;
34+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
// Copyright 2022 finchren
2+
/*
3+
Write a program in C to make such a pattern like a pyramid with numbers increased by 1. As input it takes the number of elements
4+
1
5+
2 3
6+
4 5 6
7+
7 8 9 10
8+
9+
+ 1. Take the input
10+
+ 2. Display the numbers of the pyramid
11+
+ 2.5 Display the spaces of the pyramid
12+
+ 3. Work around invalid input
13+
+ 4. Test
14+
+ 5. Cpplint test
15+
+ 6. Add and push
16+
*/
17+
18+
#include <stdio.h>
19+
20+
int number_of_rows(int number);
21+
void print_spaces(int number_of_rows);
22+
23+
int main() {
24+
int input_number, output_number = 1, rows;
25+
char endline;
26+
if (scanf("%d%c", &input_number, &endline) && ((endline == ' ') || (endline == '\n'))) {
27+
rows = number_of_rows(input_number);
28+
for (int i = 1; i <= input_number; i++) {
29+
print_spaces(rows);
30+
for (int j = 0; j < i; j++) {
31+
printf("%d ", output_number);
32+
input_number -= 1;
33+
output_number++;
34+
}
35+
printf("\n");
36+
rows -= 1;
37+
}
38+
} else {
39+
printf("n/a");
40+
}
41+
return 0;
42+
}
43+
44+
void print_spaces(int number_of_rows) {
45+
for (int i = number_of_rows - 1; i > 0; i--) {
46+
printf(" ");
47+
}
48+
}
49+
50+
int number_of_rows(int number) {
51+
int result = 0;
52+
for (int i = 1; i <= number; i++) {
53+
for (int j = 0; j < i; j++) {
54+
number -= j;
55+
}
56+
result++;
57+
}
58+
return result;
59+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
// Copyright 2022 finchren
2+
/*
3+
Write a program in C to make such a pattern like a pyramid with numbers increased by 1. As input it takes the number of elements
4+
1
5+
2 3
6+
4 5 6
7+
7 8 9 10
8+
9+
+ 1. Take the input
10+
+ 2. Display the numbers of the pyramid
11+
+ 2.5 Display the spaces of the pyramid
12+
+ 3. Work around invalid input
13+
+ 4. Test
14+
+ 5. Cpplint test
15+
+ 6. Add and push
16+
*/
17+
18+
#include <stdio.h>
19+
20+
int number_of_rows(int number);
21+
void print_spaces(int number_of_rows);
22+
23+
int main() {
24+
int input_number, output_number = 1, rows;
25+
char endline;
26+
// Check if input is a number and there isn't any characters at the end except for space or a newline
27+
if (scanf("%d%c", &input_number, &endline) && ((endline == ' ') || (endline == '\n'))) {
28+
rows = number_of_rows(input_number) - 1;
29+
// Check to see if we can build a symmetric pyramid out of the input number
30+
if (rows > 0) {
31+
for (int i = 1; i <= input_number; i++) {
32+
print_spaces(rows);
33+
for (int j = 0; j < i; j++) {
34+
printf("%d ", output_number);
35+
input_number -= 1;
36+
output_number++;
37+
}
38+
printf("\n");
39+
rows -= 1;
40+
}
41+
} else {
42+
printf("The symmetric pyramid cannot be build out of that number \n"
43+
"Allowed numbers are 10, 15, 21, 28, 36, 45, 55, 66, 78 etc \n"
44+
"Int the pyramid each row is begger than previos by one digit \n");
45+
}
46+
} else {
47+
printf("n/a");
48+
}
49+
return 0;
50+
}
51+
52+
void print_spaces(int number_of_rows) {
53+
for (int i = number_of_rows; i > 0; i--) {
54+
printf(" ");
55+
}
56+
}
57+
58+
// The function returns zero in case of invalid input
59+
// Invalid input - symmetric piramid cannot be build
60+
int number_of_rows(int number) {
61+
int result = 0, counter = 0, temp = 0;
62+
for (int i = 1; i <= number; i ++) {
63+
temp += i;
64+
counter++;
65+
if (temp == number) {
66+
result = counter;
67+
}
68+
if (temp > number) {
69+
break;
70+
}
71+
}
72+
return result;
73+
}
+115
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,115 @@
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+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
1+
// Copyright 2022 finchren
2+
/*
3+
Write a program in C to display the n terms of harmonic series and their sum.
4+
1 + 1/2 + 1/3 + 1/4 + 1/5 ... 1/n terms
5+
Test Data :
6+
Input the number of terms : 5
7+
Expected Output :
8+
1/1 + 1/2 + 1/3 + 1/4 + 1/5 +
9+
Sum of Series upto 5 terms : 2.283334
10+
11+
+ 1. Take the input of n
12+
+ 2. Write a function to display expected output
13+
+ 3. Write a function to count sum
14+
+ 4. Display the sum
15+
+ 5. Invalid input
16+
+ 6. Test
17+
+ 7. Cpplint test
18+
8. Add and push
19+
*/
20+
21+
#include <stdio.h>
22+
23+
int input_n();
24+
void display_expected(int n);
25+
double count_sum(int n);
26+
void display_sum(double sum, int n);
27+
void invalid_input();
28+
29+
int main() {
30+
int n = input_n();
31+
if (n > 0) {
32+
display_expected(n);
33+
double sum = count_sum(n);
34+
display_sum(sum, n);
35+
} else {
36+
invalid_input();
37+
}
38+
return 0;
39+
}
40+
41+
void invalid_input() {
42+
printf("n/a");
43+
}
44+
45+
void display_sum(double sum, int n) {
46+
printf("Sum of Series upto %d terms: %lf", n, sum);
47+
}
48+
49+
double count_sum(int n) {
50+
double sum = 0;
51+
for (int i = 1; i <= n; i++) {
52+
sum += 1.0 / i;
53+
}
54+
return sum;
55+
}
56+
57+
void display_expected(int n) {
58+
for (int i = 1; i <= n; i++) {
59+
printf("1/%d", i);
60+
if (i != n) {
61+
printf(" + ");
62+
}
63+
}
64+
printf("\n");
65+
}
66+
67+
int input_n() {
68+
int n;
69+
char endline;
70+
printf("Input the number of terms:\n");
71+
if ((!scanf("%d%c", &n, &endline)) || (endline != '\n')) {
72+
n = 0;
73+
}
74+
return n;
75+
}

0 commit comments

Comments
 (0)