├── learnc.png
├── README.md
├── LICENSE
└── examples.c
/learnc.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/michaelkolesidis/learnc/HEAD/learnc.png
--------------------------------------------------------------------------------
/README.md:
--------------------------------------------------------------------------------
1 |
2 |
3 |

4 |
5 |
Educational material and examples for those interested in learning the C programming language
6 |
7 |
8 |
9 |
10 |
11 | ## Files
12 |
13 | examples.c
14 | Various examples of programs written in C. Each examples has a small introduction. Code written by me from scratch. One single long file with thousands of lines. You might wanna try searching if you need something specific. Pull requests and issues for corrections are always welcome.
15 |
16 |
17 |
18 | ## License
19 |
20 |
21 |
22 | Copyright (c) 2022 Michael Kolesidis
23 | Licensed under the [GNU General Public License v3.0](https://www.gnu.org/licenses/gpl-3.0.html).
24 |
25 |
26 |
27 |
28 |
29 |
30 |
31 |
32 | [//]: # (Free Software)
33 |
34 |
35 |
36 |
37 |

38 |
39 |
40 |
41 |

42 |
43 |
--------------------------------------------------------------------------------
/LICENSE:
--------------------------------------------------------------------------------
1 | GNU GENERAL PUBLIC LICENSE
2 | Version 3, 29 June 2007
3 |
4 | Copyright (C) 2007 Free Software Foundation, Inc.
5 | Everyone is permitted to copy and distribute verbatim copies
6 | of this license document, but changing it is not allowed.
7 |
8 | Preamble
9 |
10 | The GNU General Public License is a free, copyleft license for
11 | software and other kinds of works.
12 |
13 | The licenses for most software and other practical works are designed
14 | to take away your freedom to share and change the works. By contrast,
15 | the GNU General Public License is intended to guarantee your freedom to
16 | share and change all versions of a program--to make sure it remains free
17 | software for all its users. We, the Free Software Foundation, use the
18 | GNU General Public License for most of our software; it applies also to
19 | any other work released this way by its authors. You can apply it to
20 | your programs, too.
21 |
22 | When we speak of free software, we are referring to freedom, not
23 | price. Our General Public Licenses are designed to make sure that you
24 | have the freedom to distribute copies of free software (and charge for
25 | them if you wish), that you receive source code or can get it if you
26 | want it, that you can change the software or use pieces of it in new
27 | free programs, and that you know you can do these things.
28 |
29 | To protect your rights, we need to prevent others from denying you
30 | these rights or asking you to surrender the rights. Therefore, you have
31 | certain responsibilities if you distribute copies of the software, or if
32 | you modify it: responsibilities to respect the freedom of others.
33 |
34 | For example, if you distribute copies of such a program, whether
35 | gratis or for a fee, you must pass on to the recipients the same
36 | freedoms that you received. You must make sure that they, too, receive
37 | or can get the source code. And you must show them these terms so they
38 | know their rights.
39 |
40 | Developers that use the GNU GPL protect your rights with two steps:
41 | (1) assert copyright on the software, and (2) offer you this License
42 | giving you legal permission to copy, distribute and/or modify it.
43 |
44 | For the developers' and authors' protection, the GPL clearly explains
45 | that there is no warranty for this free software. For both users' and
46 | authors' sake, the GPL requires that modified versions be marked as
47 | changed, so that their problems will not be attributed erroneously to
48 | authors of previous versions.
49 |
50 | Some devices are designed to deny users access to install or run
51 | modified versions of the software inside them, although the manufacturer
52 | can do so. This is fundamentally incompatible with the aim of
53 | protecting users' freedom to change the software. The systematic
54 | pattern of such abuse occurs in the area of products for individuals to
55 | use, which is precisely where it is most unacceptable. Therefore, we
56 | have designed this version of the GPL to prohibit the practice for those
57 | products. If such problems arise substantially in other domains, we
58 | stand ready to extend this provision to those domains in future versions
59 | of the GPL, as needed to protect the freedom of users.
60 |
61 | Finally, every program is threatened constantly by software patents.
62 | States should not allow patents to restrict development and use of
63 | software on general-purpose computers, but in those that do, we wish to
64 | avoid the special danger that patents applied to a free program could
65 | make it effectively proprietary. To prevent this, the GPL assures that
66 | patents cannot be used to render the program non-free.
67 |
68 | The precise terms and conditions for copying, distribution and
69 | modification follow.
70 |
71 | TERMS AND CONDITIONS
72 |
73 | 0. Definitions.
74 |
75 | "This License" refers to version 3 of the GNU General Public License.
76 |
77 | "Copyright" also means copyright-like laws that apply to other kinds of
78 | works, such as semiconductor masks.
79 |
80 | "The Program" refers to any copyrightable work licensed under this
81 | License. Each licensee is addressed as "you". "Licensees" and
82 | "recipients" may be individuals or organizations.
83 |
84 | To "modify" a work means to copy from or adapt all or part of the work
85 | in a fashion requiring copyright permission, other than the making of an
86 | exact copy. The resulting work is called a "modified version" of the
87 | earlier work or a work "based on" the earlier work.
88 |
89 | A "covered work" means either the unmodified Program or a work based
90 | on the Program.
91 |
92 | To "propagate" a work means to do anything with it that, without
93 | permission, would make you directly or secondarily liable for
94 | infringement under applicable copyright law, except executing it on a
95 | computer or modifying a private copy. Propagation includes copying,
96 | distribution (with or without modification), making available to the
97 | public, and in some countries other activities as well.
98 |
99 | To "convey" a work means any kind of propagation that enables other
100 | parties to make or receive copies. Mere interaction with a user through
101 | a computer network, with no transfer of a copy, is not conveying.
102 |
103 | An interactive user interface displays "Appropriate Legal Notices"
104 | to the extent that it includes a convenient and prominently visible
105 | feature that (1) displays an appropriate copyright notice, and (2)
106 | tells the user that there is no warranty for the work (except to the
107 | extent that warranties are provided), that licensees may convey the
108 | work under this License, and how to view a copy of this License. If
109 | the interface presents a list of user commands or options, such as a
110 | menu, a prominent item in the list meets this criterion.
111 |
112 | 1. Source Code.
113 |
114 | The "source code" for a work means the preferred form of the work
115 | for making modifications to it. "Object code" means any non-source
116 | form of a work.
117 |
118 | A "Standard Interface" means an interface that either is an official
119 | standard defined by a recognized standards body, or, in the case of
120 | interfaces specified for a particular programming language, one that
121 | is widely used among developers working in that language.
122 |
123 | The "System Libraries" of an executable work include anything, other
124 | than the work as a whole, that (a) is included in the normal form of
125 | packaging a Major Component, but which is not part of that Major
126 | Component, and (b) serves only to enable use of the work with that
127 | Major Component, or to implement a Standard Interface for which an
128 | implementation is available to the public in source code form. A
129 | "Major Component", in this context, means a major essential component
130 | (kernel, window system, and so on) of the specific operating system
131 | (if any) on which the executable work runs, or a compiler used to
132 | produce the work, or an object code interpreter used to run it.
133 |
134 | The "Corresponding Source" for a work in object code form means all
135 | the source code needed to generate, install, and (for an executable
136 | work) run the object code and to modify the work, including scripts to
137 | control those activities. However, it does not include the work's
138 | System Libraries, or general-purpose tools or generally available free
139 | programs which are used unmodified in performing those activities but
140 | which are not part of the work. For example, Corresponding Source
141 | includes interface definition files associated with source files for
142 | the work, and the source code for shared libraries and dynamically
143 | linked subprograms that the work is specifically designed to require,
144 | such as by intimate data communication or control flow between those
145 | subprograms and other parts of the work.
146 |
147 | The Corresponding Source need not include anything that users
148 | can regenerate automatically from other parts of the Corresponding
149 | Source.
150 |
151 | The Corresponding Source for a work in source code form is that
152 | same work.
153 |
154 | 2. Basic Permissions.
155 |
156 | All rights granted under this License are granted for the term of
157 | copyright on the Program, and are irrevocable provided the stated
158 | conditions are met. This License explicitly affirms your unlimited
159 | permission to run the unmodified Program. The output from running a
160 | covered work is covered by this License only if the output, given its
161 | content, constitutes a covered work. This License acknowledges your
162 | rights of fair use or other equivalent, as provided by copyright law.
163 |
164 | You may make, run and propagate covered works that you do not
165 | convey, without conditions so long as your license otherwise remains
166 | in force. You may convey covered works to others for the sole purpose
167 | of having them make modifications exclusively for you, or provide you
168 | with facilities for running those works, provided that you comply with
169 | the terms of this License in conveying all material for which you do
170 | not control copyright. Those thus making or running the covered works
171 | for you must do so exclusively on your behalf, under your direction
172 | and control, on terms that prohibit them from making any copies of
173 | your copyrighted material outside their relationship with you.
174 |
175 | Conveying under any other circumstances is permitted solely under
176 | the conditions stated below. Sublicensing is not allowed; section 10
177 | makes it unnecessary.
178 |
179 | 3. Protecting Users' Legal Rights From Anti-Circumvention Law.
180 |
181 | No covered work shall be deemed part of an effective technological
182 | measure under any applicable law fulfilling obligations under article
183 | 11 of the WIPO copyright treaty adopted on 20 December 1996, or
184 | similar laws prohibiting or restricting circumvention of such
185 | measures.
186 |
187 | When you convey a covered work, you waive any legal power to forbid
188 | circumvention of technological measures to the extent such circumvention
189 | is effected by exercising rights under this License with respect to
190 | the covered work, and you disclaim any intention to limit operation or
191 | modification of the work as a means of enforcing, against the work's
192 | users, your or third parties' legal rights to forbid circumvention of
193 | technological measures.
194 |
195 | 4. Conveying Verbatim Copies.
196 |
197 | You may convey verbatim copies of the Program's source code as you
198 | receive it, in any medium, provided that you conspicuously and
199 | appropriately publish on each copy an appropriate copyright notice;
200 | keep intact all notices stating that this License and any
201 | non-permissive terms added in accord with section 7 apply to the code;
202 | keep intact all notices of the absence of any warranty; and give all
203 | recipients a copy of this License along with the Program.
204 |
205 | You may charge any price or no price for each copy that you convey,
206 | and you may offer support or warranty protection for a fee.
207 |
208 | 5. Conveying Modified Source Versions.
209 |
210 | You may convey a work based on the Program, or the modifications to
211 | produce it from the Program, in the form of source code under the
212 | terms of section 4, provided that you also meet all of these conditions:
213 |
214 | a) The work must carry prominent notices stating that you modified
215 | it, and giving a relevant date.
216 |
217 | b) The work must carry prominent notices stating that it is
218 | released under this License and any conditions added under section
219 | 7. This requirement modifies the requirement in section 4 to
220 | "keep intact all notices".
221 |
222 | c) You must license the entire work, as a whole, under this
223 | License to anyone who comes into possession of a copy. This
224 | License will therefore apply, along with any applicable section 7
225 | additional terms, to the whole of the work, and all its parts,
226 | regardless of how they are packaged. This License gives no
227 | permission to license the work in any other way, but it does not
228 | invalidate such permission if you have separately received it.
229 |
230 | d) If the work has interactive user interfaces, each must display
231 | Appropriate Legal Notices; however, if the Program has interactive
232 | interfaces that do not display Appropriate Legal Notices, your
233 | work need not make them do so.
234 |
235 | A compilation of a covered work with other separate and independent
236 | works, which are not by their nature extensions of the covered work,
237 | and which are not combined with it such as to form a larger program,
238 | in or on a volume of a storage or distribution medium, is called an
239 | "aggregate" if the compilation and its resulting copyright are not
240 | used to limit the access or legal rights of the compilation's users
241 | beyond what the individual works permit. Inclusion of a covered work
242 | in an aggregate does not cause this License to apply to the other
243 | parts of the aggregate.
244 |
245 | 6. Conveying Non-Source Forms.
246 |
247 | You may convey a covered work in object code form under the terms
248 | of sections 4 and 5, provided that you also convey the
249 | machine-readable Corresponding Source under the terms of this License,
250 | in one of these ways:
251 |
252 | a) Convey the object code in, or embodied in, a physical product
253 | (including a physical distribution medium), accompanied by the
254 | Corresponding Source fixed on a durable physical medium
255 | customarily used for software interchange.
256 |
257 | b) Convey the object code in, or embodied in, a physical product
258 | (including a physical distribution medium), accompanied by a
259 | written offer, valid for at least three years and valid for as
260 | long as you offer spare parts or customer support for that product
261 | model, to give anyone who possesses the object code either (1) a
262 | copy of the Corresponding Source for all the software in the
263 | product that is covered by this License, on a durable physical
264 | medium customarily used for software interchange, for a price no
265 | more than your reasonable cost of physically performing this
266 | conveying of source, or (2) access to copy the
267 | Corresponding Source from a network server at no charge.
268 |
269 | c) Convey individual copies of the object code with a copy of the
270 | written offer to provide the Corresponding Source. This
271 | alternative is allowed only occasionally and noncommercially, and
272 | only if you received the object code with such an offer, in accord
273 | with subsection 6b.
274 |
275 | d) Convey the object code by offering access from a designated
276 | place (gratis or for a charge), and offer equivalent access to the
277 | Corresponding Source in the same way through the same place at no
278 | further charge. You need not require recipients to copy the
279 | Corresponding Source along with the object code. If the place to
280 | copy the object code is a network server, the Corresponding Source
281 | may be on a different server (operated by you or a third party)
282 | that supports equivalent copying facilities, provided you maintain
283 | clear directions next to the object code saying where to find the
284 | Corresponding Source. Regardless of what server hosts the
285 | Corresponding Source, you remain obligated to ensure that it is
286 | available for as long as needed to satisfy these requirements.
287 |
288 | e) Convey the object code using peer-to-peer transmission, provided
289 | you inform other peers where the object code and Corresponding
290 | Source of the work are being offered to the general public at no
291 | charge under subsection 6d.
292 |
293 | A separable portion of the object code, whose source code is excluded
294 | from the Corresponding Source as a System Library, need not be
295 | included in conveying the object code work.
296 |
297 | A "User Product" is either (1) a "consumer product", which means any
298 | tangible personal property which is normally used for personal, family,
299 | or household purposes, or (2) anything designed or sold for incorporation
300 | into a dwelling. In determining whether a product is a consumer product,
301 | doubtful cases shall be resolved in favor of coverage. For a particular
302 | product received by a particular user, "normally used" refers to a
303 | typical or common use of that class of product, regardless of the status
304 | of the particular user or of the way in which the particular user
305 | actually uses, or expects or is expected to use, the product. A product
306 | is a consumer product regardless of whether the product has substantial
307 | commercial, industrial or non-consumer uses, unless such uses represent
308 | the only significant mode of use of the product.
309 |
310 | "Installation Information" for a User Product means any methods,
311 | procedures, authorization keys, or other information required to install
312 | and execute modified versions of a covered work in that User Product from
313 | a modified version of its Corresponding Source. The information must
314 | suffice to ensure that the continued functioning of the modified object
315 | code is in no case prevented or interfered with solely because
316 | modification has been made.
317 |
318 | If you convey an object code work under this section in, or with, or
319 | specifically for use in, a User Product, and the conveying occurs as
320 | part of a transaction in which the right of possession and use of the
321 | User Product is transferred to the recipient in perpetuity or for a
322 | fixed term (regardless of how the transaction is characterized), the
323 | Corresponding Source conveyed under this section must be accompanied
324 | by the Installation Information. But this requirement does not apply
325 | if neither you nor any third party retains the ability to install
326 | modified object code on the User Product (for example, the work has
327 | been installed in ROM).
328 |
329 | The requirement to provide Installation Information does not include a
330 | requirement to continue to provide support service, warranty, or updates
331 | for a work that has been modified or installed by the recipient, or for
332 | the User Product in which it has been modified or installed. Access to a
333 | network may be denied when the modification itself materially and
334 | adversely affects the operation of the network or violates the rules and
335 | protocols for communication across the network.
336 |
337 | Corresponding Source conveyed, and Installation Information provided,
338 | in accord with this section must be in a format that is publicly
339 | documented (and with an implementation available to the public in
340 | source code form), and must require no special password or key for
341 | unpacking, reading or copying.
342 |
343 | 7. Additional Terms.
344 |
345 | "Additional permissions" are terms that supplement the terms of this
346 | License by making exceptions from one or more of its conditions.
347 | Additional permissions that are applicable to the entire Program shall
348 | be treated as though they were included in this License, to the extent
349 | that they are valid under applicable law. If additional permissions
350 | apply only to part of the Program, that part may be used separately
351 | under those permissions, but the entire Program remains governed by
352 | this License without regard to the additional permissions.
353 |
354 | When you convey a copy of a covered work, you may at your option
355 | remove any additional permissions from that copy, or from any part of
356 | it. (Additional permissions may be written to require their own
357 | removal in certain cases when you modify the work.) You may place
358 | additional permissions on material, added by you to a covered work,
359 | for which you have or can give appropriate copyright permission.
360 |
361 | Notwithstanding any other provision of this License, for material you
362 | add to a covered work, you may (if authorized by the copyright holders of
363 | that material) supplement the terms of this License with terms:
364 |
365 | a) Disclaiming warranty or limiting liability differently from the
366 | terms of sections 15 and 16 of this License; or
367 |
368 | b) Requiring preservation of specified reasonable legal notices or
369 | author attributions in that material or in the Appropriate Legal
370 | Notices displayed by works containing it; or
371 |
372 | c) Prohibiting misrepresentation of the origin of that material, or
373 | requiring that modified versions of such material be marked in
374 | reasonable ways as different from the original version; or
375 |
376 | d) Limiting the use for publicity purposes of names of licensors or
377 | authors of the material; or
378 |
379 | e) Declining to grant rights under trademark law for use of some
380 | trade names, trademarks, or service marks; or
381 |
382 | f) Requiring indemnification of licensors and authors of that
383 | material by anyone who conveys the material (or modified versions of
384 | it) with contractual assumptions of liability to the recipient, for
385 | any liability that these contractual assumptions directly impose on
386 | those licensors and authors.
387 |
388 | All other non-permissive additional terms are considered "further
389 | restrictions" within the meaning of section 10. If the Program as you
390 | received it, or any part of it, contains a notice stating that it is
391 | governed by this License along with a term that is a further
392 | restriction, you may remove that term. If a license document contains
393 | a further restriction but permits relicensing or conveying under this
394 | License, you may add to a covered work material governed by the terms
395 | of that license document, provided that the further restriction does
396 | not survive such relicensing or conveying.
397 |
398 | If you add terms to a covered work in accord with this section, you
399 | must place, in the relevant source files, a statement of the
400 | additional terms that apply to those files, or a notice indicating
401 | where to find the applicable terms.
402 |
403 | Additional terms, permissive or non-permissive, may be stated in the
404 | form of a separately written license, or stated as exceptions;
405 | the above requirements apply either way.
406 |
407 | 8. Termination.
408 |
409 | You may not propagate or modify a covered work except as expressly
410 | provided under this License. Any attempt otherwise to propagate or
411 | modify it is void, and will automatically terminate your rights under
412 | this License (including any patent licenses granted under the third
413 | paragraph of section 11).
414 |
415 | However, if you cease all violation of this License, then your
416 | license from a particular copyright holder is reinstated (a)
417 | provisionally, unless and until the copyright holder explicitly and
418 | finally terminates your license, and (b) permanently, if the copyright
419 | holder fails to notify you of the violation by some reasonable means
420 | prior to 60 days after the cessation.
421 |
422 | Moreover, your license from a particular copyright holder is
423 | reinstated permanently if the copyright holder notifies you of the
424 | violation by some reasonable means, this is the first time you have
425 | received notice of violation of this License (for any work) from that
426 | copyright holder, and you cure the violation prior to 30 days after
427 | your receipt of the notice.
428 |
429 | Termination of your rights under this section does not terminate the
430 | licenses of parties who have received copies or rights from you under
431 | this License. If your rights have been terminated and not permanently
432 | reinstated, you do not qualify to receive new licenses for the same
433 | material under section 10.
434 |
435 | 9. Acceptance Not Required for Having Copies.
436 |
437 | You are not required to accept this License in order to receive or
438 | run a copy of the Program. Ancillary propagation of a covered work
439 | occurring solely as a consequence of using peer-to-peer transmission
440 | to receive a copy likewise does not require acceptance. However,
441 | nothing other than this License grants you permission to propagate or
442 | modify any covered work. These actions infringe copyright if you do
443 | not accept this License. Therefore, by modifying or propagating a
444 | covered work, you indicate your acceptance of this License to do so.
445 |
446 | 10. Automatic Licensing of Downstream Recipients.
447 |
448 | Each time you convey a covered work, the recipient automatically
449 | receives a license from the original licensors, to run, modify and
450 | propagate that work, subject to this License. You are not responsible
451 | for enforcing compliance by third parties with this License.
452 |
453 | An "entity transaction" is a transaction transferring control of an
454 | organization, or substantially all assets of one, or subdividing an
455 | organization, or merging organizations. If propagation of a covered
456 | work results from an entity transaction, each party to that
457 | transaction who receives a copy of the work also receives whatever
458 | licenses to the work the party's predecessor in interest had or could
459 | give under the previous paragraph, plus a right to possession of the
460 | Corresponding Source of the work from the predecessor in interest, if
461 | the predecessor has it or can get it with reasonable efforts.
462 |
463 | You may not impose any further restrictions on the exercise of the
464 | rights granted or affirmed under this License. For example, you may
465 | not impose a license fee, royalty, or other charge for exercise of
466 | rights granted under this License, and you may not initiate litigation
467 | (including a cross-claim or counterclaim in a lawsuit) alleging that
468 | any patent claim is infringed by making, using, selling, offering for
469 | sale, or importing the Program or any portion of it.
470 |
471 | 11. Patents.
472 |
473 | A "contributor" is a copyright holder who authorizes use under this
474 | License of the Program or a work on which the Program is based. The
475 | work thus licensed is called the contributor's "contributor version".
476 |
477 | A contributor's "essential patent claims" are all patent claims
478 | owned or controlled by the contributor, whether already acquired or
479 | hereafter acquired, that would be infringed by some manner, permitted
480 | by this License, of making, using, or selling its contributor version,
481 | but do not include claims that would be infringed only as a
482 | consequence of further modification of the contributor version. For
483 | purposes of this definition, "control" includes the right to grant
484 | patent sublicenses in a manner consistent with the requirements of
485 | this License.
486 |
487 | Each contributor grants you a non-exclusive, worldwide, royalty-free
488 | patent license under the contributor's essential patent claims, to
489 | make, use, sell, offer for sale, import and otherwise run, modify and
490 | propagate the contents of its contributor version.
491 |
492 | In the following three paragraphs, a "patent license" is any express
493 | agreement or commitment, however denominated, not to enforce a patent
494 | (such as an express permission to practice a patent or covenant not to
495 | sue for patent infringement). To "grant" such a patent license to a
496 | party means to make such an agreement or commitment not to enforce a
497 | patent against the party.
498 |
499 | If you convey a covered work, knowingly relying on a patent license,
500 | and the Corresponding Source of the work is not available for anyone
501 | to copy, free of charge and under the terms of this License, through a
502 | publicly available network server or other readily accessible means,
503 | then you must either (1) cause the Corresponding Source to be so
504 | available, or (2) arrange to deprive yourself of the benefit of the
505 | patent license for this particular work, or (3) arrange, in a manner
506 | consistent with the requirements of this License, to extend the patent
507 | license to downstream recipients. "Knowingly relying" means you have
508 | actual knowledge that, but for the patent license, your conveying the
509 | covered work in a country, or your recipient's use of the covered work
510 | in a country, would infringe one or more identifiable patents in that
511 | country that you have reason to believe are valid.
512 |
513 | If, pursuant to or in connection with a single transaction or
514 | arrangement, you convey, or propagate by procuring conveyance of, a
515 | covered work, and grant a patent license to some of the parties
516 | receiving the covered work authorizing them to use, propagate, modify
517 | or convey a specific copy of the covered work, then the patent license
518 | you grant is automatically extended to all recipients of the covered
519 | work and works based on it.
520 |
521 | A patent license is "discriminatory" if it does not include within
522 | the scope of its coverage, prohibits the exercise of, or is
523 | conditioned on the non-exercise of one or more of the rights that are
524 | specifically granted under this License. You may not convey a covered
525 | work if you are a party to an arrangement with a third party that is
526 | in the business of distributing software, under which you make payment
527 | to the third party based on the extent of your activity of conveying
528 | the work, and under which the third party grants, to any of the
529 | parties who would receive the covered work from you, a discriminatory
530 | patent license (a) in connection with copies of the covered work
531 | conveyed by you (or copies made from those copies), or (b) primarily
532 | for and in connection with specific products or compilations that
533 | contain the covered work, unless you entered into that arrangement,
534 | or that patent license was granted, prior to 28 March 2007.
535 |
536 | Nothing in this License shall be construed as excluding or limiting
537 | any implied license or other defenses to infringement that may
538 | otherwise be available to you under applicable patent law.
539 |
540 | 12. No Surrender of Others' Freedom.
541 |
542 | If conditions are imposed on you (whether by court order, agreement or
543 | otherwise) that contradict the conditions of this License, they do not
544 | excuse you from the conditions of this License. If you cannot convey a
545 | covered work so as to satisfy simultaneously your obligations under this
546 | License and any other pertinent obligations, then as a consequence you may
547 | not convey it at all. For example, if you agree to terms that obligate you
548 | to collect a royalty for further conveying from those to whom you convey
549 | the Program, the only way you could satisfy both those terms and this
550 | License would be to refrain entirely from conveying the Program.
551 |
552 | 13. Use with the GNU Affero General Public License.
553 |
554 | Notwithstanding any other provision of this License, you have
555 | permission to link or combine any covered work with a work licensed
556 | under version 3 of the GNU Affero General Public License into a single
557 | combined work, and to convey the resulting work. The terms of this
558 | License will continue to apply to the part which is the covered work,
559 | but the special requirements of the GNU Affero General Public License,
560 | section 13, concerning interaction through a network will apply to the
561 | combination as such.
562 |
563 | 14. Revised Versions of this License.
564 |
565 | The Free Software Foundation may publish revised and/or new versions of
566 | the GNU General Public License from time to time. Such new versions will
567 | be similar in spirit to the present version, but may differ in detail to
568 | address new problems or concerns.
569 |
570 | Each version is given a distinguishing version number. If the
571 | Program specifies that a certain numbered version of the GNU General
572 | Public License "or any later version" applies to it, you have the
573 | option of following the terms and conditions either of that numbered
574 | version or of any later version published by the Free Software
575 | Foundation. If the Program does not specify a version number of the
576 | GNU General Public License, you may choose any version ever published
577 | by the Free Software Foundation.
578 |
579 | If the Program specifies that a proxy can decide which future
580 | versions of the GNU General Public License can be used, that proxy's
581 | public statement of acceptance of a version permanently authorizes you
582 | to choose that version for the Program.
583 |
584 | Later license versions may give you additional or different
585 | permissions. However, no additional obligations are imposed on any
586 | author or copyright holder as a result of your choosing to follow a
587 | later version.
588 |
589 | 15. Disclaimer of Warranty.
590 |
591 | THERE IS NO WARRANTY FOR THE PROGRAM, TO THE EXTENT PERMITTED BY
592 | APPLICABLE LAW. EXCEPT WHEN OTHERWISE STATED IN WRITING THE COPYRIGHT
593 | HOLDERS AND/OR OTHER PARTIES PROVIDE THE PROGRAM "AS IS" WITHOUT WARRANTY
594 | OF ANY KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING, BUT NOT LIMITED TO,
595 | THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
596 | PURPOSE. THE ENTIRE RISK AS TO THE QUALITY AND PERFORMANCE OF THE PROGRAM
597 | IS WITH YOU. SHOULD THE PROGRAM PROVE DEFECTIVE, YOU ASSUME THE COST OF
598 | ALL NECESSARY SERVICING, REPAIR OR CORRECTION.
599 |
600 | 16. Limitation of Liability.
601 |
602 | IN NO EVENT UNLESS REQUIRED BY APPLICABLE LAW OR AGREED TO IN WRITING
603 | WILL ANY COPYRIGHT HOLDER, OR ANY OTHER PARTY WHO MODIFIES AND/OR CONVEYS
604 | THE PROGRAM AS PERMITTED ABOVE, BE LIABLE TO YOU FOR DAMAGES, INCLUDING ANY
605 | GENERAL, SPECIAL, INCIDENTAL OR CONSEQUENTIAL DAMAGES ARISING OUT OF THE
606 | USE OR INABILITY TO USE THE PROGRAM (INCLUDING BUT NOT LIMITED TO LOSS OF
607 | DATA OR DATA BEING RENDERED INACCURATE OR LOSSES SUSTAINED BY YOU OR THIRD
608 | PARTIES OR A FAILURE OF THE PROGRAM TO OPERATE WITH ANY OTHER PROGRAMS),
609 | EVEN IF SUCH HOLDER OR OTHER PARTY HAS BEEN ADVISED OF THE POSSIBILITY OF
610 | SUCH DAMAGES.
611 |
612 | 17. Interpretation of Sections 15 and 16.
613 |
614 | If the disclaimer of warranty and limitation of liability provided
615 | above cannot be given local legal effect according to their terms,
616 | reviewing courts shall apply local law that most closely approximates
617 | an absolute waiver of all civil liability in connection with the
618 | Program, unless a warranty or assumption of liability accompanies a
619 | copy of the Program in return for a fee.
620 |
621 | END OF TERMS AND CONDITIONS
622 |
623 | How to Apply These Terms to Your New Programs
624 |
625 | If you develop a new program, and you want it to be of the greatest
626 | possible use to the public, the best way to achieve this is to make it
627 | free software which everyone can redistribute and change under these terms.
628 |
629 | To do so, attach the following notices to the program. It is safest
630 | to attach them to the start of each source file to most effectively
631 | state the exclusion of warranty; and each file should have at least
632 | the "copyright" line and a pointer to where the full notice is found.
633 |
634 |
635 | Copyright (C)
636 |
637 | This program is free software: you can redistribute it and/or modify
638 | it under the terms of the GNU General Public License as published by
639 | the Free Software Foundation, either version 3 of the License, or
640 | (at your option) any later version.
641 |
642 | This program is distributed in the hope that it will be useful,
643 | but WITHOUT ANY WARRANTY; without even the implied warranty of
644 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
645 | GNU General Public License for more details.
646 |
647 | You should have received a copy of the GNU General Public License
648 | along with this program. If not, see .
649 |
650 | Also add information on how to contact you by electronic and paper mail.
651 |
652 | If the program does terminal interaction, make it output a short
653 | notice like this when it starts in an interactive mode:
654 |
655 | Copyright (C)
656 | This program comes with ABSOLUTELY NO WARRANTY; for details type `show w'.
657 | This is free software, and you are welcome to redistribute it
658 | under certain conditions; type `show c' for details.
659 |
660 | The hypothetical commands `show w' and `show c' should show the appropriate
661 | parts of the General Public License. Of course, your program's commands
662 | might be different; for a GUI interface, you would use an "about box".
663 |
664 | You should also get your employer (if you work as a programmer) or school,
665 | if any, to sign a "copyright disclaimer" for the program, if necessary.
666 | For more information on this, and how to apply and follow the GNU GPL, see
667 | .
668 |
669 | The GNU General Public License does not permit incorporating your program
670 | into proprietary programs. If your program is a subroutine library, you
671 | may consider it more useful to permit linking proprietary applications with
672 | the library. If this is what you want to do, use the GNU Lesser General
673 | Public License instead of this License. But first, please read
674 | .
675 |
--------------------------------------------------------------------------------
/examples.c:
--------------------------------------------------------------------------------
1 | /* The simple hello world program */
2 |
3 | #include
4 | #include
5 |
6 | int main() {
7 | printf("Hello, World!");
8 | return 0;
9 | }
10 |
11 | /* ---------------------------------------------------------------- */
12 |
13 | /* Hello world using string */
14 |
15 | #include
16 | #include
17 |
18 | int main() {
19 | char hello[13] = "Hello, World!";
20 | printf("%s", hello);
21 | return 0;
22 | }
23 |
24 | /* ---------------------------------------------------------------- */
25 |
26 | /* Sum of two numbers */
27 |
28 | #include
29 | #include
30 |
31 | int main()
32 | {
33 | int a;
34 | int b;
35 | int sum;
36 | printf("Enter two numbers to get the sum (exit with input 0)\n");
37 | scanf("%d %d", &a, &b);
38 | while (a != 0 && b != 0)
39 | {
40 | sum = a + b;
41 | printf("The sum of %d and %d is %d\n\n", a, b, sum);
42 | printf("Enter two numbers to get the sum\n");
43 | scanf("%d %d", &a, &b);
44 | }
45 | return 0;
46 | }
47 |
48 | /* ---------------------------------------------------------------- */
49 |
50 | /* Convert float to integer */
51 |
52 | #include
53 | #include
54 |
55 | int main() {
56 | float a = 10.987;
57 | int b = (int) a;
58 | printf("%d", b);
59 | return 0;
60 | }
61 |
62 | /* ---------------------------------------------------------------- */
63 |
64 | /* Experiment with scanf return values */
65 |
66 | #include
67 | #include
68 |
69 | int main()
70 | {
71 | int a;
72 | int b;
73 | if (scanf("%d %d", &a, &b) != 2)
74 | {
75 | printf("1");
76 | }
77 | else
78 | {
79 | printf("0");
80 | }
81 |
82 | return 0;
83 | }
84 |
85 | /* ---------------------------------------------------------------- */
86 |
87 | /* Simplest calculator, checks for operator input but not type of numerical input */
88 |
89 | #include
90 | #include
91 |
92 | int main()
93 | {
94 | float a;
95 | float b;
96 | char operator;
97 | float output;
98 | int error = 0;
99 |
100 | printf("Enter two numbers followed by the operator + ,- ,* or / (exit with input 0)\n");
101 | scanf("%f %f %c", &a, &b, &operator);
102 |
103 |
104 | while (a != 0 && b != 0)
105 | {
106 | if (operator== '+')
107 | {
108 | output = a + b;
109 | }
110 | else if (operator== '-')
111 | {
112 | output = a - b;
113 | }
114 | else if (operator== '*')
115 | {
116 | output = a * b;
117 | }
118 | else if (operator== '/')
119 | {
120 | output = a / b;
121 | }
122 | else
123 | {
124 | printf("Please use one of the four operators.\n\n");
125 | error = 1;
126 | }
127 |
128 | if (error == 0)
129 | {
130 | printf("%f%c%f = %f\n\n", a, operator, b, output);
131 | }
132 | printf("Enter two numbers followed by the operator + ,- ,* or / (exit with input 0)\n");
133 | scanf("%f %f %c", &a, &b, &operator);
134 | error = 0;
135 | }
136 | return 0;
137 | }
138 |
139 | /* ---------------------------------------------------------------- */
140 |
141 | /* Average of four numbers, exits if one of the numbers is 0 */
142 |
143 | #include
144 | #include
145 |
146 | int main()
147 | {
148 | float a;
149 | float b;
150 | float c;
151 | float d;
152 | float average;
153 |
154 | printf("Enter four numbers to get their average (exit with input 0)\n");
155 | scanf("%f %f %f %f", &a, &b, &c, &d);
156 |
157 | while (a != 0 && b != 0 && c != 0 && d != 0)
158 | {
159 | average = (a + b +c + d) / 4;
160 | printf("The average is %f.\n\n", average);
161 | printf("Enter four numbers to get their average (exit with input 0)\n");
162 | scanf("%f %f %f %f", &a, &b, &c, &d);
163 | }
164 | return 0;
165 | }
166 |
167 | /* ---------------------------------------------------------------- */
168 |
169 | /* Average of four numbers, exits if all of the numbers are 0 */
170 |
171 | #include
172 | #include
173 |
174 | int main()
175 | {
176 | float a;
177 | float b;
178 | float c;
179 | float d;
180 | float average;
181 | printf("Enter four numbers to get their average (exit with input 0)\n");
182 | scanf("%f %f %f %f", &a, &b, &c, &d);
183 | if (a+b+c+d)
184 | {
185 | average = (a + b +c + d) / 4;
186 | printf("The average is %f.\n\n", average);
187 | printf("Enter four numbers to get their average (exit with input 0)\n");
188 | scanf("%f %f %f %f", &a, &b, &c, &d);
189 | }
190 | return 0;
191 | }
192 |
193 | /* ---------------------------------------------------------------- */
194 |
195 | /* Even or odd, method 1 */
196 |
197 | #include
198 | #include
199 |
200 | int main()
201 | {
202 | int number, half;
203 | printf("Enter a number to check if it is even or odd\n");
204 | scanf("%d", &number);
205 | half = number / 2;
206 |
207 | if (half * 2 == number)
208 | printf("%d is even.\n", number);
209 | else
210 | printf("%d is odd.\n", number);
211 |
212 | return 0;
213 | }
214 |
215 | /* ---------------------------------------------------------------- */
216 |
217 | /* Even or odd, method 2 */
218 |
219 | #include
220 | #include
221 |
222 | int main()
223 | {
224 | int number;
225 | printf("Enter a number to check if it is even or odd\n");
226 | scanf("%d", &number);
227 |
228 | if (number % 2 == 0)
229 | printf("%d is even.\n", number);
230 | else
231 | printf("%d is odd.\n", number);
232 |
233 | return 0;
234 | }
235 |
236 | /* ---------------------------------------------------------------- */
237 |
238 | /* Equality test for floating point numbers */
239 |
240 | #include
241 | #include
242 |
243 | int main()
244 | {
245 | double tolerance = 0.00000001;
246 | int boolean;
247 | float a = 0.1 + 0.2;
248 | float b = 0.3;
249 | boolean = (fabs (a - b) < tolerance);
250 | printf("%d", boolean);
251 | return 0
252 | }
253 |
254 | /* ---------------------------------------------------------------- */
255 |
256 | /* Ask seperately for three numbers and print their sum, product and average */
257 |
258 | #include
259 | #include
260 |
261 | int main()
262 | {
263 | int a, b, c, sum, product, average;
264 | printf("Enter first number:\n");
265 | scanf("%d", &a);
266 | printf("Enter second number:\n");
267 | scanf("%d", &b);
268 | printf("Enter third number:\n");
269 | scanf("%d", &c);
270 | sum = a + b + c;
271 | product = a * b * c;
272 | average = (a + b +c) / 3;
273 | printf("The sum of %d, %d and %d is %d\n", a, b, c, sum);
274 | printf("The product of %d, %d and %d is %d\n", a, b, c, product);
275 | printf("The average of %d, %d and %d is %d\n", a, b, c, average);
276 | }
277 |
278 | /* ---------------------------------------------------------------- */
279 |
280 | /* Maximum of three numbers */
281 |
282 | #include
283 | #include
284 |
285 | int main()
286 | {
287 | int a, b, c, max;
288 | printf("Enter three numbers to find the maximum:\n");
289 | scanf("%d %d %d", &a, &b, &c);
290 |
291 | max = a;
292 |
293 | if (b > max)
294 | max = b;
295 | if (c > max)
296 | max = c;
297 | printf("The maximum is %d", max);
298 | return 0;
299 | }
300 |
301 | /* ---------------------------------------------------------------- */
302 |
303 | /* Circle length and circumference */
304 |
305 | #include
306 | #include
307 | #define PI 3.141592
308 |
309 | int main()
310 | {
311 | float r, circum, area;
312 | printf("Enter circle radius:\n");
313 | scanf("%f", &r);
314 | circum = 2 * PI * r;
315 | area = PI * r * r;
316 |
317 | printf("Circumference = %f\nArea = %f", circum, area);
318 | return 0;
319 | }
320 |
321 | /* ---------------------------------------------------------------- */
322 |
323 | /* VAT Price Calculator */
324 |
325 | #include
326 | #include
327 |
328 | int main()
329 | {
330 | float price, price_vat, vat;
331 |
332 | printf("Enter price without VAT and VAT percentage:\n");
333 | scanf("%f %f", &price, &vat);
334 |
335 | price_vat = price + (price * vat / 100);
336 |
337 | printf("Price without VAT is: %f\nVAT is: %f %%\nPrice with VAT is: %f\n", price, vat, price_vat);
338 | return 0;
339 | }
340 |
341 | /* ---------------------------------------------------------------- */
342 |
343 | /* Cost of stamps */
344 |
345 | #include
346 | #include
347 |
348 | int main()
349 | {
350 | char location;
351 | const float price_i = 0.70, price_n = 0.23, price_l = 0.15;
352 | float price, total;
353 | int num;
354 | printf("Will you be sending your letter (L)ocally, (N)ationally or (I)nternationally?\n");
355 | scanf("%c", &location);
356 | printf("How many letters will you be sending?\n");
357 | scanf("%d", &num);
358 |
359 | switch (location)
360 | {
361 | case 'L':
362 | price = price_l;
363 | case 'N':
364 | price = price_n;
365 | case 'I':
366 | price = price_i;
367 | }
368 |
369 | total = price * num;
370 |
371 | printf("Sending %d letters, will cost you $%f\n", num, total);
372 |
373 |
374 | return 0;
375 | }
376 |
377 | /* ---------------------------------------------------------------- */
378 |
379 | /* BMI Caclulator */
380 |
381 | #include
382 | #include
383 |
384 | int main()
385 | {
386 | float height, weight, bmi;
387 | printf("Enter your height:\n");
388 | scanf("%f", &height);
389 | printf("Enter your weight:\n");
390 | scanf("%f", &weight);
391 |
392 | bmi = weight / ( height * height);
393 |
394 | printf("You weight %f kilos, you are %f meters tall and your BMI is %f", weight, height, bmi);
395 |
396 | return 0;
397 | }
398 |
399 | /* ---------------------------------------------------------------- */
400 |
401 | /* Cheap or expensive gasoline */
402 |
403 | #include
404 | #include
405 |
406 | int main()
407 | {
408 | float liters, price_total, price_per_liter;
409 | printf("Enter how much you paid for gasoline:\n");
410 | scanf("%f", &price_total);
411 | printf("Enter liters:\n");
412 | scanf("%f", &liters);
413 |
414 | price_per_liter = price_total / liters;
415 |
416 | if (price_per_liter > 1.3)
417 | printf("Expensive gasoline\n");
418 | else
419 | printf("Cheap gasoline");
420 |
421 | return 0;
422 | }
423 |
424 | /* ---------------------------------------------------------------- */
425 |
426 | /* Swap values of two variables */
427 |
428 | #include
429 | #include
430 |
431 | int main()
432 | {
433 | int a, b, temp;
434 |
435 | printf("Enter value of a:\n");
436 | scanf("%d", &a);
437 | printf("Enter value of b:\n");
438 | scanf("%d", &b);
439 |
440 | temp = a;
441 | a = b;
442 | b = temp;
443 |
444 | printf("a = %d\nb = %d\n", a, b);
445 |
446 | return 0;
447 | }
448 |
449 | /* ---------------------------------------------------------------- */
450 |
451 | /* Calculate the sum of the first billion integers */
452 |
453 | #include
454 |
455 | int main() {
456 | long long int sum = 0;
457 | for (int i = 1; i < 1000000000; i++)
458 | {
459 | sum += i;
460 | printf("%lli\n", sum);
461 | }
462 | return 0;
463 | }
464 |
465 | /* ---------------------------------------------------------------- */
466 |
467 | /* Dice Roller */
468 |
469 | #include
470 | #include
471 | #include
472 |
473 | int main()
474 | {
475 | char roll;
476 | int dice1, dice2;
477 | srand(time(NULL));
478 | printf("Roll dice? (y/n)\n");
479 | scanf("%c", &roll);
480 |
481 | while (roll != 'n')
482 | {
483 | if (roll == 'y')
484 | {
485 | dice1 = rand() % 6 + 1;
486 | dice2 = rand() % 6 + 1;
487 | printf("You rolled %d and %d\n\n", dice1, dice2);
488 | printf("Roll dice? (y/n)\n");
489 | scanf("%c", &roll);
490 | }
491 | else
492 | {
493 | scanf("%c", &roll);
494 | }
495 | }
496 | }
497 |
498 | /* ---------------------------------------------------------------- */
499 |
500 | /* Euclidean Algorithm computing the greatest commin divisor of two integers */
501 |
502 | #include
503 |
504 | int gcd(long long int a, long long int b)
505 | {
506 | long long int t;
507 | while (b != 0)
508 | {
509 | t = b;
510 | b = a % b;
511 | a = t;
512 | }
513 | if (a < 0) a *= -1;
514 |
515 | printf("The greatest common divisor is: %lli\n\n", a);
516 | return 0;
517 | }
518 |
519 | int main()
520 | {
521 | long long int num1, num2;
522 | printf("Enter two integers to calculate the greatest common divisor:\n");
523 | scanf("%lli %lli", &num1, &num2);
524 | gcd(num1, num2);
525 | return 0;
526 | }
527 |
528 | /* ---------------------------------------------------------------- */
529 |
530 | /*
531 | * A digital building alarm system returns an 1-byte integer as a value,
532 | * whose bits transmit the following data:
533 | *
534 | * Bits 7, 6, 5 and 4 specify the checkpoint (16 possible combinations from 0-15
535 | * Bits 3 and 2 the type of emergency (0-None, 1-Fire, 2-Break-in, 3-Smoke)
536 | * Bit 1 show the system state (1-Working, 0-Failure)
537 | * Bit 0 is not used.
538 | *
539 | * Write a program that takes the alarm system output as input and displays
540 | * the checkpoint and the emergency type. In case of a system failure, the
541 | * output should be "System Failure".
542 | */
543 |
544 | #include
545 | #include
546 |
547 | int main()
548 | {
549 | int input, checkpoint, emergency, state;
550 |
551 | printf("Enter alarm system output:\n");
552 | scanf("%d", &input);
553 |
554 | checkpoint = (input & 240) >> 4;
555 | emergency = (input & 12) >> 2;
556 | state = (input & 2) >> 1;
557 |
558 | if (state == 0) {
559 | printf("System Failure\n");
560 | return 1;
561 | }
562 |
563 | printf("Checkpoint: %d\n", checkpoint + 1);
564 | if (emergency == 0) printf ("Emergency: None\n");
565 | if (emergency == 1) printf ("Emergency: Fire\n");
566 | if (emergency == 2) printf ("Emergency: Break-in\n");
567 | if (emergency == 3) printf ("Emergency: Smoke\n");
568 |
569 | return 0;
570 | }
571 |
572 | /* ---------------------------------------------------------------- */
573 |
574 | /* Convert seconds to hours, minutes and seconds */
575 |
576 | #include
577 | #include
578 |
579 | int main() {
580 | int sec, min, h;
581 | printf("Enter time in seconds:\n");
582 | scanf("%d", &sec);
583 |
584 | h = sec / 3600;
585 | min = sec % 3600 / 60;
586 | sec = sec % 3600 % 60;
587 | printf("%d hours, %d minutes and %d seconds\n", h, min, sec);
588 | return 0;
589 | }
590 |
591 | /* ---------------------------------------------------------------- */
592 |
593 | /* Average of marks from three modules and evaluation - max: 100 pass > 50 */
594 |
595 | #include
596 | #include
597 |
598 | int main() {
599 | int mark1, mark2, mark3, average;
600 |
601 | printf("Enter marks in three modules:\n");
602 | scanf("%d %d %d", &mark1, &mark2, &mark3);
603 |
604 | average = (mark1 + mark2 + mark3) / 3;
605 |
606 | if (average >= 50)
607 | printf("Pass\n");
608 | else
609 | printf("Fail\n");
610 |
611 | return 0;
612 | }
613 |
614 | /* ---------------------------------------------------------------- */
615 |
616 | /* Character to ASCII code */
617 |
618 | #include
619 | #include
620 |
621 | int main()
622 | {
623 | char c;
624 | printf("Enter a character to get its ASCII code:\n");
625 | c = getchar();
626 | printf("Character: %c\n ASCII: %d", c, c);
627 | return 0;
628 | }
629 |
630 | /* ---------------------------------------------------------------- */
631 |
632 | /* ASCII code to character */
633 |
634 | #include
635 | #include
636 |
637 | int main()
638 | {
639 | int ascii;
640 | printf("Enter aan ASCII code to get the character:\n");
641 | scanf("%d", &ascii);
642 | printf("%c", ascii);
643 |
644 | return 0;
645 | }
646 |
647 | /* ---------------------------------------------------------------- */
648 |
649 | /* Read chatacter and print it on screen */
650 |
651 | #include
652 | #include
653 |
654 | int main()
655 | {
656 | char c;
657 | printf("Enter character:\n");
658 | c = getchar();
659 | putchar(c);
660 | putchar('\n');
661 |
662 | return 0;
663 | }
664 |
665 | /* ---------------------------------------------------------------- */
666 |
667 | /* Checks if character is a capital latin letter */
668 |
669 | #include
670 | #include
671 |
672 | int main()
673 | {
674 | char c;
675 | printf("Enter a character to check if it is a capital latin letter:\n");
676 | c = getchar();
677 |
678 | if (c >= 65 && c <= 90)
679 | printf("The character is a capital latin letter.\n");
680 | else
681 | printf("The character is not a capital latin letter.\n");
682 |
683 | return 0;
684 | }
685 |
686 | /* ---------------------------------------------------------------- */
687 |
688 | /* Takes character as input and:
689 | * Prints the character if it is a lowercase letter
690 | * Prints "You enter a digit" if it is a number
691 | * Does nothing otherwise
692 | */
693 |
694 | #include
695 | #include
696 |
697 | int main()
698 | {
699 | char ch;
700 | ch = getchar();
701 | if (ch >= 97 && ch <= 122)
702 | putchar(ch);
703 | if (ch >= 48 && ch <= 57)
704 | printf("You entered a digit");
705 |
706 | return 0;
707 | }
708 |
709 | /* ---------------------------------------------------------------- */
710 |
711 | /* Take a character as input, output the next ASCII character it it's a letter
712 | * or the character if it's a number.
713 | */
714 |
715 | #include
716 | #include
717 |
718 | int main()
719 | {
720 | char ch;
721 | ch = getchar();
722 |
723 | if (ch >= 48 && ch <= 57) /* number */
724 | putchar(ch);
725 | if ((ch >= 65 && ch <= 90) || (ch >= 97 && ch <= 122)) /* Capital and lowercase letters */
726 | printf("%c", ch + 1);
727 |
728 | return 0;
729 | }
730 |
731 | /* ---------------------------------------------------------------- */
732 |
733 | /* Take an floating point number and display the integer and the
734 | * fractional parts seperately */
735 |
736 | #include
737 |
738 | int main()
739 | {
740 | float num, fra_p;
741 | int int_p;
742 | scanf("%f", &num);
743 | int_p = num;
744 | fra_p = num - int_p;
745 |
746 | printf("%d\n", int_p);
747 | printf("%f\n", fra_p);
748 |
749 | return 0;
750 | }
751 |
752 | /* ---------------------------------------------------------------- */
753 |
754 | /* Area of a circle */
755 |
756 | #include
757 | #include
758 | #define PI 3.141592653589793
759 |
760 | int main()
761 | {
762 | double r, area;
763 | printf("Enter radius:\n");
764 | scanf("%lf", &r);
765 |
766 | area = PI * pow(r, 2);
767 | printf("A circle of radius %f has an area of %lf\n", r, area);
768 |
769 | return 0;
770 | }
771 |
772 | /* ---------------------------------------------------------------- */
773 |
774 | /* Takes a float and an int and calculates the remainder of their division,
775 | * ex. 5.14 and will output 1.14 */
776 |
777 | #include
778 |
779 | int main()
780 | {
781 | float num_f;
782 | int num_i;
783 | int num_f_to_i;
784 | float remainder;
785 |
786 | scanf("%f %d", &num_f, &num_i);
787 |
788 | num_f_to_i = num_f;
789 | remainder = (num_f_to_i % num_i) + (num_f - num_f_to_i);
790 |
791 | printf("%f", remainder);
792 |
793 | return 0;
794 | }
795 |
796 | /* ---------------------------------------------------------------- */
797 |
798 | /* Takes a float and an int and calculates the remainder of their division,
799 | * ex. 5.14 and will output 1.14 */
800 |
801 | /* Alternative solution */
802 |
803 | #include
804 |
805 | int main()
806 | {
807 | float num_f, remainder;
808 | int num_i, temp;
809 | scanf("%f %d", &num_f, &num_i);
810 | temp = num_f / num_i;
811 | remainder = num_f - temp * num_i;
812 | printf("%f\n", remainder);
813 |
814 | return 0;
815 | }
816 |
817 | /* ---------------------------------------------------------------- */
818 |
819 | /* Stylized multiplication of two floating-point numbers */
820 |
821 | #include
822 |
823 | int main()
824 | {
825 | float a, b, product;
826 | scanf("%f %f", &a, &b);
827 |
828 | product = a * b;
829 |
830 | printf("\n %9.2f\n", a);
831 | printf("x%9.2f\n", b);
832 | printf("==========\n");
833 | printf(" %9.2f\n\n", product);
834 |
835 | return 0;
836 | }
837 |
838 | /* ---------------------------------------------------------------- */
839 |
840 | /* Tax calculator
841 | *
842 | * Income Tax
843 | * <5000 0%
844 | * 5000-10000 5%
845 | * 10000-30000 15%
846 | * >30000 35%
847 | */
848 |
849 | #include
850 |
851 | int main()
852 | {
853 | float income, tax;
854 | scanf("%f", &income);
855 |
856 | if (income < 5000)
857 | tax = 0;
858 | else if (income >= 5000 && income <= 10000)
859 | tax = income * 0.05;
860 | else if (income > 10000 && income <= 30000)
861 | tax = income * 0.15;
862 | else
863 | tax = income * 0.35;
864 |
865 | printf("Tax: %f", tax);
866 | return 0;
867 | }
868 |
869 | /* ---------------------------------------------------------------- */
870 |
871 | /* Leap Years */
872 |
873 | #include
874 |
875 | int main()
876 | {
877 | int year;
878 | scanf("%d", &year);
879 |
880 | if (year % 4 == 0)
881 | {
882 | if (year % 100 == 0)
883 | {
884 | if (year % 400 == 0)
885 | {
886 | printf("Leap year");
887 | }
888 | else
889 | {
890 | printf("Not leap year");
891 | }
892 | }
893 | else
894 | {
895 | printf("Leap year");
896 | }
897 | }
898 | else
899 | {
900 | printf("Not leap year");
901 | }
902 |
903 | return 0;
904 | }
905 |
906 | /* ---------------------------------------------------------------- */
907 |
908 | /* Coffee Venting Machine
909 | * Takes only €5 banknotes
910 | * Coffee price: €0.70
911 | * Takes number of coffees as input and calculates change
912 | * and whether €5 are enough for the purchase.
913 | */
914 |
915 | #include
916 |
917 | int main()
918 | {
919 | const int price = 70;
920 | const int available = 500;
921 | int coffees, total, change, total_change;
922 | int c10, c20, c50, c1, c2 = 0;
923 |
924 | printf("How many coffees would you like to order?\n");
925 | scanf("%d", &coffees);
926 |
927 | while (coffees < 0)
928 | {
929 | printf("Please enter a valid number:\n");
930 | scanf("%d", &coffees);
931 | }
932 |
933 | total = price * coffees;
934 | if (total > available)
935 | {
936 | printf("€5 are not enough for %d coffees.\n", coffees);
937 | return 0;
938 | }
939 |
940 | change = available - total;
941 | total_change = change;
942 |
943 | while (change > 0)
944 | {
945 | if (change >= 400)
946 | {
947 | c2 += 2;
948 | change -= 400;
949 | }
950 | if (change >= 200 && change < 400)
951 | {
952 | c2 += 1;
953 | change -= 200;
954 | }
955 | if (change >= 100 && change < 200)
956 | {
957 | c1 += 1;
958 | change -= 100;
959 | }
960 | if (change >= 50 && change < 100)
961 | {
962 | c50 += 1;
963 | change -= 50;
964 | }
965 | if (change >= 20 && change < 50)
966 | {
967 | c20 += 1;
968 | change -= 20;
969 | }
970 | if (change >= 10 && change < 20)
971 | {
972 | c10 += 1;
973 | change -= 10;
974 | }
975 | }
976 |
977 | printf("\n Price : %d", total / 100);
978 | printf("\nChange :\n");
979 | if (c2) printf(" €2 coins : %d\n", c2);
980 | if (c1) printf(" €1 coins : %d\n", c1);
981 | if (c50) printf("€0.50 coins : %d\n", c50);
982 | if (c20) printf("€0.20 coins : %d\n", c20);
983 | if (c10) printf("€0.10 coins : %d\n", c10);
984 | printf("=======================\n");
985 | printf(" Total : %d\n\n", total_change / 100);
986 |
987 | return 0;
988 | }
989 |
990 | /* ---------------------------------------------------------------- */
991 |
992 | /* Coffee Venting Machine */
993 | /* Solution 2 */
994 |
995 | #include
996 |
997 | int main()
998 | {
999 | const int price = 70;
1000 | const int available = 500;
1001 | int coffees, change, total_change;
1002 | int c10, c20, c50, c1, c2 = 0;
1003 |
1004 | printf("How many coffees would you like to order?\n");
1005 | scanf("%d", &coffees);
1006 |
1007 | while (coffees < 0)
1008 | {
1009 | printf("Please enter a valid number:\n");
1010 | scanf("%d", &coffees);
1011 | }
1012 |
1013 | change = available - price * coffees;
1014 | total_change = change;
1015 |
1016 | if (change < 0)
1017 | {
1018 | printf("€5 are not enough for %d coffees.\n", coffees);
1019 | return 1;
1020 | }
1021 |
1022 | c2 = change /200;
1023 | change = change % 200;
1024 |
1025 | c1 = change /100;
1026 | change = change % 100;
1027 |
1028 | c50 = change /50;
1029 | change = change % 50;
1030 |
1031 | c20 = change /20;
1032 | change = change % 20;
1033 |
1034 | c10 = change /10;
1035 | change = change % 10;
1036 |
1037 | printf("\nChange :\n");
1038 | if (c2)
1039 | printf(" €2 coins : %d\n", c2);
1040 | if (c1)
1041 | printf(" €1 coins : %d\n", c1);
1042 | if (c50)
1043 | printf("€0.50 coins : %d\n", c50);
1044 | if (c20)
1045 | printf("€0.20 coins : %d\n", c20);
1046 | if (c10)
1047 | printf("€0.10 coins : %d\n", c10);
1048 | printf("=======================\n");
1049 | printf(" Total : %f\n\n", total_change / 100.0);
1050 |
1051 | return 0;
1052 | }
1053 |
1054 | /* ---------------------------------------------------------------- */
1055 |
1056 | /* BMI Calculator */
1057 |
1058 | #include
1059 |
1060 | int main()
1061 | {
1062 | float weight, height, bmi;
1063 |
1064 | printf("Please enter your weight:\n");
1065 | scanf("%f", &weight);
1066 |
1067 | printf("Please enter your height:\n");
1068 | scanf("%f", &height);
1069 |
1070 | bmi = weight / (height * height);
1071 |
1072 | if (bmi < 18.5)
1073 | {
1074 | printf("BMI: %f\nUnderwight\n", bmi);
1075 | }
1076 | else if (bmi >= 18.5 && bmi < 25)
1077 | {
1078 | printf("BMI: %f\nNormal Range\n", bmi);
1079 | }
1080 | else if (bmi >= 25 && bmi < 30)
1081 | {
1082 | printf("BMI: %f\nOverweight\n", bmi);
1083 | }
1084 | else
1085 | {
1086 | printf("BMI: %f\nObese\n", bmi);
1087 | }
1088 |
1089 | return 0;
1090 | }
1091 |
1092 | /* ---------------------------------------------------------------- */
1093 |
1094 | /* Quadratic Formula */
1095 |
1096 | #include
1097 | #include
1098 |
1099 | int main()
1100 | {
1101 | float a, b, c, discriminant, x1, x2;
1102 | printf("Enter a, b, c:\n");
1103 | scanf("%f %f %f", &a, &b, &c);
1104 |
1105 | discriminant = b * b - 4 * a * c;
1106 |
1107 | if (discriminant < 0)
1108 | {
1109 | printf("There aren't real solutions to the quadratic equation.\n");
1110 | }
1111 | else if (discriminant == 0)
1112 | {
1113 | x1 = x2 = -b / (2 * a);
1114 | printf("x1, x2 = %f\n", x1);
1115 | }
1116 | else
1117 | {
1118 | x1 = (-b + sqrt(discriminant)) / (2 * a);
1119 | x2 = (-b - sqrt(discriminant)) / (2 * a);
1120 | printf("x1 = %f\n", x1);
1121 | printf("x2 = %f\n", x2);
1122 | }
1123 |
1124 | return 0;
1125 | }
1126 |
1127 | /* ---------------------------------------------------------------- */
1128 |
1129 | /* Tiered Pricing */
1130 |
1131 | #include
1132 |
1133 | int main()
1134 | {
1135 | int sms;
1136 | float price;
1137 |
1138 | printf("How many SMS did you send:\n");
1139 | scanf("%d", &sms);
1140 |
1141 | if (sms <= 10)
1142 | price = sms * 2;
1143 | else if (sms <= 60)
1144 | price = 10 * 2 + (sms - 10) * 1.5;
1145 | else if (sms <= 160)
1146 | price = 10 * 2 + 50 * 1.5 + (sms - 60) * 1.2;
1147 | else
1148 | price = 10 * 2 + 50 * 1.5 + 100 * 1.2 + (sms - 160) * 1;
1149 |
1150 | printf("SMS sent: %d\n", sms);
1151 | printf(" Price : %f\n", price / 100);
1152 |
1153 | return 0;
1154 | }
1155 |
1156 | /* ---------------------------------------------------------------- */
1157 |
1158 | /* Fuel Consumption */
1159 |
1160 | #include
1161 |
1162 | int main()
1163 | {
1164 | float liters, miles, consumption;
1165 |
1166 | printf("Distance in miles:\n");
1167 | scanf("%f", &miles);
1168 |
1169 | printf("Fuel used in liters:\n");
1170 | scanf("%f", &liters);
1171 |
1172 | consumption = liters / miles;
1173 |
1174 | if (consumption <= 0.9)
1175 | printf("Very Low Consumption");
1176 | else if (consumption <= 1.2)
1177 | printf("Low Consumption");
1178 | else if (consumption <= 1.8)
1179 | printf("Normal Consumption");
1180 | else
1181 | printf("High Consumption");
1182 |
1183 | return 0;
1184 | }
1185 |
1186 | /* ---------------------------------------------------------------- */
1187 |
1188 | /* Multiplication Table */
1189 |
1190 | #include
1191 |
1192 | int main()
1193 | {
1194 | int i, j;
1195 | for (i = 1; i <= 10; i++)
1196 | {
1197 | for (j = 1; j <= 10; j++)
1198 | {
1199 | printf("%d x %d = %d\n", i, j, i * j);
1200 | }
1201 | printf("-------------\n");
1202 | }
1203 |
1204 | return 0;
1205 | }
1206 |
1207 | /* ---------------------------------------------------------------- */
1208 |
1209 | /* Reverse integer : Takes an integer as input and prints its digits
1210 | * in reverse order */
1211 |
1212 | #include
1213 |
1214 | int main()
1215 | {
1216 | int num, quo, rem;
1217 | printf("Enter a number:\n");
1218 | scanf("%d", &num);
1219 | do
1220 | {
1221 | rem = num % 10;
1222 | quo = num /10;
1223 | printf("%d", rem);
1224 | num = quo;
1225 | } while (quo != 0);
1226 |
1227 | return 0;
1228 | }
1229 |
1230 | /* ---------------------------------------------------------------- */
1231 |
1232 | /* Counts typed Latin characters */
1233 |
1234 | #include
1235 |
1236 | int main()
1237 | {
1238 | char ch;
1239 | int lat = 0;
1240 | while (ch != 27)
1241 | {
1242 | ch = getchar();
1243 | if (ch >= 65 && ch <= 90 || ch >= 97 && ch <= 122)
1244 | {
1245 | lat += 1;
1246 | }
1247 | }
1248 | printf("You typed %d Latin letters\n", lat);
1249 | return 0;
1250 | }
1251 |
1252 | /* ---------------------------------------------------------------- */
1253 |
1254 | /* Count characters typed between two asterisks */
1255 |
1256 | #include
1257 |
1258 | int main()
1259 | {
1260 | char ch;
1261 | int asterisk = 0;
1262 | int total = 0;
1263 |
1264 | do
1265 | {
1266 | scanf("%c", &ch);
1267 | if (ch == '*')
1268 | asterisk += 1;
1269 | if (asterisk > 0 && ch != '*' && ch!= 10)
1270 | total += 1;
1271 |
1272 | } while (asterisk < 2);
1273 | printf("%d characters typed between the two asterisks\n", total);
1274 | return 0;
1275 | }
1276 |
1277 | /* ---------------------------------------------------------------- */
1278 |
1279 | /* Sum of fraction 1/1 + 1/2 + ... + 1/limit */
1280 |
1281 | #include
1282 |
1283 | int main()
1284 | {
1285 | int limit = 100;
1286 | double sum = 0;
1287 | for (int i = 1; i <= limit; i++)
1288 | {
1289 | sum += (1.0 / i);
1290 | }
1291 | printf("%lf", sum);
1292 |
1293 | return 0;
1294 | }
1295 |
1296 | /* ---------------------------------------------------------------- */
1297 |
1298 | /* Decimal to Binary Converter */
1299 |
1300 | #include
1301 |
1302 | int main()
1303 | {
1304 | int num, bin, shift;
1305 | printf("Enter number in decimal:\n");
1306 | scanf("%d", &num);
1307 | printf("%d in binary form is:\n", num);
1308 |
1309 | for (shift = 31; shift >= 0; shift--)
1310 | {
1311 | bin = num >> shift;
1312 |
1313 | if (bin & 1)
1314 | printf("1");
1315 | else
1316 | printf("0");
1317 | }
1318 |
1319 | return 0;
1320 | }
1321 |
1322 | /* ---------------------------------------------------------------- */
1323 |
1324 | /* Print numbers 1 to 100 in a matrix */
1325 |
1326 | #include
1327 |
1328 | int main()
1329 | {
1330 | int i;
1331 | for (i = 1; i <= 100; i++)
1332 | {
1333 | if (i < 10)
1334 | printf("0%d ", i);
1335 | else
1336 | printf("%d ", i);
1337 | if (i % 10 == 0)
1338 | putchar('\n');
1339 | }
1340 |
1341 | return 0;
1342 | }
1343 |
1344 | /* ---------------------------------------------------------------- */
1345 |
1346 | /* Calculates the sum of the digits of a given integer */
1347 |
1348 | #include
1349 |
1350 | int main()
1351 | {
1352 | int num, r, q;
1353 | int sum = 0;
1354 | printf("Enter a number:\n");
1355 | scanf("%d", &num);
1356 |
1357 | do
1358 | {
1359 | q = num / 10;
1360 | r = num % 10;
1361 | sum += r;
1362 | num = q;
1363 | } while (q != 0);
1364 |
1365 | printf("Sum of digits: %d", sum);
1366 | return 0;
1367 | }
1368 |
1369 | /* ---------------------------------------------------------------- */
1370 |
1371 | /* Student marks average, maximum and second biggest mark */
1372 |
1373 | #include
1374 |
1375 | int main()
1376 | {
1377 | int i, students;
1378 | float mark, sum, max, max2, average;
1379 | sum = 0;
1380 | students = 10;
1381 |
1382 | for (i = 1; i <= students; i++)
1383 | {
1384 | printf("Enter mark of student %d : \n", i);
1385 | scanf("%f", &mark);
1386 | sum += mark;
1387 |
1388 | if (i == 1)
1389 | {
1390 | max = mark;
1391 | }
1392 | else if (i == 2)
1393 | {
1394 | max2 = mark;
1395 | }
1396 | else
1397 | {
1398 | if (mark > max)
1399 | {
1400 | max2 = max;
1401 | max = mark;
1402 | }
1403 | else if (mark > max2)
1404 | {
1405 | max2 = mark;
1406 | }
1407 | }
1408 | }
1409 |
1410 | average = sum / students;
1411 |
1412 | printf(" Average : %.2f\n", average);
1413 | printf(" Biggest mark : %.2f\n", max);
1414 | printf("Second biggest mark : %.2f\n", max2);
1415 |
1416 | return 0;
1417 | }
1418 |
1419 | /* ---------------------------------------------------------------- */
1420 |
1421 | /* Calculate f(x)=x^4-5x^2+3 for x = 0 to x = 1 with step 0.05 */
1422 |
1423 | #include
1424 | #include
1425 | #include
1426 |
1427 | int main()
1428 | {
1429 | int i;
1430 | double x, fx;
1431 |
1432 | for (x = 0; x <= 1.001; x = x + 0.05)
1433 | {
1434 | fx = pow(x, 4) - 5 * pow(x, 2) + 3;
1435 | printf("x= %lf f(x)= %lf\n", x, fx);
1436 | }
1437 |
1438 | return 0;
1439 | }
1440 |
1441 | /* ---------------------------------------------------------------- */
1442 |
1443 | /* ax^2-bx+3=0 Calculate all real solutions for combinations of a, b
1444 | * in the range [-50, 50], unless they are both 0.
1445 | */
1446 |
1447 | #include
1448 | #include
1449 | #include
1450 |
1451 | int main()
1452 | {
1453 | int a, b;
1454 | double x1, x2, d;
1455 |
1456 | for (a = -50; a <= 50; a++)
1457 | {
1458 | for (b = -50; b <= 50; b++)
1459 | {
1460 | if (a == 0 || b == 0)
1461 | continue;
1462 |
1463 | d = pow(b, 2) - 4 * a * 3;
1464 | if (d > 0)
1465 | {
1466 | x1 = (-b + sqrt(d)) / (2 * a);
1467 | x2 = (-b - sqrt(d)) / (2 * a);
1468 | printf("a=%d b=%d x1=%lf x2=%lf\n", a, b, x1, x2);
1469 | }
1470 | else if (d == 0)
1471 | {
1472 | x1 = -b / (2 * a);
1473 | printf("a=%d b=%d x=%lf\n", a, b, x1);
1474 | }
1475 | else
1476 | {
1477 | printf("a=%d b=%d there are no real solutions\n", a, b);
1478 | }
1479 | }
1480 | }
1481 |
1482 | return 0;
1483 | }
1484 |
1485 | /* ---------------------------------------------------------------- */
1486 |
1487 | /*
1488 | * Euclidean Algorithm computing the greatest commin divisor of two integers
1489 | * Alternative solution
1490 | */
1491 |
1492 | #include
1493 | #include
1494 |
1495 | int main()
1496 | {
1497 | int m, n, temp, r, gcd;
1498 | scanf("%d %d", &m, &n);
1499 |
1500 | if (n > m)
1501 | {
1502 | temp = m;
1503 | m = n;
1504 | n = temp;
1505 | }
1506 |
1507 | if (n == 0)
1508 | {
1509 | gcd = m;
1510 | }
1511 |
1512 | else
1513 | {
1514 | do
1515 | {
1516 | r = m % n;
1517 |
1518 | if (r != 0)
1519 | {
1520 | m = n;
1521 | n = r;
1522 | }
1523 | } while (r != 0);
1524 | gcd = n;
1525 | }
1526 |
1527 | printf("greatest common divisor --> %d", gcd);
1528 | return 0;
1529 | }
1530 |
1531 | /* ---------------------------------------------------------------- */
1532 |
1533 | /* Print all integers in the given range */
1534 |
1535 | #include
1536 |
1537 | void numbers(int from, int to)
1538 | {
1539 | int i, temp;
1540 |
1541 | if (from > to)
1542 | {
1543 | temp = to;
1544 | to = from;
1545 | from = temp;
1546 | }
1547 |
1548 | for (i = from; i <= to; i++)
1549 | {
1550 | printf("%d ", i);
1551 | }
1552 | printf("\n");
1553 | }
1554 |
1555 | int main()
1556 | {
1557 | int first, last;
1558 | printf("Choose range: first number to last number\n");
1559 | scanf("%d %d", &first, &last);
1560 | printf("Show numbers:\n");
1561 | numbers(first, last);
1562 |
1563 | return 0;
1564 | }
1565 |
1566 | /* ---------------------------------------------------------------- */
1567 |
1568 | /* John Wallis formula for pi approximation */
1569 |
1570 | #include
1571 | #include
1572 | #include
1573 |
1574 | long double wallis(int last)
1575 | {
1576 | long double half_pi;
1577 | int i;
1578 | half_pi = 1;
1579 |
1580 | for (i = 1; i <= last - 2; i += 2)
1581 | {
1582 | half_pi *= pow((i+1), 2) / (i * (i+2));
1583 | }
1584 |
1585 | return half_pi;
1586 | }
1587 |
1588 | int main()
1589 | {
1590 | int x;
1591 | scanf("%d", &x);
1592 | printf(" pi = %Lf", 2 * wallis(x));
1593 |
1594 | return 0;
1595 | }
1596 |
1597 | /* ---------------------------------------------------------------- */
1598 |
1599 | /* Leap years between two given years */
1600 |
1601 | #include
1602 | #include
1603 | #include
1604 |
1605 | int leap(int year)
1606 | {
1607 | if (year % 4 == 0)
1608 | {
1609 | if (year % 100 == 0)
1610 | {
1611 | if (year % 400 == 0)
1612 | {
1613 | return 1;
1614 | }
1615 | else
1616 | {
1617 | return 0;
1618 | }
1619 | }
1620 | else
1621 | {
1622 | return 1;
1623 | }
1624 | }
1625 | else
1626 | {
1627 | return 0;
1628 | }
1629 | }
1630 |
1631 | int main()
1632 | {
1633 | int start_year, end_year, this_year;
1634 | scanf("%d %d", &start_year, &end_year);
1635 |
1636 | for (this_year = start_year; this_year <= end_year; this_year++)
1637 | {
1638 | if (leap(this_year) == 1)
1639 | {
1640 | printf("%d ", this_year);
1641 | }
1642 | }
1643 |
1644 | return 0;
1645 | }
1646 |
1647 | /* ---------------------------------------------------------------- */
1648 |
1649 | /* BMI using functions */
1650 |
1651 | #include
1652 | #include
1653 |
1654 | int calculate(float height, float weight)
1655 | {
1656 | float bmi_;
1657 | bmi_ = weight / (height * height);
1658 | return bmi_;
1659 | }
1660 |
1661 | void results(float bmi)
1662 | {
1663 | if (bmi < 18.5)
1664 | {
1665 | printf("BMI: %f\nUnderwight\n", bmi);
1666 | }
1667 | else if (bmi >= 18.5 && bmi < 25)
1668 | {
1669 | printf("BMI: %f\nNormal Range\n", bmi);
1670 | }
1671 | else if (bmi >= 25 && bmi < 30)
1672 | {
1673 | printf("BMI: %f\nOverweight\n", bmi);
1674 | }
1675 | else
1676 | {
1677 | printf("BMI: %f\nObese\n", bmi);
1678 | }
1679 | printf("\n");
1680 | }
1681 |
1682 | int main()
1683 | {
1684 | float h, w;
1685 | do
1686 | {
1687 | printf("Enter height:\n");
1688 | scanf("%f", &h);
1689 | printf("Enter weight:\n");
1690 | scanf("%f", &w);
1691 | if (h == 0 || w == 0) break;
1692 | results(calculate(h, w));
1693 | } while (1);
1694 | return 0;
1695 | }
1696 |
1697 | /* ---------------------------------------------------------------- */
1698 |
1699 | /* Calculate f(x)=x^4-10x^2+2 for x = 0 to x = 1 with step 0.05 using function */
1700 |
1701 | #include
1702 | #include
1703 | #include
1704 |
1705 | double calculate(double x_)
1706 | {
1707 | double fx_;
1708 | fx_ = pow(x_, 4) - 10 * pow(x_, 2) + 2;
1709 |
1710 | return fx_;
1711 | }
1712 |
1713 | int main()
1714 | {
1715 | int i;
1716 | double x;
1717 |
1718 | for (x = 0; x <= 1.001; x = x + 0.05)
1719 | {
1720 | calculate(x);
1721 | printf("x= %.3lf f(x)= %.3lf\n", x, calculate(x));
1722 | }
1723 |
1724 | return 0;
1725 | }
1726 |
1727 | /* ---------------------------------------------------------------- */
1728 |
1729 | /* VAT calculator with functions
1730 | * Each product belongs to a VAT category as following
1731 | *
1732 | * Category VAT%
1733 | * 1 0.00
1734 | * 2 0.06
1735 | * 3 0.13
1736 | * 4 0.19
1737 | */
1738 |
1739 | #include
1740 | #include
1741 | #include
1742 |
1743 | float calculate_vat(int quantity_, float price_, int cat_)
1744 | {
1745 | float vat;
1746 | switch (cat_)
1747 | {
1748 | case 1:
1749 | vat = 0;
1750 | break;
1751 | case 2:
1752 | vat = quantity_ * price_ * 0.06;
1753 | break;
1754 | case 3:
1755 | vat = quantity_ * price_ * 0.13;
1756 | break;
1757 | case 4:
1758 | vat = quantity_ * price_ * 0.19;
1759 | break;
1760 | }
1761 | return vat;
1762 | }
1763 |
1764 | int main()
1765 | {
1766 | int i, quantity, cat;
1767 | float price, total_cost = 0, total_vat = 0;
1768 | for (i = 1; i <= 10; i++)
1769 | {
1770 | printf("Product %d\n", i);
1771 | printf("Quantity:\n");
1772 | scanf("%d", &quantity);
1773 | printf("Price:\n");
1774 | scanf("%f", &price);
1775 | printf("Category:\n");
1776 | scanf("%d", &cat);
1777 |
1778 | total_cost += quantity * price;
1779 | total_vat += calculate_vat(quantity, price, cat);
1780 | }
1781 |
1782 | printf("Price without VAT: %f\n", total_cost - total_vat);
1783 | printf(" VAT: %f\n", total_vat);
1784 | printf("==============================\n");
1785 | printf(" Total: %f", total_cost);
1786 |
1787 | return 0;
1788 | }
1789 |
1790 | /* ---------------------------------------------------------------- */
1791 |
1792 | /* 100 First Primes */
1793 |
1794 | #include
1795 |
1796 | int prime()
1797 | {
1798 | static int num = 1;
1799 | int i, found = 1;
1800 | while (found)
1801 | {
1802 | found = 0;
1803 | num = num + 1;
1804 | for (i = 2; i <= num / 2; i++)
1805 | {
1806 | if (num % i == 0)
1807 | found = 1;
1808 | }
1809 | }
1810 | return num;
1811 | }
1812 |
1813 | int main()
1814 | {
1815 | int i;
1816 | for (int i = 0; i < 100; i++)
1817 | {
1818 | printf("%d ", prime());
1819 | }
1820 | return 0;
1821 | }
1822 |
1823 | /* ---------------------------------------------------------------- */
1824 |
1825 | /* 100 First Primes using a global variable */
1826 | /* THE USE OF GLOBAL VARIABLES IS NOT ADVISABLE */
1827 |
1828 | #include
1829 |
1830 | int num = 1;
1831 |
1832 | int prime()
1833 | {
1834 | int i, found = 1;
1835 | while (found)
1836 | {
1837 | found = 0;
1838 | num = num + 1;
1839 | for (i = 2; i <= num / 2; i++)
1840 | {
1841 | if (num % i == 0)
1842 | found = 1;
1843 | }
1844 | }
1845 | return num;
1846 | }
1847 |
1848 | int main()
1849 | {
1850 | int i;
1851 | for (int i = 0; i < 100; i++)
1852 | {
1853 | printf("%d ", prime());
1854 | }
1855 | return 0;
1856 | }
1857 |
1858 |
1859 | /* ---------------------------------------------------------------- */
1860 |
1861 | /* Sum and product of two numbers using global variables */
1862 | /* THE USE OF GLOBAL VARIABLES IS NOT ADVISABLE */
1863 |
1864 | #include
1865 |
1866 | int x, y, sum, pro;
1867 |
1868 | int add()
1869 | {
1870 | return x + y;
1871 | }
1872 |
1873 | int gin()
1874 | {
1875 | return x * y;
1876 | }
1877 |
1878 | void out()
1879 | {
1880 | printf("Sum = %d Product = %d \n", sum, pro);
1881 | }
1882 |
1883 | int main()
1884 | {
1885 | scanf("%d %d", &x, &y);
1886 | sum = add();
1887 | pro = gin();
1888 | out();
1889 |
1890 | return 0;
1891 | }
1892 |
1893 | /* ---------------------------------------------------------------- */
1894 |
1895 | /* Factorial */
1896 |
1897 | #include
1898 |
1899 | unsigned long long int calc_fact(int num)
1900 | {
1901 | int i;
1902 | unsigned long long int factorial = 1;
1903 |
1904 | for (i = 1; i <= num; i++)
1905 | {
1906 | factorial *= i;
1907 | }
1908 | return factorial;
1909 | }
1910 |
1911 | int main()
1912 | {
1913 | int num;
1914 | scanf("%d", &num);
1915 | printf("!%d = %llu", num, calc_fact(num));
1916 |
1917 | return 0;
1918 | }
1919 |
1920 | /* ---------------------------------------------------------------- */
1921 |
1922 | /* Live number counter */
1923 |
1924 | #include
1925 |
1926 | int main()
1927 | {
1928 | int delay = 1000000;
1929 |
1930 | for (int i = 0; i < 100; i++)
1931 | {
1932 | printf("\rNumber : %d", i);
1933 | for (int j = 0; j < delay; j++)
1934 | fflush(stdout);
1935 | }
1936 |
1937 | return 0;
1938 | }
1939 |
1940 | /* ---------------------------------------------------------------- */
1941 |
1942 | /* Creates a custom parallelogram of varying sizes according to user input
1943 | * It is created using a character of choice and be filled or empty.
1944 | */
1945 |
1946 | #include
1947 |
1948 | int draw(char ch, int w, int h, int fill)
1949 | {
1950 | int i, j;
1951 | printf("\n\n");
1952 |
1953 | /* print top side */
1954 | for (i = 1; i <= w; i++)
1955 | printf("%c", ch);
1956 | printf("\n");
1957 |
1958 | /* print middle part */
1959 | for (i = 1; i <= h - 2; i++)
1960 | {
1961 | printf("%c", ch);
1962 | for (j = 1; j <= w - 2; j++)
1963 | {
1964 | if (fill == 0)
1965 | printf(" ");
1966 | else if (fill == 1)
1967 | printf("%c", ch);
1968 | }
1969 | printf("%c", ch);
1970 | printf("\n");
1971 | }
1972 |
1973 | /* print bottom side */
1974 | for (i = 1; i <= w; i++)
1975 | {
1976 | printf("%c", ch);
1977 | }
1978 | printf("\n\n\n");
1979 | return 0;
1980 | }
1981 |
1982 | int main()
1983 | {
1984 | char ch;
1985 | int w, h, fill;
1986 |
1987 | printf("Character: ");
1988 | scanf("%c", &ch);
1989 |
1990 | printf("Width: ");
1991 | scanf("%d", &w);
1992 |
1993 | printf("Height: ");
1994 | scanf("%d", &h);
1995 |
1996 | printf("Fill (n=0 y=1): ");
1997 | scanf("%d", &fill);
1998 |
1999 | draw(ch, w, h, fill);
2000 | return 0;
2001 | }
2002 |
2003 | /* ---------------------------------------------------------------- */
2004 | /* -------------------------------POINTERS------------------------- */
2005 | /* ---------------------------------------------------------------- */
2006 |
2007 | /* Print string using a pointer */
2008 |
2009 | #include
2010 |
2011 | int main()
2012 | {
2013 | char *b;
2014 | b = "Hello";
2015 | printf("%s", b);
2016 | }Ï
2017 |
2018 | /* ---------------------------------------------------------------- */
2019 |
2020 | /* Length of string using a pointer */
2021 |
2022 | #include
2023 |
2024 | int length(char *p)
2025 | {
2026 | int c = 0;
2027 | while(*p != '\0')
2028 | {
2029 | c++;
2030 | p++;
2031 | }
2032 | return c;
2033 | }
2034 |
2035 | int main()
2036 | {
2037 | char *p;
2038 | p = "Hello";
2039 | printf("%d", length(p));
2040 |
2041 | return 0;
2042 | }
2043 |
2044 | /* ---------------------------------------------------------------- */
2045 |
2046 | /* Count occurences of a character in a string using a pointer */
2047 |
2048 | #include
2049 | #include
2050 |
2051 | int main()
2052 | {
2053 | char *p, ch;
2054 | ch = getchar();
2055 | int ctr = 0;
2056 | p = "What a great day to be alive!";
2057 |
2058 | while (*p != '\0')
2059 | {
2060 | if (*p == ch)
2061 | ctr++;
2062 | p++;
2063 | }
2064 |
2065 | printf("\'%c\' appears %d times.\n", ch, ctr);
2066 |
2067 | return 0;
2068 | }
2069 |
2070 | /* ---------------------------------------------------------------- */
2071 |
2072 | /* Reverse string */
2073 |
2074 | #include
2075 | #include
2076 |
2077 | int main()
2078 | {
2079 | char *ptr, *start;
2080 | int c = 0;
2081 | ptr = "ACID";
2082 | start = ptr;
2083 |
2084 | while (*ptr != '\0')
2085 | {
2086 | ptr++;
2087 | }
2088 | ptr--;
2089 |
2090 | while (ptr >= start)
2091 | {
2092 | putchar(*ptr);
2093 | ptr--;
2094 | }
2095 |
2096 | return 0;
2097 | }
2098 |
2099 | /* ---------------------------------------------------------------- */
2100 |
2101 | /* Sum of given numbers using pointers */
2102 |
2103 | #include
2104 | #include
2105 |
2106 | int main()
2107 | {
2108 | int a, b, c, *p1, *p2, *p3;
2109 | p1 = &a;
2110 | p2 = &b;
2111 | p3 = &c;
2112 | scanf("%d %d", p1, p2);
2113 |
2114 | *p3 = *p1 + *p2;
2115 |
2116 | printf("c = %d", *p3);
2117 | return 0;
2118 | }
2119 |
2120 | /* ---------------------------------------------------------------- */
2121 | /* --------------------------------STRINGS------------------------- */
2122 | /* ---------------------------------------------------------------- */
2123 |
2124 | /* Print string */
2125 |
2126 | #include
2127 | #include
2128 |
2129 | int main()
2130 | {
2131 | char word[10] = "Hello!";
2132 | printf("%s\n", word);
2133 | return 0;
2134 | }
2135 |
2136 | /* ---------------------------------------------------------------- */
2137 |
2138 | /* Write and print string */
2139 |
2140 | #include
2141 | #include
2142 |
2143 | int main()
2144 | {
2145 | char word[10];
2146 | printf("Enter word\n");
2147 | scanf("%s", word);
2148 | printf("The word is: %s\n", word);
2149 | return 0;
2150 | }
2151 |
2152 | /* ---------------------------------------------------------------- */
2153 |
2154 |
2155 |
2156 | /* ---------------------------------------------------------------- */
2157 |
2158 |
2159 |
2160 | /* ---------------------------------------------------------------- */
2161 |
2162 |
2163 |
2164 | /* ---------------------------------------------------------------- */
2165 |
2166 |
2167 |
2168 | /* ---------------------------------------------------------------- */
2169 |
2170 |
2171 |
2172 | /* ---------------------------------------------------------------- */
2173 |
2174 |
2175 |
2176 | /* ---------------------------------------------------------------- */
2177 |
2178 |
2179 |
2180 | /* ---------------------------------------------------------------- */
2181 |
2182 |
2183 |
2184 | /* ---------------------------------------------------------------- */
2185 |
2186 |
2187 |
2188 | /* ---------------------------------------------------------------- */
2189 |
2190 |
2191 |
2192 | /* ---------------------------------------------------------------- */
2193 |
2194 |
2195 |
2196 | /* ---------------------------------------------------------------- */
2197 |
2198 |
2199 |
2200 | /* ---------------------------------------------------------------- */
2201 |
2202 |
2203 |
2204 | /* ---------------------------------------------------------------- */
2205 |
2206 |
2207 |
2208 | /* ---------------------------------------------------------------- */
2209 |
2210 |
2211 |
2212 | /* ---------------------------------------------------------------- */
2213 |
2214 |
2215 |
2216 | /* ---------------------------------------------------------------- */
2217 |
2218 |
2219 |
2220 | /* ---------------------------------------------------------------- */
2221 |
2222 |
2223 |
2224 | /* ---------------------------------------------------------------- */
2225 |
2226 |
2227 |
2228 | /* ---------------------------------------------------------------- */
2229 |
2230 |
2231 |
2232 | /* ---------------------------------------------------------------- */
2233 |
2234 |
2235 |
2236 | /* ---------------------------------------------------------------- */
2237 |
2238 |
2239 |
2240 | /* ---------------------------------------------------------------- */
2241 |
2242 |
2243 |
2244 | /* ---------------------------------------------------------------- */
2245 |
2246 |
2247 |
2248 | /* ---------------------------------------------------------------- */
2249 |
2250 |
2251 |
2252 | /* ---------------------------------------------------------------- */
2253 |
2254 |
2255 |
2256 | /* ---------------------------------------------------------------- */
2257 |
2258 |
2259 |
2260 | /* ---------------------------------------------------------------- */
2261 |
2262 |
2263 |
2264 | /* ---------------------------------------------------------------- */
2265 |
2266 |
2267 |
2268 | /* ---------------------------------------------------------------- */
2269 |
2270 |
2271 |
2272 | /* ---------------------------------------------------------------- */
2273 |
2274 |
2275 |
2276 | /* ---------------------------------------------------------------- */
2277 |
2278 |
2279 |
2280 | /* ---------------------------------------------------------------- */
2281 |
2282 |
2283 |
2284 | /* ---------------------------------------------------------------- */
2285 |
2286 |
2287 |
2288 | /* ---------------------------------------------------------------- */
2289 |
2290 |
2291 |
2292 | /* ---------------------------------------------------------------- */
2293 |
2294 |
2295 |
2296 | /* ---------------------------------------------------------------- */
2297 |
2298 |
2299 |
2300 | /* ---------------------------------------------------------------- */
2301 |
2302 |
2303 |
2304 | /* ---------------------------------------------------------------- */
2305 |
2306 |
2307 |
2308 | /* ---------------------------------------------------------------- */
2309 |
2310 |
2311 |
2312 | /* ---------------------------------------------------------------- */
2313 |
2314 |
2315 |
2316 | /* ---------------------------------------------------------------- */
2317 |
2318 |
2319 |
2320 | /* ---------------------------------------------------------------- */
2321 |
2322 |
2323 |
2324 | /* ---------------------------------------------------------------- */
2325 |
2326 |
2327 |
2328 | /* ---------------------------------------------------------------- */
2329 |
2330 |
2331 |
2332 | /* ---------------------------------------------------------------- */
2333 |
2334 |
2335 |
2336 | /* ---------------------------------------------------------------- */
2337 |
2338 |
2339 |
2340 | /* ---------------------------------------------------------------- */
2341 |
2342 |
2343 |
2344 | /* ---------------------------------------------------------------- */
2345 |
2346 |
2347 |
2348 | /* ---------------------------------------------------------------- */
2349 |
2350 |
2351 |
2352 | /* ---------------------------------------------------------------- */
2353 |
2354 |
2355 |
2356 | /* ---------------------------------------------------------------- */
2357 |
2358 |
2359 |
2360 | /* ---------------------------------------------------------------- */
2361 |
2362 |
2363 |
2364 | /* ---------------------------------------------------------------- */
2365 |
2366 |
2367 |
2368 | /* ---------------------------------------------------------------- */
2369 |
2370 |
2371 |
2372 | /* ---------------------------------------------------------------- */
2373 |
2374 |
2375 |
2376 | /* ---------------------------------------------------------------- */
2377 |
2378 |
2379 |
2380 | /* ---------------------------------------------------------------- */
2381 |
2382 |
2383 |
2384 | /* ---------------------------------------------------------------- */
2385 |
2386 |
2387 |
2388 | /* ---------------------------------------------------------------- */
2389 |
2390 |
2391 |
2392 | /* ---------------------------------------------------------------- */
2393 |
2394 |
2395 |
2396 | /* ---------------------------------------------------------------- */
2397 |
2398 |
2399 |
2400 | /* ---------------------------------------------------------------- */
2401 |
2402 |
2403 |
2404 | /* ---------------------------------------------------------------- */
2405 |
2406 |
2407 |
2408 | /* ---------------------------------------------------------------- */
2409 |
2410 |
2411 |
2412 | /* ---------------------------------------------------------------- */
2413 |
2414 |
2415 |
2416 | /* ---------------------------------------------------------------- */
2417 |
2418 |
2419 |
2420 | /* ---------------------------------------------------------------- */
2421 |
2422 |
2423 |
2424 | /* ---------------------------------------------------------------- */
2425 |
2426 |
2427 |
2428 | /* ---------------------------------------------------------------- */
2429 |
2430 |
2431 |
2432 | /* ---------------------------------------------------------------- */
2433 |
2434 |
2435 |
2436 | /* ---------------------------------------------------------------- */
2437 |
2438 |
2439 |
2440 | /* ---------------------------------------------------------------- */
2441 |
2442 |
2443 |
2444 | /* ---------------------------------------------------------------- */
2445 |
2446 |
2447 |
2448 | /* ---------------------------------------------------------------- */
2449 |
2450 |
2451 |
2452 | /* ---------------------------------------------------------------- */
2453 |
2454 |
2455 |
2456 | /* ---------------------------------------------------------------- */
2457 |
2458 |
2459 |
2460 | /* ---------------------------------------------------------------- */
2461 |
2462 |
2463 |
2464 | /* ---------------------------------------------------------------- */
2465 |
2466 |
2467 |
2468 | /* ---------------------------------------------------------------- */
2469 |
2470 |
2471 |
2472 | /* ---------------------------------------------------------------- */
2473 |
2474 |
2475 |
2476 | /* ---------------------------------------------------------------- */
2477 |
2478 |
2479 |
2480 | /* ---------------------------------------------------------------- */
2481 |
2482 |
2483 |
2484 | /* ---------------------------------------------------------------- */
2485 |
2486 |
2487 |
2488 | /* ---------------------------------------------------------------- */
2489 |
2490 |
2491 |
2492 | /* ---------------------------------------------------------------- */
2493 |
2494 |
2495 |
2496 | /* ---------------------------------------------------------------- */
2497 |
2498 |
2499 |
2500 | /* ---------------------------------------------------------------- */
2501 |
2502 |
2503 |
2504 | /* ---------------------------------------------------------------- */
2505 |
2506 |
2507 |
2508 | /* ---------------------------------------------------------------- */
2509 |
2510 |
2511 |
2512 | /* ---------------------------------------------------------------- */
2513 |
2514 |
2515 |
2516 | /* ---------------------------------------------------------------- */
2517 |
2518 |
2519 |
2520 | /* ---------------------------------------------------------------- */
2521 |
2522 |
2523 |
2524 | /* ---------------------------------------------------------------- */
2525 |
2526 |
2527 |
2528 | /* ---------------------------------------------------------------- */
2529 |
2530 |
2531 |
2532 |
--------------------------------------------------------------------------------