सामग्री पर जाएँ

लेसन 129 · Practice and Review

C प्रोग्राम: 50 क्लासिक उदाहरण

आउटपुट सहित 50 क्लासिक C प्रोग्राम: अभाज्य संख्या, फ़ैक्टोरियल, फ़िबोनाची, पैलिंड्रोम, GCD और LCM, आर्मस्ट्रॉन्ग संख्या, बेस रूपांतरण, स्ट्रिंग, सॉर्टिंग और मैट्रिक्स।

हर C प्रोग्रामर ने यही छोटे-छोटे क्लासिक प्रोग्राम लिखे हैं: जाँचना कि कोई संख्या अभाज्य (prime) है या नहीं, स्ट्रिंग (string) को उलटना, ऐरे (array) में दूसरा सबसे बड़ा मान खोजना, दो मैट्रिक्स गुणा करना। ये इतने छोटे हैं कि एक मिनट में पढ़े जा सकते हैं, फिर भी हर एक असली कौशल का अभ्यास कराता है: सही सीमाओं वाले लूप (loop), ऐसा इंटीजर (integer) अंकगणित जो ओवरफ़्लो न हो, char ऐरे और नल टर्मिनेटर के साथ सावधानी से काम, और ऐरे में पॉइंटर (pointer)। इस पेज पर ऐसे 50 प्रोग्राम एक जगह हैं, सब साफ़-सुथरे, पोर्टेबल C में शुरू से लिखे गए।

हर प्रोग्राम पूरा है और जैसा है वैसा चलता है। Run दबाएँ, हमारा सर्वर उसे GCC 15 (-std=c23 -Wall -Wextra) से कंपाइल करके आउटपुट दिखाता है। संख्याएँ बदलें, जान-बूझकर कुछ बिगाड़ें और फिर चलाएँ। जो प्रोग्राम इनपुट पढ़ते हैं उनके Input बॉक्स में मान पहले से भरे होते हैं, उन्हें भी बदला जा सकता है। हर उदाहरण के साथ एक छोटी व्याख्या और उस पाठ का लिंक है जो इस विचार को विस्तार से समझाता है।

पूरे पेज में कुछ आदतें दिखेंगी। प्रोग्राम जान-बूझकर टाइप चुनते हैं: जब मान तेज़ी से बढ़ते हैं तो unsigned long long, और आकार व इंडेक्स के लिए size_t। वे scanf की रिटर्न वैल्यू (return value) जाँचते हैं। <ctype.h> के फ़ंक्शन (function) को बुलाने से पहले वे unsigned char में कास्ट (cast) करते हैं। और वे ऐरे की लंबाई sizeof array / sizeof array[0] से केवल वहीं निकालते हैं जहाँ ऐरे अब भी ऐरे है, पॉइंटर नहीं। अगर आप हमारे प्रोग्राम पढ़ने से पहले खुद इन्हें लिखना चाहते हैं, तो पहले practice exercises आज़माएँ।

इनपुट और बुनियादी अंकगणित

ये reading input, printf, arithmetic operators और if statements का अभ्यास कराते हैं।

1. संख्याओं का योग और औसत

इनपुट खत्म होने तक संख्याएँ पढ़ता है और गिनती, योग और औसत प्रिंट करता है। scanf बताता है कि उसने कितने मान सहेजे, इसलिए इनपुट के अंत पर (या पहली ऐसी चीज़ पर जो संख्या नहीं है) लूप साफ़-साफ़ रुक जाता है।

C 1. योग और औसत

Press Run (or Ctrl + Enter) to see the result here.

Program input · what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with scanf or fgets), it gets the next line from this box.

2. सम या विषम

संख्या सम (even) तब होती है जब n % 2 == 0। तुलना 0 से करें, 1 से नहीं: C99 से % बाएँ ऑपरेंड का चिह्न रखता है, इसलिए -7 % 2 का मान -1 है, और n % 2 == 1 वाली जाँच -7 को "सम" कह देगी।

C 2. सम या विषम

Press Run (or Ctrl + Enter) to see the result here.

Program input · what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with scanf or fgets), it gets the next line from this box.

3. तीन संख्याओं में सबसे बड़ी

if की श्रृंखला "अब तक की सबसे बड़ी" संख्या सँभालकर रखती है, यही विचार आप प्रोग्राम 37 में पूरे ऐरे के लिए इस्तेमाल करेंगे। Comparison and Logical Operators देखें।

C 3. तीन में सबसे बड़ी

Press Run (or Ctrl + Enter) to see the result here.

Program input · what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with scanf or fgets), it gets the next line from this box.

4. दो संख्याओं की अदला-बदली

C आर्ग्युमेंट (argument) वैल्यू से पास करता है, इसलिए स्वैप फ़ंक्शन को दोनों वेरिएबल (variable) के पते (addresses) चाहिए। XOR वाली तरकीब इंटरव्यू का क्लासिक जवाब है, लेकिन उसे पढ़ना कठिन है और अगर दोनों पॉइंटर एक ही वेरिएबल की ओर हों तो वह मान को शून्य कर देती है। Pointers and Functions देखें।

C 4. दो संख्याओं की अदला-बदली

Press Run (or Ctrl + Enter) to see the result here.

Program input · what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with scanf or fgets), it gets the next line from this box.

5. सरल कैलकुलेटर

12 * 4 जैसे व्यंजक, हर पंक्ति में एक, पढ़ता है और ऑपरेटर अक्षर पर switch से उनका मान निकालता है। फ़ॉर्मैट में %c से पहले का स्पेस व्हाइटस्पेस (whitespace) छोड़ देता है, इसलिए 12*4 और 12 * 4 दोनों चलते हैं। शून्य से भाग देने पर inf प्रिंट करने के बजाय संदेश दिया जाता है। switch Statements देखें।

