This repository has been archived by the owner on Feb 4, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 14
/
examples.c
2531 lines (1763 loc) · 47.2 KB
/
examples.c
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
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/* The simple hello world program */
#include <stdio.h>
#include <stdlib.h>
int main() {
printf("Hello, World!");
return 0;
}
/* ---------------------------------------------------------------- */
/* Hello world using string */
#include <stdio.h>
#include <stdlib.h>
int main() {
char hello[13] = "Hello, World!";
printf("%s", hello);
return 0;
}
/* ---------------------------------------------------------------- */
/* Sum of two numbers */
#include <stdio.h>
#include <stdlib.h>
int main()
{
int a;
int b;
int sum;
printf("Enter two numbers to get the sum (exit with input 0)\n");
scanf("%d %d", &a, &b);
while (a != 0 && b != 0)
{
sum = a + b;
printf("The sum of %d and %d is %d\n\n", a, b, sum);
printf("Enter two numbers to get the sum\n");
scanf("%d %d", &a, &b);
}
return 0;
}
/* ---------------------------------------------------------------- */
/* Convert float to integer */
#include <stdio.h>
#include <stdlib.h>
int main() {
float a = 10.987;
int b = (int) a;
printf("%d", b);
return 0;
}
/* ---------------------------------------------------------------- */
/* Experiment with scanf return values */
#include <stdio.h>
#include <stdlib.h>
int main()
{
int a;
int b;
if (scanf("%d %d", &a, &b) != 2)
{
printf("1");
}
else
{
printf("0");
}
return 0;
}
/* ---------------------------------------------------------------- */
/* Simplest calculator, checks for operator input but not type of numerical input */
#include <stdio.h>
#include <stdlib.h>
int main()
{
float a;
float b;
char operator;
float output;
int error = 0;
printf("Enter two numbers followed by the operator + ,- ,* or / (exit with input 0)\n");
scanf("%f %f %c", &a, &b, &operator);
while (a != 0 && b != 0)
{
if (operator== '+')
{
output = a + b;
}
else if (operator== '-')
{
output = a - b;
}
else if (operator== '*')
{
output = a * b;
}
else if (operator== '/')
{
output = a / b;
}
else
{
printf("Please use one of the four operators.\n\n");
error = 1;
}
if (error == 0)
{
printf("%f%c%f = %f\n\n", a, operator, b, output);
}
printf("Enter two numbers followed by the operator + ,- ,* or / (exit with input 0)\n");
scanf("%f %f %c", &a, &b, &operator);
error = 0;
}
return 0;
}
/* ---------------------------------------------------------------- */
/* Average of four numbers, exits if one of the numbers is 0 */
#include <stdio.h>
#include <stdlib.h>
int main()
{
float a;
float b;
float c;
float d;
float average;
printf("Enter four numbers to get their average (exit with input 0)\n");
scanf("%f %f %f %f", &a, &b, &c, &d);
while (a != 0 && b != 0 && c != 0 && d != 0)
{
average = (a + b +c + d) / 4;
printf("The average is %f.\n\n", average);
printf("Enter four numbers to get their average (exit with input 0)\n");
scanf("%f %f %f %f", &a, &b, &c, &d);
}
return 0;
}
/* ---------------------------------------------------------------- */
/* Average of four numbers, exits if all of the numbers are 0 */
#include <stdio.h>
#include <stdlib.h>
int main()
{
float a;
float b;
float c;
float d;
float average;
printf("Enter four numbers to get their average (exit with input 0)\n");
scanf("%f %f %f %f", &a, &b, &c, &d);
if (a+b+c+d)
{
average = (a + b +c + d) / 4;
printf("The average is %f.\n\n", average);
printf("Enter four numbers to get their average (exit with input 0)\n");
scanf("%f %f %f %f", &a, &b, &c, &d);
}
return 0;
}
/* ---------------------------------------------------------------- */
/* Even or odd, method 1 */
#include <stdio.h>
#include <stdlib.h>
int main()
{
int number, half;
printf("Enter a number to check if it is even or odd\n");
scanf("%d", &number);
half = number / 2;
if (half * 2 == number)
printf("%d is even.\n", number);
else
printf("%d is odd.\n", number);
return 0;
}
/* ---------------------------------------------------------------- */
/* Even or odd, method 2 */
#include <stdio.h>
#include <stdlib.h>
int main()
{
int number;
printf("Enter a number to check if it is even or odd\n");
scanf("%d", &number);
if (number % 2 == 0)
printf("%d is even.\n", number);
else
printf("%d is odd.\n", number);
return 0;
}
/* ---------------------------------------------------------------- */
/* Equality test for floating point numbers */
#include <stdio.h>
#include <math.h>
int main()
{
double tolerance = 0.00000001;
int boolean;
float a = 0.1 + 0.2;
float b = 0.3;
boolean = (fabs (a - b) < tolerance);
printf("%d", boolean);
return 0
}
/* ---------------------------------------------------------------- */
/* Ask seperately for three numbers and print their sum, product and average */
#include <stdio.h>
#include <stdlib.h>
int main()
{
int a, b, c, sum, product, average;
printf("Enter first number:\n");
scanf("%d", &a);
printf("Enter second number:\n");
scanf("%d", &b);
printf("Enter third number:\n");
scanf("%d", &c);
sum = a + b + c;
product = a * b * c;
average = (a + b +c) / 3;
printf("The sum of %d, %d and %d is %d\n", a, b, c, sum);
printf("The product of %d, %d and %d is %d\n", a, b, c, product);
printf("The average of %d, %d and %d is %d\n", a, b, c, average);
}
/* ---------------------------------------------------------------- */
/* Maximum of three numbers */
#include <stdio.h>
#include <stdlib.h>
int main()
{
int a, b, c, max;
printf("Enter three numbers to find the maximum:\n");
scanf("%d %d %d", &a, &b, &c);
max = a;
if (b > max)
max = b;
if (c > max)
max = c;
printf("The maximum is %d", max);
return 0;
}
/* ---------------------------------------------------------------- */
/* Circle length and circumference */
#include <stdio.h>
#include <stdlib.h>
#define PI 3.141592
int main()
{
float r, circum, area;
printf("Enter circle radius:\n");
scanf("%f", &r);
circum = 2 * PI * r;
area = PI * r * r;
printf("Circumference = %f\nArea = %f", circum, area);
return 0;
}
/* ---------------------------------------------------------------- */
/* VAT Price Calculator */
#include <stdio.h>
#include <stdlib.h>
int main()
{
float price, price_vat, vat;
printf("Enter price without VAT and VAT percentage:\n");
scanf("%f %f", &price, &vat);
price_vat = price + (price * vat / 100);
printf("Price without VAT is: %f\nVAT is: %f %%\nPrice with VAT is: %f\n", price, vat, price_vat);
return 0;
}
/* ---------------------------------------------------------------- */
/* Cost of stamps */
#include <stdio.h>
#include <stdlib.h>
int main()
{
char location;
const float price_i = 0.70, price_n = 0.23, price_l = 0.15;
float price, total;
int num;
printf("Will you be sending your letter (L)ocally, (N)ationally or (I)nternationally?\n");
scanf("%c", &location);
printf("How many letters will you be sending?\n");
scanf("%d", &num);
switch (location)
{
case 'L':
price = price_l;
case 'N':
price = price_n;
case 'I':
price = price_i;
}
total = price * num;
printf("Sending %d letters, will cost you $%f\n", num, total);
return 0;
}
/* ---------------------------------------------------------------- */
/* BMI Caclulator */
#include <stdio.h>
#include <stdlib.h>
int main()
{
float height, weight, bmi;
printf("Enter your height:\n");
scanf("%f", &height);
printf("Enter your weight:\n");
scanf("%f", &weight);
bmi = weight / ( height * height);
printf("You weight %f kilos, you are %f meters tall and your BMI is %f", weight, height, bmi);
return 0;
}
/* ---------------------------------------------------------------- */
/* Cheap or expensive gasoline */
#include <stdio.h>
#include <stdlib.h>
int main()
{
float liters, price_total, price_per_liter;
printf("Enter how much you paid for gasoline:\n");
scanf("%f", &price_total);
printf("Enter liters:\n");
scanf("%f", &liters);
price_per_liter = price_total / liters;
if (price_per_liter > 1.3)
printf("Expensive gasoline\n");
else
printf("Cheap gasoline");
return 0;
}
/* ---------------------------------------------------------------- */
/* Swap values of two variables */
#include <stdio.h>
#include <stdlib.h>
int main()
{
int a, b, temp;
printf("Enter value of a:\n");
scanf("%d", &a);
printf("Enter value of b:\n");
scanf("%d", &b);
temp = a;
a = b;
b = temp;
printf("a = %d\nb = %d\n", a, b);
return 0;
}
/* ---------------------------------------------------------------- */
/* Calculate the sum of the first billion integers */
#include <stdio.h>
int main() {
long long int sum = 0;
for (int i = 1; i < 1000000000; i++)
{
sum += i;
printf("%lli\n", sum);
}
return 0;
}
/* ---------------------------------------------------------------- */
/* Dice Roller */
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
int main()
{
char roll;
int dice1, dice2;
srand(time(NULL));
printf("Roll dice? (y/n)\n");
scanf("%c", &roll);
while (roll != 'n')
{
if (roll == 'y')
{
dice1 = rand() % 6 + 1;
dice2 = rand() % 6 + 1;
printf("You rolled %d and %d\n\n", dice1, dice2);
printf("Roll dice? (y/n)\n");
scanf("%c", &roll);
}
else
{
scanf("%c", &roll);
}
}
}
/* ---------------------------------------------------------------- */
/* Euclidean Algorithm computing the greatest commin divisor of two integers */
#include <stdio.h>
int gcd(long long int a, long long int b)
{
long long int t;
while (b != 0)
{
t = b;
b = a % b;
a = t;
}
if (a < 0) a *= -1;
printf("The greatest common divisor is: %lli\n\n", a);
return 0;
}
int main()
{
long long int num1, num2;
printf("Enter two integers to calculate the greatest common divisor:\n");
scanf("%lli %lli", &num1, &num2);
gcd(num1, num2);
return 0;
}
/* ---------------------------------------------------------------- */
/*
* A digital building alarm system returns an 1-byte integer as a value,
* whose bits transmit the following data:
*
* Bits 7, 6, 5 and 4 specify the checkpoint (16 possible combinations from 0-15
* Bits 3 and 2 the type of emergency (0-None, 1-Fire, 2-Break-in, 3-Smoke)
* Bit 1 show the system state (1-Working, 0-Failure)
* Bit 0 is not used.
*
* Write a program that takes the alarm system output as input and displays
* the checkpoint and the emergency type. In case of a system failure, the
* output should be "System Failure".
*/
#include <stdio.h>
#include <stdlib.h>
int main()
{
int input, checkpoint, emergency, state;
printf("Enter alarm system output:\n");
scanf("%d", &input);
checkpoint = (input & 240) >> 4;
emergency = (input & 12) >> 2;
state = (input & 2) >> 1;
if (state == 0) {
printf("System Failure\n");
return 1;
}
printf("Checkpoint: %d\n", checkpoint + 1);
if (emergency == 0) printf ("Emergency: None\n");
if (emergency == 1) printf ("Emergency: Fire\n");
if (emergency == 2) printf ("Emergency: Break-in\n");
if (emergency == 3) printf ("Emergency: Smoke\n");
return 0;
}
/* ---------------------------------------------------------------- */
/* Convert seconds to hours, minutes and seconds */
#include <stdio.h>
#include <stdlib.h>
int main() {
int sec, min, h;
printf("Enter time in seconds:\n");
scanf("%d", &sec);
h = sec / 3600;
min = sec % 3600 / 60;
sec = sec % 3600 % 60;
printf("%d hours, %d minutes and %d seconds\n", h, min, sec);
return 0;
}
/* ---------------------------------------------------------------- */
/* Average of marks from three modules and evaluation - max: 100 pass > 50 */
#include <stdio.h>
#include <stdlib.h>
int main() {
int mark1, mark2, mark3, average;
printf("Enter marks in three modules:\n");
scanf("%d %d %d", &mark1, &mark2, &mark3);
average = (mark1 + mark2 + mark3) / 3;
if (average >= 50)
printf("Pass\n");
else
printf("Fail\n");
return 0;
}
/* ---------------------------------------------------------------- */
/* Character to ASCII code */
#include <stdio.h>
#include <stdlib.h>
int main()
{
char c;
printf("Enter a character to get its ASCII code:\n");
c = getchar();
printf("Character: %c\n ASCII: %d", c, c);
return 0;
}
/* ---------------------------------------------------------------- */
/* ASCII code to character */
#include <stdio.h>
#include <stdlib.h>
int main()
{
int ascii;
printf("Enter aan ASCII code to get the character:\n");
scanf("%d", &ascii);
printf("%c", ascii);
return 0;
}
/* ---------------------------------------------------------------- */
/* Read chatacter and print it on screen */
#include <stdio.h>
#include <stdlib.h>
int main()
{
char c;
printf("Enter character:\n");
c = getchar();
putchar(c);
putchar('\n');
return 0;
}
/* ---------------------------------------------------------------- */
/* Checks if character is a capital latin letter */
#include <stdio.h>
#include <stdlib.h>
int main()
{
char c;
printf("Enter a character to check if it is a capital latin letter:\n");
c = getchar();
if (c >= 65 && c <= 90)
printf("The character is a capital latin letter.\n");
else
printf("The character is not a capital latin letter.\n");
return 0;
}
/* ---------------------------------------------------------------- */
/* Takes character as input and:
* Prints the character if it is a lowercase letter
* Prints "You enter a digit" if it is a number
* Does nothing otherwise
*/
#include <stdio.h>
#include <stdlib.h>
int main()
{
char ch;
ch = getchar();
if (ch >= 97 && ch <= 122)
putchar(ch);
if (ch >= 48 && ch <= 57)
printf("You entered a digit");
return 0;
}
/* ---------------------------------------------------------------- */
/* Take a character as input, output the next ASCII character it it's a letter
* or the character if it's a number.
*/
#include <stdio.h>
#include <stdlib.h>
int main()
{
char ch;
ch = getchar();
if (ch >= 48 && ch <= 57) /* number */
putchar(ch);
if ((ch >= 65 && ch <= 90) || (ch >= 97 && ch <= 122)) /* Capital and lowercase letters */
printf("%c", ch + 1);
return 0;
}
/* ---------------------------------------------------------------- */
/* Take an floating point number and display the integer and the
* fractional parts seperately */
#include <stdio.h>
int main()
{
float num, fra_p;
int int_p;
scanf("%f", &num);
int_p = num;
fra_p = num - int_p;
printf("%d\n", int_p);
printf("%f\n", fra_p);
return 0;
}
/* ---------------------------------------------------------------- */
/* Area of a circle */
#include <stdio.h>
#include <math.h>
#define PI 3.141592653589793
int main()
{
double r, area;
printf("Enter radius:\n");
scanf("%lf", &r);
area = PI * pow(r, 2);
printf("A circle of radius %f has an area of %lf\n", r, area);
return 0;
}
/* ---------------------------------------------------------------- */
/* Takes a float and an int and calculates the remainder of their division,
* ex. 5.14 and will output 1.14 */
#include <stdio.h>
int main()
{
float num_f;
int num_i;
int num_f_to_i;
float remainder;
scanf("%f %d", &num_f, &num_i);
num_f_to_i = num_f;
remainder = (num_f_to_i % num_i) + (num_f - num_f_to_i);
printf("%f", remainder);
return 0;
}
/* ---------------------------------------------------------------- */
/* Takes a float and an int and calculates the remainder of their division,
* ex. 5.14 and will output 1.14 */
/* Alternative solution */
#include <stdio.h>
int main()
{
float num_f, remainder;
int num_i, temp;
scanf("%f %d", &num_f, &num_i);
temp = num_f / num_i;
remainder = num_f - temp * num_i;
printf("%f\n", remainder);
return 0;
}
/* ---------------------------------------------------------------- */
/* Stylized multiplication of two floating-point numbers */
#include <stdio.h>
int main()
{
float a, b, product;
scanf("%f %f", &a, &b);
product = a * b;
printf("\n %9.2f\n", a);
printf("x%9.2f\n", b);
printf("==========\n");
printf(" %9.2f\n\n", product);
return 0;
}
/* ---------------------------------------------------------------- */
/* Tax calculator
*
* Income Tax
* <5000 0%
* 5000-10000 5%
* 10000-30000 15%
* >30000 35%
*/
#include <stdio.h>
int main()
{
float income, tax;
scanf("%f", &income);
if (income < 5000)
tax = 0;
else if (income >= 5000 && income <= 10000)
tax = income * 0.05;
else if (income > 10000 && income <= 30000)
tax = income * 0.15;
else
tax = income * 0.35;
printf("Tax: %f", tax);
return 0;
}
/* ---------------------------------------------------------------- */
/* Leap Years */
#include <stdio.h>
int main()
{
int year;
scanf("%d", &year);
if (year % 4 == 0)
{
if (year % 100 == 0)
{
if (year % 400 == 0)
{
printf("Leap year");
}
else
{
printf("Not leap year");
}
}
else
{
printf("Leap year");
}
}
else
{
printf("Not leap year");
}
return 0;
}
/* ---------------------------------------------------------------- */
/* Coffee Venting Machine
* Takes only €5 banknotes
* Coffee price: €0.70
* Takes number of coffees as input and calculates change
* and whether €5 are enough for the purchase.
*/
#include <stdio.h>
int main()
{
const int price = 70;
const int available = 500;
int coffees, total, change, total_change;
int c10, c20, c50, c1, c2 = 0;
printf("How many coffees would you like to order?\n");
scanf("%d", &coffees);
while (coffees < 0)
{
printf("Please enter a valid number:\n");
scanf("%d", &coffees);
}
total = price * coffees;
if (total > available)
{
printf("€5 are not enough for %d coffees.\n", coffees);
return 0;
}
change = available - total;
total_change = change;
while (change > 0)
{
if (change >= 400)
{
c2 += 2;
change -= 400;
}
if (change >= 200 && change < 400)
{
c2 += 1;
change -= 200;
}
if (change >= 100 && change < 200)
{
c1 += 1;
change -= 100;
}
if (change >= 50 && change < 100)
{
c50 += 1;
change -= 50;
}
if (change >= 20 && change < 50)
{
c20 += 1;
change -= 20;
}
if (change >= 10 && change < 20)
{
c10 += 1;
change -= 10;
}
}
printf("\n Price : %d", total / 100);
printf("\nChange :\n");
if (c2) printf(" €2 coins : %d\n", c2);
if (c1) printf(" €1 coins : %d\n", c1);
if (c50) printf("€0.50 coins : %d\n", c50);
if (c20) printf("€0.20 coins : %d\n", c20);
if (c10) printf("€0.10 coins : %d\n", c10);
printf("=======================\n");
printf(" Total : %d\n\n", total_change / 100);
return 0;
}
/* ---------------------------------------------------------------- */
/* Coffee Venting Machine */
/* Solution 2 */
#include <stdio.h>
int main()
{
const int price = 70;
const int available = 500;