-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrecfunctions_ornekler.txt
275 lines (201 loc) · 4.97 KB
/
recfunctions_ornekler.txt
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
//fonksiyon hatýrlatma
/* Fig. 5.4: fig05_04.c
Finding the maximum of three integers */
#include <stdio.h>
int maximum( int, int, int ); /* function prototype */
int main()
{
int a, b, c;
printf( "Enter three integers: " );
scanf( "%d%d%d", &a, &b, &c );
printf( "Maximum is: %d\n", maximum( a, b, c ) );
return 0;
}
/* Function maximum definition */
int maximum( int x, int y, int z )
{
int max = x;
if ( y > max )
max = y;
if ( z > max )
max = z;
return max;
}
/***************************************************
#include<stdio.h>
void message(int times){
if (times > 0) {
{printf("\n Bu bir recursive fonksiyon. "); }
message(times - 1); }
}
main()
{
message(5);}
/************************************
/* ex05_47.c */
#include <stdio.h>
int mystery( int, int );
int main()
{
int x, y;
printf( "Enter two integers: " );
scanf( "%d%d", &x, &y );
printf( "The result is %d\n", mystery( x, y ) );
return 0;
}
/* Parameter b must be a positive
integer to prevent infinite recursion */
int mystery( int a, int b )
{
if ( b == 1 )
return a;
else
return a + mystery( a, b - 1 );
}
/******************************************************************
#include <stdio.h>
int gcd(int a, int b) ;
int main()
{
int x, y;
printf( "Enter two integers: " );
scanf( "%d%d", &x, &y );
printf( "The result is %d\n", gcd( x, y ) );
return 0;
}
/* Parameter b must be a positive
integer to prevent infinite recursion */
int gcd(int a, int b)
{
return ( b == 0 ? a : gcd(b, a % b) );
}
/******************************************************************
/* Fig. 5.14: fig05_14.c
Recursive factorial function */
#include <stdio.h>
long factorial( long );
int main()
{
int i;
for ( i = 1; i <= 10; i++ )
printf( "%2d! = %ld\n", i, factorial( i ) );
return 0;
}
long factorial( long number )
{
if ( number <= 1 )
return 1;
else
return ( number * factorial( number - 1 ) );
}
/***************************************************
#include <stdio.h>
int hcf(int n1, int n2);
int main()
{
int n1, n2;
printf("iki sayý giriniz ....");
scanf("%d %d", &n1, &n2);
printf("%d ve %d için En buyuk ortak bolen %d.", n1, n2, hcf(n1,n2));
return 0;
}
int hcf(int n1, int n2)
{
if (n2 != 0)
return hcf(n2, n1%n2);
else
return n1;
}
/*****************************************************
#include <stdio.h>
int gcd(int a, int b)
{
if (a == 0)
return b;
if (b == 0)
return a;
if (a == b)
return a;
if (a > b)
return gcd(a-b, b);
return gcd(a, b-a);
}
int main()
{
int a = 98, b = 56;
printf("GCD of %d and %d is %d ", a, b, gcd(a, b));
return 0;
}
*************************
#include <stdio.h>
int findSum(int A[], int N)
{
if (N <= 0)
return 0;
return (findSum(A, N - 1) + A[N - 1]);
}
// Driver code
int main()
{
int b[] = { 1, 2, 3, 4, 5,10,11 };
int N = sizeof(b) / sizeof(b[0]);
printf("%d", findSum(b, N));
return 0;
}
/**************************************************************
#include<stdio.h>
/* Recursive function to search x in arr[l..r] */
int recSearch(int arr[], int l, int r, int x)
{
if (r < l)
return -1;
if (arr[l] == x)
return l;
if (arr[r] == x)
return r;
return recSearch(arr, l+1, r-1, x);
}
int main()
{
int arr[] = {12, 34, 54, 2, 3}, i;
int n = sizeof(arr)/sizeof(arr[0]);
int x = 3;
int index = recSearch(arr, 0, n-1, x);
if (index != -1)
printf("Element %d is present at index %d", x, index);
else
printf("Element %d is not present", x);
return 0;
}
/******************************************
#include<stdio.h>
#define MAX 100
int getMaxElement(int []); // takes array of int as parameter
int size;
int main()
{
int arr[MAX], max, i;
printf("\n\nEnter the size of the array: ");
scanf("%d", &size);
printf("\n\nEnter %d elements\n\n", size);
for(i = 0; i < size; i++)
{
scanf("%d", &arr[i]);
}
max = getMaxElement(arr); // passing the complete array as parameter
printf("\n\nLargest element of the array is %d\n\n", max);
printf("\n\n\t\t\tCoding is Fun !\n\n\n");
return 0;
}
int getMaxElement(int a[])
{
static int i = 0, max =- 9999; // static int max=a[0] is invalid
if(i < size) // till the last element
{
if(max < a[i])
max = a[i];
i++; // to check the next element in the next iteration
getMaxElement(a); // recursive call
}
return max;
}