C 5. सरल कैलकुलेटर

Press Run (or Ctrl + Enter) to see the result here.

Program input · what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with scanf or fgets), it gets the next line from this box.

6. लीप ईयर

कोई वर्ष लीप ईयर तब होता है जब वह 4 से विभाज्य हो, सदी वाले वर्षों को छोड़कर, जब तक कि वे 400 से विभाज्य न हों। एक ही बूलियन (boolean) व्यंजक तीनों नियम समेट लेता है। C23 में bool एक कीवर्ड है; पुराना कोड इसके लिए <stdbool.h> शामिल करता है (Booleans देखें)।

C 6. लीप ईयर

Press Run (or Ctrl + Enter) to see the result here.

Program input · what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with scanf or fgets), it gets the next line from this box.

7. तापमान रूपांतरण तालिका

सेल्सियस और फ़ारेनहाइट को फ़ील्ड चौड़ाई के साथ अगल-बगल प्रिंट करता है। 9.0 लिखने से अंकगणित फ़्लोटिंग पॉइंट (floating point) में होता है; शुद्ध int अंकगणित में celsius * 9 / 5 ट्रंकेट (truncate) हो जाता। Type Conversions देखें।

C 7. तापमान तालिका

Press Run (or Ctrl + Enter) to see the result here.

Program input · what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with scanf or fgets), it gets the next line from this box.

लूप और संख्या सिद्धांत

ये for loops, while loops, functions, recursion और integer types का अभ्यास कराते हैं।

8. फ़ैक्टोरियल

n! को लूप से और रिकर्शन (recursion) से निकालता है। unsigned long long में 20! तक के फ़ैक्टोरियल आ जाते हैं; 21! 64 बिट में नहीं समाता, और अनसाइन्ड अंकगणित चुपचाप घूम जाता है, इसलिए आख़िरी पंक्ति गलत (लेकिन सुपरिभाषित) संख्या प्रिंट करती है।

C 8. फ़ैक्टोरियल

Press Run (or Ctrl + Enter) to see the result here.

Program input · what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with scanf or fgets), it gets the next line from this box.

9. फ़िबोनाची अनुक्रम

केवल पिछले दो पद रखता है, इसलिए O(n) समय और O(1) मेमोरी में चलता है। F(93) आख़िरी फ़िबोनाची संख्या है जो unsigned long long में समाती है। सीधा रिकर्सिव संस्करण वही पद घातीय (exponential) बार दोबारा निकालता है; Recursion देखें।

C 9. फ़िबोनाची अनुक्रम

Press Run (or Ctrl + Enter) to see the result here.

Program input · what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with scanf or fgets), it gets the next line from this box.

10. अभाज्य संख्या की जाँच

ट्रायल डिवीज़न (trial division) में भाजकों की जाँच केवल n के वर्गमूल तक करनी होती है। शर्त को d * d <= n की जगह d <= n / d लिखने से INT_MAX के पास की संख्याओं पर ओवरफ़्लो नहीं होता। सम भाजक छोड़ देने से काम आधा हो जाता है।

C 10. अभाज्य जाँच

Press Run (or Ctrl + Enter) to see the result here.

Program input · what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with scanf or fgets), it gets the next line from this box.

11. N तक की सभी अभाज्य संख्याएँ (एराटोस्थनीज़ की छलनी)

हर संख्या को जाँचने के बजाय छलनी (sieve) हर अभाज्य संख्या के गुणजों को काट देती है। वह p * p से शुरू करती है क्योंकि छोटे गुणज छोटी अभाज्य संख्याएँ पहले ही काट चुकी होती हैं। किसी रेंज की सभी अभाज्य संख्याओं की सूची बनाने का यह सबसे तेज़ सरल तरीका है।

C 11. एराटोस्थनीज़ की छलनी

Press Run (or Ctrl + Enter) to see the result here.

Program input · what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with scanf or fgets), it gets the next line from this box.

12. GCD और LCM

यूक्लिड का एल्गोरिदम (a, b) को (b, a % b) से बदलता रहता है जब तक b शून्य न हो जाए। LCM के लिए गुणा करने से पहले भाग दें (a / gcd * b), ताकि बीच का मान छोटा रहे।

C 12. GCD और LCM

Press Run (or Ctrl + Enter) to see the result here.

Program input · what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with scanf or fgets), it gets the next line from this box.

13. संख्या उलटना और उसके अंकों का योग

n % 10 आख़िरी अंक देता है और n / 10 उसे हटा देता है। उलटा मान इंटीजर के रूप में बनाने से पीछे के शून्य गायब हो जाते हैं: 1200 उलटकर 21 बनता है।

C 13. उलटना और अंकों का योग

Press Run (or Ctrl + Enter) to see the result here.

Program input · what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with scanf or fgets), it gets the next line from this box.

14. पैलिंड्रोम संख्या

संख्या पैलिंड्रोम (palindrome) तब होती है जब वह अपने उलटे के बराबर हो। यह संस्करण केवल आधे अंक उलटता है, जिससे बड़े मानों पर ओवरफ़्लो भी नहीं होता: जब उलटा आधा हिस्सा बचे हुए हिस्से तक पहुँच जाता है तो यह रुक जाता है।

C 14. पैलिंड्रोम संख्या

Press Run (or Ctrl + Enter) to see the result here.

Program input · what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with scanf or fgets), it gets the next line from this box.

15. आर्मस्ट्रॉन्ग संख्याएँ

