Repository navigation
Expand file tree
/
Copy pathfunction-test-case.c
More file actions
366 lines (283 loc) · 7.7 KB
/
Copy pathfunction-test-case.c
File metadata and controls
366 lines (283 loc) · 7.7 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
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
// Write a C program, for the functions float sine(int n, float x), and float cosine(int n, int x), where n is the number of terms of sine series and x is degrees. i have already written the main function to take input these variable just return the output result of the above sine and cosine functions.
#include <stdio.h>
float sine(int n, float x);
float cosine(int n, float x);
int factorial(int n);
int main() {
int n;
float x;
printf("Enter the number of terms in the series: ");
scanf("%d", &n);
printf("Enter the angle in degrees: ");
scanf("%f", &x);
// Convert degrees to radians
x = x * 3.14159 / 180.0;
printf("Sine: %.4f\n", sine(n, x));
printf("Cosine: %.4f\n", cosine(n, x));
return 0;
}
float sine(int n, float x) {
float result = 0.0;
int i;
for (i = 0; i < n; ++i) {
result += (i % 2 == 0 ? 1 : -1) * power(x, 2 * i + 1) / factorial(2 * i + 1);
}
return result;
}
float cosine(int n, float x) {
float result = 0.0;
int i;
for (i = 0; i < n; ++i) {
result += (i % 2 == 0 ? 1 : -1) * power(x, 2 * i) / factorial(2 * i);
}
return result;
}
int factorial(int n) {
int result = 1;
for (int i = 1; i <= n; ++i) {
result *= i;
}
return result;
}
float power(float base, int exponent) {
float result = 1.0;
for (int i = 0; i < exponent; ++i) {
result *= base;
}
return result;
}
// Method 2
#include <stdio.h>
#include <math.h>
int decimalToBinary(int d){
int bin = 0, rem, base = 1;
while(d > 0){
rem = d % 2;
bin += rem * base;
d /= 2;
base *= 10;
}
printf("%d", bin);
}
int decimalToHex(int d){
char hex[20];
int index = 0;
while(d != 0){
int rem = d % 16;
if(rem < 10){
hex[index++] = rem + '0';
} else{
hex[index++] = rem - 10 + 'A';
}
d /= 16;
}
if(index == 0){
hex[index++] = '0';
}
hex[index] = '\0';
for(int i = index - 1; i >= 0; i--){
printf("%c", hex[i]);
}
}
void main(){
int d;
printf("Enter a positive decimal number : ");
scanf("%d", &d);
printf("The binary number of decimal %d is : ");
decimalToBinary(d);
printf("\n");
printf("The hex number of decimal %d is : ", d);
decimalToHex(d);
printf("\n");
}
// Conversion modified
#include <stdio.h>
#include <math.h>
int decimalToBinary(int d) {
int bin = 0, rem, base = 1;
while (d > 0) {
rem = d % 2;
bin += rem * base;
d /= 2;
base *= 10;
}
printf("%d", bin);
}
int decimalToHex(int d) {
char hex[20];
int index = 0;
while (d != 0) {
int rem = d % 16;
if (rem < 10) {
hex[index++] = rem + '0';
} else {
hex[index++] = rem - 10 + 'A';
}
d /= 16;
}
if (index == 0) {
hex[index++] = '0';
}
hex[index] = '\0';
for (int i = index - 1; i >= 0; i--) {
printf("%c", hex[i]);
}
}
int main() {
int d;
printf("Enter a positive decimal number: ");
scanf("%d", &d);
printf("The binary number of decimal %d is: ", d);
decimalToBinary(d);
printf("\n");
printf("The hex number of decimal %d is: ", d);
decimalToHex(d);
printf("\n");
return 0;
}
// Decimal to hex try
#include <stdio.h>
char to_hex(int num) {
// Convert decimal digit (0-9) to its corresponding hexadecimal character (0-F)
return (num < 10) ? num + '0' : num - 10 + 'A';
}
void dec_to_hex(int decimal_num, char *hex_string) {
// Handle negative numbers (convert to positive and prefix with '-')
int is_negative = 0;
if (decimal_num < 0) {
is_negative = 1;
decimal_num = -decimal_num;
}
int i = 0;
while (decimal_num != 0) {
int remainder = decimal_num % 16;
hex_string[i++] = to_hex(remainder);
decimal_num /= 16;
}
// Append null terminator for string
hex_string[i] = '\0';
// Reverse the hex string (optional, can be printed directly in the loop)
if (is_negative) {
hex_string[i++] = '-';
}
for (int j = 0; j < i / 2; j++) {
char temp = hex_string[j];
hex_string[j] = hex_string[i - j - 1];
hex_string[i - j - 1] = temp;
}
}
int main() {
int decimal_num;
char hex_string[100];
printf("Enter a decimal number: ");
scanf("%d", &decimal_num);
dec_to_hex(decimal_num, hex_string);
printf("The hexadecimal equivalent: %s\n", hex_string);
return 0;
}
// Try 4
#include <stdio.h>
#include <string.h>
char *decimalToHex(int decimal_num) {
// Handle negative numbers (convert to positive and prefix with '-')
int is_negative = 0;
if (decimal_num < 0) {
is_negative = 1;
decimal_num = -decimal_num;
}
// Dynamically allocate a string to hold the hex digits
char *hex_string = malloc(sizeof(char) * (14 + is_negative)); // Max 13 hex digits + '-' + '\0'
if (hex_string == NULL) {
printf("Memory allocation failed!\n");
return NULL;
}
int i = 0;
while (decimal_num != 0) {
int remainder = decimal_num % 16;
hex_string[i++] = to_hex(remainder);
decimal_num /= 16;
}
// Append null terminator
hex_string[i] = '\0';
// Reverse the hex string
if (is_negative) {
hex_string[i++] = '-';
}
for (int j = 0; j < i / 2; j++) {
char temp = hex_string[j];
hex_string[j] = hex_string[i - j - 1];
hex_string[i - j - 1] = temp;
}
return hex_string;
}
char to_hex(int num) {
// Convert decimal digit (0-9) to its corresponding hexadecimal character (0-F)
return (num < 10) ? num + '0' : num - 10 + 'A';
}
// int main() {
// int decimal_num;
// printf("Enter a decimal number: ");
// scanf("%d", &decimal_num);
// // char *hex_string = decimalToHex(decimal_num);
// if (hex_string != NULL) {
// printf("The hexadecimal equivalent: %s\n", hex_string);
// free(hex_string); // Free the allocated memory
// }
// return 0;
// }
// Try 5
#include <stdio.h>
void decimalToHex(int d);
int main() {
int decimal;
printf("Enter a decimal number: ");
scanf("%d", &decimal);
printf("Hexadecimal equivalent: ");
decimalToHex(decimal);
return 0;
}
void decimalToHex(int d) {
int remainder, quotient;
int i = 1, j, temp;
char hexadecimal[100];
quotient = d;
while (quotient != 0) {
temp = quotient % 16;
// To convert integer into character
if (temp < 10)
temp += 48;
else
temp += 55;
hexadecimal[i++] = temp;
quotient /= 16;
}
printf("0x");
for (j = i - 1; j > 0; j--)
printf("%c", hexadecimal[j]);
printf("\n");
}
// Try 6
#include <stdio.h>
void decimalToBinary(int d);
int main() {
int decimal;
printf("Enter a decimal number: ");
scanf("%d", &decimal);
printf("Binary equivalent: ");
decimalToBinary(decimal);
return 0;
}
void decimalToBinary(int d) {
int binary[32]; // Assuming 32-bit binary representation
int i = 0;
while (d > 0) {
binary[i] = d % 2;
d /= 2;
i++;
}
// Printing binary in reverse order
for (int j = i - 1; j >= 0; j--) {
printf("%d", binary[j]);
}
printf("\n");
}