-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDSA16.c
More file actions
343 lines (315 loc) · 10.5 KB
/
Copy pathDSA16.c
File metadata and controls
343 lines (315 loc) · 10.5 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
/*
This code base own and maintained by Tanmoy Samnata using standardization C23 (ISO/IEC 9899:2024)
*/
#include <stdio.h>
#include <stdlib.h>
#include <stdbool.h>
// Find the sum of all even or odd numbers in a given range using recursion.
int calculate_range_sum_recursive(int current_number, int upper_bound, bool choose_even)
{
if (current_number > upper_bound)
{
return 0;
}
bool is_current_even = (current_number % 2 == 0);
int term_value = 0;
if ((choose_even && is_current_even) || (!choose_even && !is_current_even))
{term_value = current_number;}
return term_value + calculate_range_sum_recursive(current_number + 1, upper_bound, choose_even);
}
int SumOFNumbersGivenRecursion(void)
{
int lower_bound, upper_bound, user_choice;
printf("Enter lower and upper bounds: ");
if (scanf("%d %d", &lower_bound, &upper_bound) != 2 || lower_bound > upper_bound)
{
printf("Error: Invalid input or interval bounds.\n");
return 1;
}
printf("Select option (1 for Sum of Even numbers, 2 for Sum of Odd numbers): ");
if (scanf("%d", &user_choice) != 1 || (user_choice != 1 && user_choice != 2))
{
printf("Error: Invalid selection choice.\n");
return 1;
}
bool choose_even = (user_choice == 1);
int total_accumulated_sum = calculate_range_sum_recursive(lower_bound, upper_bound, choose_even);
printf("The sum of the requested numbers between %d and %d is: %d\n",
lower_bound, upper_bound, total_accumulated_sum);
return 0;
}
// Find reverse of any number using recursion.
int reverse_number_recursive(int remaining_digits, int accumulated_reversed_value)
{
if (remaining_digits == 0)
{
return accumulated_reversed_value;
}
int last_digit = remaining_digits % 10;
int updated_reversed_value = (accumulated_reversed_value * 10) + last_digit;
return reverse_number_recursive(remaining_digits / 10, updated_reversed_value);
}
int ReverseNumbersRecursion(void)
{
int user_input_number;
printf("Enter an integer to reverse: ");
if (scanf("%d", &user_input_number) != 1)
{
printf("Error: Invalid numeric input.\n");
return 1;
}
int sign_multiplier = (user_input_number < 0) ? -1 : 1;
int absolute_input_number = user_input_number * sign_multiplier;
int reversed_absolute_result = reverse_number_recursive(absolute_input_number, 0);
int final_reversed_number = reversed_absolute_result * sign_multiplier;
printf("The reversed number is: %d\n", final_reversed_number);
return 0;
}
// Check whether a number is a palindrome or not using recursion.
int reverse_digits_recursive(int remaining_digits, int accumulated_value)
{
if (remaining_digits == 0)
{
return accumulated_value;
}
int last_digit = remaining_digits % 10;
int updated_accumulated_value = (accumulated_value * 10) + last_digit;
return reverse_digits_recursive(remaining_digits / 10, updated_accumulated_value);
}
bool is_palindrome(int evaluation_number)
{
if (evaluation_number < 0)
{
return false;
}
int reversed_number = reverse_digits_recursive(evaluation_number, 0);
return (evaluation_number == reversed_number);
}
int CheckNumberPalindromeRecursion(void)
{
int user_input_number;
printf("Enter an integer: ");
if (scanf("%d", &user_input_number) != 1)
{
printf("Error: Invalid numeric input.\n");
return 1;
}
if (is_palindrome(user_input_number))
{
printf("%d is a palindrome number.\n", user_input_number);
}
else
{
printf("%d is not a palindrome number.\n", user_input_number);
}
return 0;
}
// Find the sum of digits of a given number using recursion.
int SumOfDigitsOfNumberRecursion(void)
{
int user_input_number;
printf("Enter an integer: ");
if (scanf("%d", &user_input_number) != 1)
{
printf("Error: Invalid numeric input.\n");
return 1;
}
if (is_palindrome(user_input_number))
{
printf("%d is a palindrome number.\n", user_input_number);
}
else
{
printf("%d is not a palindrome number.\n", user_input_number);
}
return 0;
}
// Find factorial of any number using recursion.
unsigned long long calculate_factorial_recursive(int evaluation_number)
{
if (evaluation_number <= 1)
{
return 1;
}
return (unsigned long long)evaluation_number * calculate_factorial_recursive(evaluation_number - 1);
}
int FactorialNumbers_Recursion(void)
{
int user_input_number;
printf("Enter a non-negative integer: ");
if (scanf("%d", &user_input_number) != 1 || user_input_number < 0)
{
printf("Error: Invalid input. Please enter a non-negative integer.\n");
return 1;
}
if (user_input_number > 20)
{
printf("Error: Input too large. Factorial calculations above 20 cause integer overflow.\n");
return 1;
}
unsigned long long computational_result = calculate_factorial_recursive(user_input_number);
printf("The factorial of %d is: %llu\n", user_input_number, computational_result);
return 0;
}
// Generate nth Fibonacci term using recursion.
unsigned long long calculate_fibonacci_recursive(int term_index)
{
if (term_index == 0)
{
return 0;
}
if (term_index == 1)
{
return 1;
}
return calculate_fibonacci_recursive(term_index - 1) + calculate_fibonacci_recursive(term_index - 2);
}
int nthFibonacci(void)
{
int user_input_term;
printf("Enter the Fibonacci term index (n starting from 0): ");
if (scanf("%d", &user_input_term) != 1 || user_input_term < 0)
{
printf("Error: Invalid input. Please enter a non-negative integer.\n");
return 1;
}
if (user_input_term > 45)
{
printf("Error: Input too large. Values above 45 cause extreme execution delays.\n");
return 1;
}
unsigned long long structural_result = calculate_fibonacci_recursive(user_input_term);
printf("The Fibonacci term at index %d is: %llu\n", user_input_term, structural_result);
return 0;
}
// Find gcd (HCF) of two numbers using recursion.
int calculate_gcd_recursive(int first_number, int second_number)
{
if (second_number == 0)
{
return first_number;
}
return calculate_gcd_recursive(second_number, first_number % second_number);
}
int gcd2Recursion(void)
{
int user_input_first, user_input_second;
printf("Enter two integers: ");
if (scanf("%d %d", &user_input_first, &user_input_second) != 2)
{
printf("Error: Invalid numeric input.\n");
return 1;
}
if (user_input_first == 0 && user_input_second == 0)
{
printf("Error: GCD of 0 and 0 is mathematically undefined.\n");
return 1;
}
int absolute_first = abs(user_input_first);
int absolute_second = abs(user_input_second);
int computational_result = calculate_gcd_recursive(absolute_first, absolute_second);
printf("The GCD (HCF) of %d and %d is: %d\n", user_input_first, user_input_second, computational_result);
return 0;
}
// Find lcm of two numbers using recursion.
int lcm2Recursion(void)
{
int user_input_first, user_input_second;
printf("Enter two integers: ");
if (scanf("%d %d", &user_input_first, &user_input_second) != 2)
{
printf("Error: Invalid numeric input.\n");
return 1;
}
int absolute_first = abs(user_input_first);
int absolute_second = abs(user_input_second);
if (absolute_first == 0 || absolute_second == 0)
{
printf("The LCM of the given numbers is: 0\n");
return 0;
}
int greatest_common_divisor = calculate_gcd_recursive(absolute_first, absolute_second);
int lowest_common_multiple = (absolute_first / greatest_common_divisor) * absolute_second;
printf("The LCM of %d and %d is: %d\n", user_input_first, user_input_second, lowest_common_multiple);
return 0;
}
// Display all array elements using recursion.
void display_array_recursive(const int data_array[], int current_index, int array_size)
{
if (current_index >= array_size)
{
return;
}
printf("%d ", data_array[current_index]);
display_array_recursive(data_array, current_index + 1, array_size);
}
int DisplayArrayElementsRecursion(void)
{
int target_array_size;
printf("Enter the number of elements in the array: ");
if (scanf("%d", &target_array_size) != 1 || target_array_size <= 0 || target_array_size > 100)
{
printf("Error: Invalid array size. Please enter a value between 1 and 100.\n");
return 1;
}
int storage_array[100];
printf("Enter %d integers:\n", target_array_size);
for (int tracking_index = 0; tracking_index < target_array_size; tracking_index++)
{
if (scanf("%d", &storage_array[tracking_index]) != 1)
{
printf("Error: Invalid numeric input.\n");
return 1;
}
}
printf("The elements of the array are: ");
display_array_recursive(storage_array, 0, target_array_size);
printf("\n");
return 0;
}
// Find the sum of elements of the array using recursion.
int calculate_array_sum_recursive(const int data_array[], int current_index, int array_size)
{
if (current_index >= array_size)
{
return 0;
}
return data_array[current_index] + calculate_array_sum_recursive(data_array, current_index + 1, array_size);
}
int sumsArrayRecursion(void)
{
int target_array_size;
printf("Enter the number of elements in the array: ");
if (scanf("%d", &target_array_size) != 1 || target_array_size <= 0 || target_array_size > 100)
{
printf("Error: Invalid array size. Please enter a value between 1 and 100.\n");
return 1;
}
int storage_array[100];
printf("Enter %d integers:\n", target_array_size);
for (int tracking_index = 0; tracking_index < target_array_size; tracking_index++)
{
if (scanf("%d", &storage_array[tracking_index]) != 1)
{
printf("Error: Invalid numeric input.\n");
return 1;
}
}
int total_accumulated_sum = calculate_array_sum_recursive(storage_array, 0, target_array_size);
printf("The sum of elements in the array is: %d\n", total_accumulated_sum);
return 0;
}
int main(void)
{
// SumOFNumbersGivenRecursion();
// ReverseNumbersRecursion();
// CheckNumberPalindromeRecursion();
// SumOfDigitsOfNumberRecursion();
// FactorialNumbers_Recursion();
// nthFibonacci();
// gcd2Recursion();
// lcm2Recursion();
// DisplayArrayElementsRecursion();
// sumsArrayRecursion();
return 0;
}