आर्मस्ट्रॉन्ग (नार्सिसिस्टिक) संख्या अपने अंकों के योग के बराबर होती है, जब हर अंक को अंकों की संख्या की घात तक उठाया जाए: 153 = 1³ + 5³ + 3³। प्रोग्राम अपना इंटीजर घात फ़ंक्शन इस्तेमाल करता है, क्योंकि pow double के साथ काम करता है और राउंड कर सकता है।

C 15. आर्मस्ट्रॉन्ग संख्याएँ

Press Run (or Ctrl + Enter) to see the result here.

Program input · what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with scanf or fgets), it gets the next line from this box.

16. पूर्ण संख्याएँ

पूर्ण संख्या (perfect number) अपने उचित भाजकों के योग के बराबर होती है: 28 = 1 + 2 + 4 + 7 + 14। भाजक जोड़ियों (d, n / d) में आते हैं, इसलिए लूप केवल वर्गमूल तक चलता है।

C 16. पूर्ण संख्याएँ

Press Run (or Ctrl + Enter) to see the result here.

Program input · what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with scanf or fgets), it gets the next line from this box.

17. तेज़ घातांक

आधार का वर्ग करने और घातांक को आधा करने में केवल लगभग log₂(घातांक) गुणा लगते हैं। मॉड्यूलर संस्करण हर बीच के मान को मॉड्यूलस से नीचे रखता है, इसी तरह क्रिप्टोग्राफ़ी और हैशिंग विशाल घातें निकालती हैं।

C 17. तेज़ घातांक

Press Run (or Ctrl + Enter) to see the result here.

Program input · what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with scanf or fgets), it gets the next line from this box.

unsigned long long में कास्ट करने से %llu हर प्लैटफ़ॉर्म पर सही रहता है; सटीक विकल्प <inttypes.h> का PRIu64 मैक्रो है (Integer Types देखें)।

18. गुणन तालिका

दो नेस्टेड लूप और फ़ील्ड चौड़ाई (%4d) एक सँवरा हुआ ग्रिड बना देते हैं।

C 18. गुणन तालिका

Press Run (or Ctrl + Enter) to see the result here.

Program input · what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with scanf or fgets), it gets the next line from this box.

19. द्विघात समीकरण के मूल

विविक्तकर (discriminant) b² − 4ac तय करता है कि दो वास्तविक मूल हैं, एक दोहराया मूल है, या सम्मिश्र (complex) मूलों की जोड़ी। sqrt <math.h> से आता है; Linux पर आप -lm से लिंक करते हैं, जो हमारा सर्वर पहले ही करता है। The Math Library देखें।

C 19. द्विघात के मूल

Press Run (or Ctrl + Enter) to see the result here.

Program input · what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with scanf or fgets), it gets the next line from this box.

20. अभाज्य गुणनखंड

हर गुणनखंड को उतनी बार भाग दें जितनी बार वह जाता है। जब d * d बचे हुए मान से बड़ा हो जाए, तो शेषफल (अगर वह 1 से बड़ा है) खुद अभाज्य होना चाहिए।

C 20. अभाज्य गुणनखंडन

Press Run (or Ctrl + Enter) to see the result here.

Program input · what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with scanf or fgets), it gets the next line from this box.

21. कोलाट्ज़ अनुक्रम

अगर n सम है तो उसे आधा करें; अगर विषम है तो 3n + 1 लें। किसी ने यह सिद्ध नहीं किया कि हर शुरुआती संख्या 1 तक पहुँचती है, लेकिन आज तक परखी गई हर संख्या पहुँची है। 27 इस बात के लिए मशहूर है कि वह 111 कदम लेती है और रास्ते में 9232 तक चढ़ जाती है।

C 21. कोलाट्ज़ अनुक्रम

Press Run (or Ctrl + Enter) to see the result here.

Program input · what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with scanf or fgets), it gets the next line from this box.

और संख्या प्रोग्राम: नियॉन संख्याएँ, दो अभाज्य का योग, गुणनखंड, फ़िबोनाची योग और ब्याज

पाँच और संख्या प्रोग्राम जो ऊपर के औज़ार दोबारा इस्तेमाल करते हैं: अंकों वाले लूप, ट्रायल डिवीज़न और वर्गमूल की सीमा। (किसी रेंज में आर्मस्ट्रॉन्ग संख्याएँ पहले ही प्रोग्राम 15 में हैं, जो 100,000 से नीचे सब कुछ स्कैन करता है।)

नियॉन संख्याएँ। कोई संख्या नियॉन (neon) है अगर उसके वर्ग के अंकों का योग खुद उसी संख्या के बराबर हो: 9 × 9 = 81 और 8 + 1 = 9। अंकों का योग निकालने वाला लूप वही है जो प्रोग्राम 13 में था। प्रोग्राम दिखाता है कि 10,000 से नीचे केवल 0, 1 और 9 ही योग्य हैं।

C और: नियॉन संख्याएँ

Press Run (or Ctrl + Enter) to see the result here.

Program input · what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with scanf or fgets), it gets the next line from this box.

