-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdsa2.c
More file actions
396 lines (337 loc) · 9.35 KB
/
Copy pathdsa2.c
File metadata and controls
396 lines (337 loc) · 9.35 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
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
/*
This code base own and maintained by Tanmoy Samnata using standardization C23 (ISO/IEC 9899:2024)
*/
#include <stdio.h>
int checkLeapYear()
{
int year;
printf("Enter a year: ");
scanf("%d", &year);
// Leap year logic in one line
if ((year % 400 == 0) || (year % 4 == 0 && year % 100 != 0))
printf("%d is a leap year.\n", year);
else
printf("%d is not a leap year.\n", year);
return 0;
}
/*
Ternary Operator: The structure (condition) ? (value_if_true) : (value_if_false) for multi-line if-else block.
*/
int greatestOfThreeNumbersUsingTernaryOperators()
{
double n1, n2, n3, max;
printf("Enter three numbers: ");
scanf("%lf %lf %lf", &n1, &n2, &n3);
max = (n1 > n2) ? ((n1 > n3) ? n1 : n3) : ((n2 > n3) ? n2 : n3);
printf("%.2f is the greatest number.\n", max);
return 0;
}
int checkPositiveOrNegative()
{
double number;
printf("Enter a number: ");
if (scanf("%lf", &number) != 1)
{
printf("Invalid input.\n");
return 1;
}
if (number > 0.0)
{
printf("%.2f is positive.\n", number);
}
if (number < 0.0)
{
printf("%.2f is negative.\n", number);
}
else
{
printf("The number is zero (neither positive nor negative).\n");
}
return 0;
}
int checkPositiveOrNegativeUsingTernaryOperators()
{
double n;
printf("Enter a number: ");
scanf("%lf", &n);
// Using nested ternary operators
printf("%.2f is %s.\n", n, (n > 0) ? "positive" : (n < 0) ? "negative"
: "zero");
return 0;
}
int checkingCharacterIsAnAlphabetOrNot()
{
char c;
printf("Enter character: ");
scanf("%c", &c);
// One-line check using ternary operator using manual ASCII code
printf("'%c' is %s an alphabet.\n", c,
(c >= 'a' && c <= 'z') || (c >= 'A' && c <= 'Z') ? "" : "not");
return 0;
}
int UppercaseLowercaseSpecialCharacteraaaaOrDigit()
{
char c;
printf("Enter character: ");
scanf("%c", &c);
printf("'%c' is %s.\n", c,
(c >= 'A' && c <= 'Z') ? "Uppercase" : (c >= 'a' && c <= 'z') ? "Lowercase"
: (c >= '0' && c <= '9') ? "a Digit"
: "a Special Character");
return 0;
}
int positiveOrNegative()
{
double n;
printf("Enter a number: ");
scanf("%lf", &n);
printf("The number is %s.\n", (n > 0) ? "POSITIVE" : (n < 0) ? "NEGATIVE"
: "ZERO");
return 0;
}
int checkNumberIsEvenOrOdd()
{
int n;
printf("Enter an integer: ");
scanf("%d", &n);
// True:False
printf("The number is %s.\n", (n % 2 == 0) ? "EVEN" : "ODD");
return 0;
}
int greatestAmongThreeNumbers()
{
double a, b, c;
printf("Enter three numbers: ");
scanf("%lf %lf %lf", &a, &b, &c);
double max = (a > b) ? ((a > c) ? a : c) : ((b > c) ? b : c);
printf("The greatest number is: %.2lf\n", max);
return 0;
}
int checkLeapYearUsingTernaryOperators()
{
int year;
printf("Enter a year: ");
scanf("%d", &year);
printf("%d is %sa leap year.\n", year, ((year % 400 == 0) || (year % 4 == 0 && year % 100 != 0)) ? " " : "not ");
return 0;
}
int dateIsCorrectOrNot()
{
int d, m, y, max_d = 31;
printf("Enter date (DD MM YYYY): ");
scanf("%d %d %d", &d, &m, &y);
// 1. Basic validation for year, month, and day
if (y < 1 || m < 1 || m > 12 || d < 1)
{
printf("Invalid Date\n");
return 0;
}
// 2. Adjust maximum days for April, June, September, November
if (m == 4 || m == 6 || m == 9 || m == 11)
{
max_d = 30;
}
// 3. Adjust maximum days for February
else if (m == 2)
{
max_d = ((y % 400 == 0) || (y % 4 == 0 && y % 100 != 0)) ? 29 : 28;
}
// 4. Final check
printf("%s Date\n", (d <= max_d) ? "Valid" : "Invalid");
return 0;
}
int votingEligibilityChecker()
{
int usersAge;
char name[50];
printf("Enter your name: ");
fgets(name, sizeof(name), stdin);
printf("Enter your age: ");
scanf("%d", &usersAge);
printf("%sYou are %d and %s to vote.\n", name, usersAge, (usersAge >= 18) ? "Eligible" : "Not eligible");
return 0;
}
int maximumBetweenTwoNumbers()
{
double a, b;
printf("Enter two numbers: ");
scanf("%lf %lf", &a, &b);
printf("The maximum number is: %lf\n", (a > b) ? a : b);
return 0;
}
// Universal macro to find max of two values
#define MAXB2(a, b) ((a) > (b) ? (a) : (b))
int maximumBetweenTheThreeNumbers()
{
int a = 12, b = 45, c = 23;
// Nesting the macro to find the max of three numbers
int MAXB3 = MAXB2(a, MAXB2(b, c));
printf("The maximum is: %d\n", MAXB3);
return 0;
}
// Check whether a number is negative, positive or zero.
int CheckNegativePositiveZero()
{
double numberToCheck;
printf("Enter a number: ");
if (scanf("%lf", &numberToCheck) == 1)
printf(numberToCheck > 0 ? "Positive\n" : (numberToCheck < 0 ? "Negative\n" : "Zero\n"));
return 0;
}
int divisibleBy5And11OrNot()
{
int numberToCheck;
printf("Enter any number: ");
scanf("%d", &numberToCheck);
// Using 55 (5 * 11) is mathematically equivalent and more efficient
if (numberToCheck % 55 == 0)
{
printf("%d is divisible by 5 and 11.\n", numberToCheck);
}
else
{
printf("%d is NOT divisible by 5 and 11.\n", numberToCheck);
}
return 0;
}
int evenOrOdd()
{
int evenOrNot;
printf("Enter a number: ");
scanf("%d", &evenOrNot);
(evenOrNot % 2 == 0) ? printf("%d is even.\n", evenOrNot) : printf("%d is odd.\n", evenOrNot);
return 0;
}
int leapYearOrNot()
{
int yearToCheck;
printf("Enter a year: ");
scanf("%d", &yearToCheck);
if ((yearToCheck % 4 == 0 && yearToCheck % 100 != 0) || (yearToCheck % 400 == 0))
{
printf("%d is a leap year.\n", yearToCheck);
}
else
{
printf("%d is NOT a leap year.\n", yearToCheck);
}
return 0;
}
int alphabetOrNot()
{
char characterToCheck;
printf("Enter any character: ");
scanf("%c", &characterToCheck);
if ((characterToCheck >= 'a' && characterToCheck <= 'z') || (characterToCheck >= 'A' && characterToCheck <= 'Z'))
{
printf("'%c' is an alphabet.\n", characterToCheck);
}
else
{
printf("'%c' is NOT an alphabet.\n", characterToCheck);
}
return 0;
}
int numberOfWeekDays()
{
int dayNumber;
// Array of day names (1-indexed mapping)
const char *days[] = {"", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday", "Sunday"};
printf("Enter week number (1-7): ");
if (scanf("%d", &dayNumber) == 1 && dayNumber >= 1 && dayNumber <= 7)
{
printf("%s\n", days[dayNumber]);
}
else
{
printf("Invalid input! Please enter a number between 1 and 7.\n");
}
return 0;
}
int numberOfDaysInAMonth()
{
int month;
// Array where index 1 = Jan, 2 = Feb, etc. (0 is a placeholder)
int days[] = {0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
printf("Enter month number (1-12): ");
if (scanf("%d", &month) != 1 || month < 1 || month > 12)
{
return printf("Invalid month!\n"), 1;
}
month == 2 ? printf("28 or 29 days\n") : printf("%d days\n", days[month]);
return 0;
}
// • Count the total number of notes in a given amount.
int notesIGvenAmount()
{
int month;
// Array where index 1 = Jan, 2 = Feb, etc. (0 is a placeholder)
int days[] = {0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
printf("Enter month number (1-12): ");
if (scanf("%d", &month) != 1 || month < 1 || month > 12)
{
return printf("Invalid month!\n"), 1;
}
month == 2 ? printf("28 or 29 days\n") : printf("%d days\n", days[month]);
return 0;
}
int equilateralIsoscelesOrScaleneTriangle()
{
float a, b, c;
printf("Enter three sides of the triangle: ");
if (scanf("%f %f %f", &a, &b, &c) != 3 || !(a + b > c && a + c > b && b + c > a))
{
printf("Invalid Triangle\n");
return 1;
}
if (a == b && b == c)
printf("Equilateral Triangle\n");
else if (a == b || b == c || a == c)
printf("Isosceles Triangle\n");
else
printf("Scalene Triangle\n");
return 0;
}
int allRootsOfAQuadraticEquation()
{
float a, b, c, d, r1, r2, i = 0, s = 0;
printf("Enter a, b, c: ");
if (scanf("%f %f %f", &a, &b, &c) != 3 || a == 0)
return 1;
d = b * b - 4 * a * c;
float v = d < 0 ? -d : d;
// Inline Babylonian square root approximation
if (v > 0)
{
s = v;
for (int k = 0; k < 20; k++)
s = (s + v / s) / 2;
}
r1 = -b / (2 * a);
r2 = s / (2 * a);
if (d > 0)
printf("Roots: %.2f and %.2f\n", r1 + r2, r1 - r2);
if (d == 0)
printf("Root: %.2f\n", r1);
if (d < 0)
printf("Roots: %.2f + %.2fi and %.2f - %.2fi\n", r1, r2, r1, r2);
return 0;
}
int profitOrLoss()
{
float cp, sp;
printf("Enter CP and SP: ");
if (scanf("%f %f", &cp, &sp) != 2)
return 1;
if (sp == cp)
printf("No Profit No Loss\n");
else
printf("%s = %.2f\n", (sp > cp) ? "Profit" : "Loss", (sp > cp) ? (sp - cp) : (cp - sp));
return 0;
}
int main()
{
checkLeapYear();
// alphabetOrNot();
return 0;
}