दो अभाज्य संख्याओं का योग। क्या कोई सम संख्या दो अभाज्य संख्याओं के योग के रूप में लिखी जा सकती है? (यह सभी सम संख्याओं के लिए सिद्ध नहीं हुआ है; यही गोल्डबाख की अटकल (Goldbach's conjecture) है, जिसे कंप्यूटर से बहुत बड़ी रेंजों तक जाँचा जा चुका है।) प्रोग्राम n / 2 तक की हर अभाज्य संख्या p आज़माता है और जाँचता है कि n - p भी अभाज्य है या नहीं। विषम संख्या को केवल 2 और किसी विषम संख्या के योग के रूप में बाँटा जा सकता है, इसलिए प्रोग्राम को बँटवारा तभी मिलता है जब n - 2 अभाज्य हो। इसीलिए 11 (9 अभाज्य नहीं है) और 27 (25 अभाज्य नहीं है) बताते हैं कि कोई बँटवारा मौजूद नहीं है।

C और: दो अभाज्य का योग

Press Run (or Ctrl + Enter) to see the result here.

Program input · what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with scanf or fgets), it gets the next line from this box.

किसी संख्या के सभी गुणनखंड। n तक हर d को जाँचना धीमा और अनावश्यक है। गुणनखंड जोड़ियों d और n / d में आते हैं, और जोड़ी में से एक हमेशा वर्गमूल से छोटा या उसके बराबर होता है। इसलिए लूप d <= n / d छोटा गुणनखंड ढूँढता है, और साथी n / d बड़ा होता है। प्रोग्राम दोनों हिस्से सहेजता है और पहले छोटे गुणनखंड बढ़ते क्रम में, फिर बड़े गुणनखंड खोज के उलटे क्रम में प्रिंट करता है। पूर्ण वर्ग, जैसे 144, में बीच की जोड़ी वही संख्या दो बार होती है, और कोड उसे एक बार प्रिंट करता है।

C और: किसी संख्या के सभी गुणनखंड

Press Run (or Ctrl + Enter) to see the result here.

Program input · what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with scanf or fgets), it gets the next line from this box.

पूर्ण वर्ग का मामला देखने के लिए n को 144 कर दें: आउटपुट में 15 गुणनखंड आते हैं, जो विषम संख्या है, और ऐसा केवल वर्ग संख्याओं के साथ ही होता है।

सम-इंडेक्स वाली फ़िबोनाची संख्याओं का योग। F(0) = 0 और F(1) = 1 के साथ, सम स्थानों की संख्याएँ 0, 1, 3, 8, 21 और आगे इसी तरह हैं। प्रोग्राम इन्हें जोड़ता है और नतीजे को एक सर्वसमिका (identity) से मिलाता है: F(0) + F(2) + ... + F(2k) का योग F(2k + 1) - 1 के बराबर होता है। k = 10 के लिए यह F(21) - 1 = 10,945 है। लूप के नतीजे को बंद-रूप (closed-form) नतीजे से मिलाना एक अच्छी आदत है।

C और: सम-इंडेक्स फ़िबोनाची योग

Press Run (or Ctrl + Enter) to see the result here.

Program input · what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with scanf or fgets), it gets the next line from this box.

साधारण और चक्रवृद्धि ब्याज। साधारण ब्याज P × r × t / 100 है, हर साल वही रकम। चक्रवृद्धि ब्याज में ब्याज पर भी ब्याज मिलता है: t साल बाद कुल रकम P × (1 + r / 100)^t होती है। प्रोग्राम दोनों निकालता है, फिर हर साल ब्याज को शेष में जोड़कर साल-दर-साल तालिका प्रिंट करता है, जो pow वाले सूत्र से मेल खानी चाहिए। यहाँ सरलता के लिए पैसे के मान double में हैं; असली अकाउंटिंग सॉफ़्टवेयर राउंडिंग की गलती से बचने के लिए इंटीजर (पैसे में) इस्तेमाल करता है। Floating-point numbers देखें।

C और: साधारण और चक्रवृद्धि ब्याज

Press Run (or Ctrl + Enter) to see the result here.

Program input · what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with scanf or fgets), it gets the next line from this box.

संख्या आधार और बिट

ये bitwise operators, printf format specifiers और string conversions का अभ्यास कराते हैं।

22. दशमलव से बाइनरी, ऑक्टल और हेक्साडेसिमल

printf ऑक्टल (%o) और हेक्साडेसिमल (%x, %X) सीधे प्रिंट करता है। C23 बाइनरी के लिए %b जोड़ता है, लेकिन हर C लाइब्रेरी अभी इसे सपोर्ट नहीं करती, इसलिए यह प्रोग्राम बफ़र (buffer) को दाईं ओर से भरकर बाइनरी स्ट्रिंग खुद बनाता है।

C 22. दशमलव से अन्य आधार

Press Run (or Ctrl + Enter) to see the result here.

Program input · what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with scanf or fgets), it gets the next line from this box.

23. किसी भी आधार से वापस दशमलव

strtol 2 से 36 तक के किसी भी आधार की स्ट्रिंग पार्स करता है और अपने end पॉइंटर के ज़रिए बताता है कि वह कहाँ रुका, ताकि आप अंत के फ़ालतू अक्षर अस्वीकार कर सकें। हाथ से लिखा लूप एल्गोरिदम दिखाता है: आधार से गुणा करें, अगला अंक जोड़ें।

C 23. आधार से दशमलव

Press Run (or Ctrl + Enter) to see the result here.

Program input · what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with scanf or fgets), it gets the next line from this box.

24. सेट बिट गिनना और 2 की घात जाँचना

n & (n - 1) सबसे निचला सेट बिट साफ़ कर देता है, इसलिए लूप हर 1-बिट के लिए एक बार चलता है (ब्रायन कर्निघन की तरकीब)। 2 की घात में ठीक एक सेट बिट होता है, जिससे एक पंक्ति की जाँच बन जाती है। GCC और Clang __builtin_popcount भी देते हैं, जो कंपाइलर एक्सटेंशन है; C23 का <stdbit.h> stdc_count_ones को मानक बनाता है, लेकिन कई C लाइब्रेरी अभी इसे नहीं भेजतीं।

C 24. सेट बिट और 2 की घात

Press Run (or Ctrl + Enter) to see the result here.

Program input · what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with scanf or fgets), it gets the next line from this box.

25. बाइट क्रम (एंडियननेस) की जाँच

किसी ऑब्जेक्ट (object) को unsigned char * के ज़रिए देखना C में हमेशा मान्य है और दिखाता है कि उसके बाइट मेमोरी में कैसे रखे हैं। हमारा सर्वर x86-64 मशीन है, जो लिटिल-एंडियन (little-endian) है: सबसे कम महत्व वाला बाइट पहले आता है। जो कोड बाइनरी फ़ाइलें या नेटवर्क पैकेट लिखता है उसे इस पर निर्भर नहीं रहना चाहिए; Binary Files देखें।

C 25. एंडियननेस

Press Run (or Ctrl + Enter) to see the result here.

Program input · what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with scanf or fgets), it gets the next line from this box.

स्ट्रिंग

ये strings, string functions, characters and ctype.h और safe string handling का अभ्यास कराते हैं।

26. हाथ से स्ट्रिंग की लंबाई

C की स्ट्रिंग '\0' पर खत्म होने वाला char ऐरे है, इसलिए उसकी लंबाई उस बाइट से पहले के अक्षरों की संख्या है। sizeof पूरे ऐरे को नापता है, टर्मिनेटर और बिना इस्तेमाल हुई जगह समेत।

C 26. स्ट्रिंग की लंबाई

Press Run (or Ctrl + Enter) to see the result here.

Program input · what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with scanf or fgets), it gets the next line from this box.

27. स्ट्रिंग को उसी जगह उलटना

पहले अक्षर की अदला-बदली आख़िरी से, दूसरे की उपांत्य (second-to-last) से करें, और बीच में रुक जाएँ। स्ट्रिंग बदली जा सकने वाला ऐरे होना चाहिए: स्ट्रिंग लिटरल (literal) केवल-पढ़ने योग्य होते हैं, इसलिए reverse("abc") अनडिफ़ाइंड बिहेवियर (undefined behavior) होगा।

C 27. स्ट्रिंग उलटें

Press Run (or Ctrl + Enter) to see the result here.

Program input · what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with scanf or fgets), it gets the next line from this box.

28. पैलिंड्रोम स्ट्रिंग

दो इंडेक्स एक-दूसरे की ओर चलते हैं, जो अक्षर या अंक नहीं है उसे छोड़ते हैं, और केस की परवाह किए बिना तुलना करते हैं। (unsigned char) कास्ट मायने रखते हैं: ऋणात्मक char को isalnum या tolower को देना अनडिफ़ाइंड बिहेवियर है।

C 28. पैलिंड्रोम स्ट्रिंग

Press Run (or Ctrl + Enter) to see the result here.

Program input · what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with scanf or fgets), it gets the next line from this box.

right उस अक्षर से एक आगे की ओर इशारा करता है जिसकी तुलना हो रही है, इसलिए उसे कभी शून्य से नीचे नहीं जाना पड़ता, जो इसलिए ज़रूरी है कि size_t अनसाइन्ड है।

29. स्वर, व्यंजन, अंक और स्पेस गिनना

<ctype.h> अक्षरों का वर्गीकरण करता है; स्वरों की स्ट्रिंग पर strchr सदस्यता जाँचने का संक्षिप्त तरीका है।

C 29. अक्षरों के प्रकार गिनें

Press Run (or Ctrl + Enter) to see the result here.

Program input · what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with scanf or fgets), it gets the next line from this box.

30. पंक्तियाँ, शब्द और अक्षर गिनना

Unix के wc टूल का छोटा संस्करण। यह getchar से इनपुट एक-एक अक्षर पढ़ता है और जब भी व्हाइटस्पेस से किसी गैर-स्पेस अक्षर पर पहुँचता है तो एक शब्द गिनता है। Input बॉक्स बदलकर दोबारा चलाएँ।

C 30. पंक्ति, शब्द और अक्षर की गिनती

Press Run (or Ctrl + Enter) to see the result here.

Program input · what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with scanf or fgets), it gets the next line from this box.

c एक int है, char नहीं, क्योंकि getchar को हर बाइट मान के साथ-साथ अलग मान EOF भी लौटा पाना चाहिए। खाली तीसरी पंक्ति एक पंक्ति के रूप में गिनी जाती है, लेकिन उसमें कोई शब्द नहीं है।

31. अक्षरों की आवृत्ति

letter - 'a' से इंडेक्स किए गए 26 काउंटरों का ऐरे मैप (map) की जगह ले लेता है। यह इस पर निर्भर है कि 'a' से 'z' तक लगातार हैं, जो ASCII और UTF-8 में सच है (हर वह सिस्टम जो आप इस्तेमाल करेंगे)।

C 31. अक्षरों की आवृत्ति

Press Run (or Ctrl + Enter) to see the result here.

Program input · what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with scanf or fgets), it gets the next line from this box.

32. एनाग्राम की जाँच

दो स्ट्रिंग एनाग्राम (anagram) तब होती हैं जब हर अक्षर दोनों में बराबर बार आता है। पहली स्ट्रिंग के लिए ऊपर गिनें और दूसरी के लिए नीचे; अगर हर काउंटर शून्य पर खत्म हो तो वे एनाग्राम हैं। यह O(n) है, दोनों स्ट्रिंग को सॉर्ट करने से तेज़।

C 32. एनाग्राम जाँच

Press Run (or Ctrl + Enter) to see the result here.

Program input · what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with scanf or fgets), it gets the next line from this box.

33. हर शब्द को कैपिटल करना

एक फ़्लैग (flag) याद रखता है कि पिछला अक्षर व्हाइटस्पेस था या नहीं। फ़ंक्शन ऐरे को उसी जगह बदलता है, इसलिए उसे const char * नहीं, char * मिलता है।

C 33. शब्दों को कैपिटल करें

Press Run (or Ctrl + Enter) to see the result here.

Program input · what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with scanf or fgets), it gets the next line from this box.

34. स्ट्रिंग से कोई अक्षर हटाना

क्लासिक दो-इंडेक्स संघनन (compaction): read हर अक्षर पर जाता है, write केवल उन अक्षरों के लिए आगे बढ़ता है जिन्हें आप रखते हैं। यह एक पास में, बिना अतिरिक्त बफ़र के चलता है।

C 34. अक्षर हटाएँ

Press Run (or Ctrl + Enter) to see the result here.

Program input · what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with scanf or fgets), it gets the next line from this box.

35. रन-लेंथ एन्कोडिंग

aaabccdddd को a3b1c2d4 में बदलता है। आउटपुट बफ़र का आकार तय है, इसलिए फ़ंक्शन snprintf से लिखता है और ओवरफ़्लो करने के बजाय विफलता की रिपोर्ट करता है। यही आदत Safe String Handling से आती है।

C 35. रन-लेंथ एन्कोडिंग

Press Run (or Ctrl + Enter) to see the result here.

Program input · what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with scanf or fgets), it gets the next line from this box.

36. स्ट्रिंग को वर्णानुक्रम में सॉर्ट करना

qsort तुलनित्र (comparator) को तत्वों के पॉइंटर देता है। तत्व const char * हैं, इसलिए हर आर्ग्युमेंट असल में const char *const * है, जिसे strcmp बुलाने से पहले एक बार डीरेफ़रेंस (dereference) किया जाता है। यह एक्स्ट्रा स्तर भूल जाना सबसे आम qsort बग है। qsort and bsearch देखें।

C 36. स्ट्रिंग सॉर्ट करें

Press Run (or Ctrl + Enter) to see the result here.

Program input · what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with scanf or fgets), it gets the next line from this box.

"Apricot" सबसे पहले आता है क्योंकि strcmp बाइट मानों की तुलना करता है, और ASCII का हर अपरकेस अक्षर हर लोअरकेस अक्षर से छोटा है।

ऐरे

ये arrays, pointers and arrays और sorting and searching algorithms का अभ्यास कराते हैं।

37. योग, औसत, न्यूनतम और अधिकतम

एक ही पास चारों नतीजे इकट्ठा कर लेता है। ऐरे पॉइंटर और लंबाई के रूप में पास किया जाता है, क्योंकि फ़ंक्शन के अंदर sizeof values केवल पॉइंटर को नापेगा।

C 37. ऐरे के आँकड़े

Press Run (or Ctrl + Enter) to see the result here.

Program input · what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with scanf or fgets), it gets the next line from this box.

38. दूसरा सबसे बड़ा तत्व

एक पास में सबसे बड़ा और दूसरा सबसे बड़ा ट्रैक करें। found फ़्लैग उन ऐरे को सँभालता है जिनके सभी मान बराबर हैं और जिनमें कोई दूसरा सबसे बड़ा अलग मान नहीं होता।

C 38. दूसरा सबसे बड़ा

Press Run (or Ctrl + Enter) to see the result here.

Program input · what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with scanf or fgets), it gets the next line from this box.

39. लीनियर और बाइनरी सर्च

लीनियर सर्च किसी भी ऐरे पर O(n) में चलता है। बाइनरी सर्च को सॉर्टेड ऐरे चाहिए और वह हर कदम पर रेंज आधी कर देता है, इसलिए उसमें O(log n) लगता है: दस लाख तत्वों के लिए करीब 20 कदम। low + (high - low) / 2 से (low + high) / 2 वाला ओवरफ़्लो नहीं होता।

C 39. लीनियर और बाइनरी सर्च

Press Run (or Ctrl + Enter) to see the result here.

Program input · what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with scanf or fgets), it gets the next line from this box.

अर्ध-खुली रेंज [low, high) हर इंडेक्स को वैध size_t बनाए रखती है। high = count - 1 वाला आम संस्करण तब टूटता है जब count 0 हो या जब middle - 1 शून्य से नीचे चला जाए।

40. बबल सॉर्ट

बार-बार उन पड़ोसियों की अदला-बदली करता है जो क्रम से बाहर हैं। swapped फ़्लैग तब जल्दी रोक देता है जब किसी पास में कुछ नहीं बदलता, इसलिए पहले से सॉर्टेड ऐरे में केवल एक पास लगता है। यह O(n²) है, सीखने के लिए ठीक है, असली डेटा के लिए बहुत धीमा: उसकी जगह qsort इस्तेमाल करें।

C 40. बबल सॉर्ट

Press Run (or Ctrl + Enter) to see the result here.

Program input · what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with scanf or fgets), it gets the next line from this box.

41. ऐरे को घुमाना (rotate)

k से बाईं ओर घुमाना तीन रिवर्सल से (पहले k तत्व, बाकी, फिर पूरा) उसी जगह O(n) समय में, बिना अतिरिक्त ऐरे के हो जाता है।

C 41. ऐरे घुमाएँ

Press Run (or Ctrl + Enter) to see the result here.

Program input · what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with scanf or fgets), it gets the next line from this box.

42. सॉर्टेड ऐरे से डुप्लिकेट हटाना

सॉर्टेड ऐरे में डुप्लिकेट एक-दूसरे के बगल में होते हैं, इसलिए प्रोग्राम 34 का दो-इंडेक्स संघनन उन्हें एक पास में हटा देता है। फ़ंक्शन नई लंबाई लौटाता है; उसके बाद के तत्व बचा-खुचा हैं।

C 42. डुप्लिकेट हटाएँ

Press Run (or Ctrl + Enter) to see the result here.

Program input · what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with scanf or fgets), it gets the next line from this box.

43. दो सॉर्टेड ऐरे को मर्ज करना

आगे के तत्वों की तुलना करें, छोटा वाला लें, दोहराएँ; फिर दूसरे ऐरे का जो बचा है उसे कॉपी करें। यह मर्ज सॉर्ट का मूल है (Sorting and Searching Algorithms देखें)।

C 43. सॉर्टेड ऐरे मर्ज करें

Press Run (or Ctrl + Enter) to see the result here.

Program input · what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with scanf or fgets), it gets the next line from this box.

44. गिनना कि हर मान कितनी बार आता है

qsort से एक प्रति सॉर्ट करें, फिर बराबर मानों के रन गिनें। तुलनित्र x - y की जगह (x > y) - (x < y) इस्तेमाल करता है, क्योंकि बड़े मानों के लिए घटाने में ओवरफ़्लो हो सकता है।

C 44. तत्वों की आवृत्ति

Press Run (or Ctrl + Enter) to see the result here.

Program input · what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with scanf or fgets), it gets the next line from this box.

मैट्रिक्स

ये multidimensional arrays का अभ्यास कराते हैं।

45. मैट्रिक्स जोड़ और गुणा

2×3 मैट्रिक्स गुणा 3×2 मैट्रिक्स से 2×2 मैट्रिक्स मिलता है: तत्व [i][j] पहले की पंक्ति i और दूसरे के स्तंभ j का डॉट प्रोडक्ट है। जोड़ के लिए दोनों मैट्रिक्स का आकार एक जैसा होना ज़रूरी है।

C 45. मैट्रिक्स जोड़ और गुणा

Press Run (or Ctrl + Enter) to see the result here.

Program input · what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with scanf or fgets), it gets the next line from this box.

46. मैट्रिक्स का ट्रांसपोज़

पंक्तियाँ स्तंभ बन जाती हैं: t[j][i] = m[i][j]। फ़ंक्शन आकार पहले लेता है ताकि उन्हें ऐरे पैरामीटर के टाइप में इस्तेमाल कर सके (variably modified parameter, जिसके लिए VLA सपोर्ट चाहिए; GCC और Clang में है, MSVC में नहीं)।

C 46. मैट्रिक्स का ट्रांसपोज़

Press Run (or Ctrl + Enter) to see the result here.

Program input · what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with scanf or fgets), it gets the next line from this box.

आख़िरी कॉल में कास्ट इसलिए है कि C (C23 से पहले) int (*)[2] को अपने-आप const int (*)[2] में नहीं बदलता; C23 आख़िरकार इस रूपांतरण की अनुमति देता है, लेकिन कास्ट कोड को पोर्टेबल बनाए रखता है।

47. पंक्ति योग, स्तंभ योग और विकर्ण

वर्ग मैट्रिक्स पर एक पास पंक्ति-योग ऐरे और स्तंभ-योग ऐरे भर देता है और दोनों विकर्णों का योग जोड़ देता है।

C 47. पंक्ति और स्तंभ योग

Press Run (or Ctrl + Enter) to see the result here.

Program input · what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with scanf or fgets), it gets the next line from this box.

और मैट्रिक्स प्रोग्राम: सारणिक, ट्रेस, सीमा, घुमाव और सॉर्टेड पंक्तियाँ

वर्ग मैट्रिक्स पर पाँच और क्लासिक। सारणिक (determinant) पहली पंक्ति के साथ सहखंड प्रसार (cofactor expansion) इस्तेमाल करता है: हर तत्व को उस छोटे मैट्रिक्स के सारणिक से गुणा किया जाता है जो उसकी पंक्ति और स्तंभ हटाने के बाद बचता है, बारी-बारी से चिह्नों के साथ। यह सिकुड़ते मैट्रिक्स पर रिकर्शन है, छोटे आकारों के लिए ठीक है (लागत n! की तरह बढ़ती है, इसलिए असली कोड उसकी जगह एलिमिनेशन इस्तेमाल करता है)। ट्रेस (trace) मुख्य विकर्ण का योग है और नॉर्म (norm, यानी फ़्रोबेनियस नॉर्म) सभी वर्गों के योग का वर्गमूल है। 90 डिग्री दक्षिणावर्त घुमाने पर m[i][j] का स्थान r[j][n - 1 - i] हो जाता है।

C सारणिक, ट्रेस, नॉर्म, सीमा, घुमाव, समानता और सॉर्टेड पंक्तियाँ

Press Run (or Ctrl + Enter) to see the result here.

Program input · what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with scanf or fgets), it gets the next line from this box.

इस मैट्रिक्स का सारणिक 2 x (3 - 2) - 0 + 1 x (1 - 3) = 0 है। घुमाव मूल मैट्रिक्स की पहली पंक्ति 2 0 1 को पढ़कर उसे अंतिम स्तंभ में नीचे की ओर लिखता है। दो मैट्रिक्स तभी बराबर होते हैं जब हर तत्व मेल खाए, इसलिए प्रोग्राम पहले अंतर पर रुक जाता है; memcmp से तुलना करना तब गलत होगा जब पैडिंग (padding) या गैर-इंटीजर टाइप शामिल हों। 2D int ऐरे पंक्ति-दर-पंक्ति स्टोर होता है, इसलिए m[i] खुद N इंटीजर का ऐरे है जिसे qsort उसी जगह सॉर्ट कर सकता है। पहली और आख़िरी पंक्ति की अदला-बदली भी यही विचार है, बस एक अस्थायी पंक्ति ऐरे और memcpy कॉल के साथ।

पैटर्न और स्ट्रक्चर

और आकृतियाँ C Pattern Printing Programs में हैं। ये nested loops, printf field widths और structures का अभ्यास कराते हैं।

48. पिरामिड और फ़्लॉयड का त्रिभुज

पिरामिड की हर पंक्ति rows - row स्पेस और फिर 2 * row - 1 तारे प्रिंट करती है। %*s फ़ील्ड चौड़ाई आर्ग्युमेंट से लेता है, इसलिए printf("%*s", n, "") बिना लूप के n स्पेस प्रिंट कर देता है।

C 48. पिरामिड और फ़्लॉयड का त्रिभुज

Press Run (or Ctrl + Enter) to see the result here.

Program input · what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with scanf or fgets), it gets the next line from this box.

49. पास्कल का त्रिभुज

हर प्रविष्टि एक द्विपद गुणांक (binomial coefficient) C(row, k) है, और उसी पंक्ति की अगली प्रविष्टि C * (row - k) / (k + 1) है। भाग देने से पहले गुणा करने से अंकगणित सटीक रहता है, क्योंकि गुणनफल हमेशा विभाज्य होता है।

C 49. पास्कल का त्रिभुज

Press Run (or Ctrl + Enter) to see the result here.

Program input · what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with scanf or fgets), it gets the next line from this box.

50. struct से बना भिन्न (fraction) टाइप

struct अंश और हर को एक मान में समूहित करता है। हर फ़ंक्शन सामान्यीकृत (normalized) भिन्न लौटाता है (संक्षिप्त, धनात्मक हर के साथ), इसलिए बराबर मानों के सदस्य हमेशा बराबर होते हैं और उनकी तुलना सरल हो जाती है। struct को पास या रिटर्न करने पर वैल्यू से कॉपी किया जाता है, जो ऐसे छोटे टाइप के लिए सस्ता है। Structures देखें।

C 50. भिन्न का struct

Press Run (or Ctrl + Enter) to see the result here.

Program input · what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with scanf or fgets), it gets the next line from this box.

आख़िरी कॉल जान-बूझकर विफल होता है: make_fraction(1, 0) stderr पर त्रुटि प्रिंट करता है और exit(EXIT_FAILURE) से प्रोग्राम खत्म कर देता है, इसलिए "never printed" कभी नहीं दिखता। exit के लिए Useful stdlib.h Functions देखें।

अपनी समझ जाँचें

Quick check · 4 questions

of 4 answered You scored / 4 · perfect!

  1. 1. छलनी (प्रोग्राम 11) p के गुणजों को p * p से काटना शुरू करती है। यह सुरक्षित क्यों है?

    k < p वाले किसी भी गुणज k * p का एक अभाज्य गुणनखंड p से छोटा होता है (k का गुणनखंड), इसलिए उस अभाज्य का पास उसे पहले ही चिह्नित कर चुका होता है।
  2. 2. प्रोग्राम 2 में, C99 और उसके बाद -7 % 2 का मान क्या होता है?

    C99 से इंटीजर विभाजन शून्य की ओर ट्रंकेट होता है, और शेषफल बाएँ ऑपरेंड का चिह्न रखता है, इसलिए -7 / 2 का मान -3 और -7 % 2 का मान -1 है।
  3. 3. प्रोग्राम 30 getchar() का नतीजा char के बजाय int में क्यों रखता है?

    EOF एक ऋणात्मक int है जो हर unsigned char मान से अलग है। उसे char में रखने पर या तो वह किसी असली बाइट से गड़बड़ा जाता है (signed char) या कभी EOF से मेल नहीं खाता (unsigned char)।
  4. 4. प्रोग्राम 36 में, qsort तुलनित्र का हर आर्ग्युमेंट क्या है?

    qsort उन तत्वों के पॉइंटर देता है जिनकी वह तुलना करता है। तत्व const char * मान हैं, इसलिए तुलनित्र को उन पॉइंटरों के पॉइंटर मिलते हैं और उसे एक बार डीरेफ़रेंस करना पड़ता है।

सारांश

  • संख्याओं के क्लासिक (अभाज्य, GCD, अंक, घात, गुणनखंडन) %, / और अच्छी सीमाओं वाले लूप पर टिके हैं: d <= n / d के रूप में लिखी वर्गमूल सीमाएँ, यूक्लिड का एल्गोरिदम, घातांक को आधा करना।
  • टाइप सोच-समझकर चुनें: तेज़ी से बढ़ने वाले मानों के लिए unsigned long long, आकार और इंडेक्स के लिए size_t, getchar के नतीजे के लिए int, और हर <ctype.h> कॉल से पहले unsigned char।
  • स्ट्रिंग के क्लासिक char ऐरे पर दो-इंडेक्स वाले स्कैन हैं: रिवर्स, पैलिंड्रोम, संघनन। नल टर्मिनेटर हमेशा ध्यान में रखें और निश्चित बफ़र में snprintf से लिखें।
  • ऐरे फ़ंक्शन पॉइंटर और लंबाई लेते हैं; sizeof array / sizeof array[0] केवल वहीं काम करता है जहाँ ऐरे घोषित किया गया है।
  • qsort तुलनित्र तत्वों के पॉइंटर पाते हैं और उन्हें x - y की जगह (x > y) - (x < y) लौटाना चाहिए।
  • struct और छोटे फ़ंक्शन मिलकर साफ़ वैल्यू टाइप देते हैं, जैसे सामान्यीकृत Fraction।

अगला: C Practice Exercises, जहाँ आप ऐसे प्रोग्राम खुद लिखते हैं और अपने हल को हमारे हल से मिलाते हैं।

अपनी प्रगति ट्रैक करें

मुफ़्त अकाउंट बनाएँ, लेसन पूरे के रूप में चिह्नित करें और किसी भी डिवाइस पर वहीं से शुरू करें जहाँ छोड़ा था।