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

लेसन 163 · Practice and Review

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

आउटपुट के साथ 40 क्लासिक C++ प्रोग्राम: प्राइम जाँच, फ़ैक्टोरियल, फ़िबोनाची, पैलिंड्रोम, GCD और LCM, आर्मस्ट्रॉन्ग संख्याएँ, बेस कन्वर्ज़न, सॉर्टिंग, मैट्रिक्स और बहुत कुछ।

कोई भी प्रोग्रामर भाषा आंशिक रूप से यही छोटे क्लासिक प्रोग्राम लिखकर सीखता है: जाँचना कि कोई संख्या प्राइम है या नहीं, स्ट्रिंग उलटना, दूसरी सबसे बड़ी वैल्यू ढूँढना, दो मैट्रिक्स गुणा करना। ये प्रोग्राम इतने छोटे हैं कि एक मिनट में पढ़े जा सकते हैं, फिर भी हर एक असली कौशल का अभ्यास कराता है: लूप, इंटीजर अंकगणित, स्ट्रिंग हैंडलिंग, कंटेनर या एल्गोरिद्म। इस पेज पर ऐसे 40 प्रोग्राम हैं, जो आधुनिक C++23 में शुरू से लिखे गए हैं।

हर प्रोग्राम पूरा है और जैसा है वैसा चलता है: उसे हमारे सर्वर पर कंपाइल करने और आउटपुट देखने के लिए Run दबाएँ। संख्याएँ बदलें, जानबूझकर चीज़ें तोड़ें, और फिर चलाएँ। जो प्रोग्राम इनपुट पढ़ते हैं उनके Input बॉक्स में पहले से वैल्यू भरी होती हैं, इसलिए आप उन्हें भी बदल सकते हैं। हर उदाहरण के साथ एक पंक्ति की व्याख्या है और उस लेसन का लिंक जो विचार को गहराई से कवर करता है।

पूरे पेज में दो आदतें दिखती हैं। पहली, प्रोग्राम जहाँ स्टैंडर्ड लाइब्रेरी काम अच्छे से करती है वहाँ उसे इस्तेमाल करते हैं (std::gcd, std::ranges::sort, std::format), और जब एल्गोरिद्म ही असली मुद्दा हो तब उसके बग़ल में हाथ से लिखा वर्शन दिखाते हैं। दूसरी, वे टाइप जानबूझकर चुनते हैं: जब वैल्यू तेज़ी से बढ़ें तब long long या unsigned long long, इंडेक्स और साइज़ के लिए std::size_t। जब आप हमारे प्रोग्राम पढ़ने से पहले इन्हें ख़ुद लिखना चाहें, तो पहले अभ्यास प्रश्न आज़माएँ।

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

ये इनपुट पढ़ना, अंकगणितीय ऑपरेटर और if स्टेटमेंट का अभ्यास कराते हैं।

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

इनपुट ख़त्म होने तक संख्याएँ पढ़ता है और प्रिंट करता है कि कितनी थीं, उनका योग और उनका औसत। while (std::cin >> n) लूप इनपुट के अंत पर साफ़ तरीक़े से रुकता है।

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 std::cin or std::getline), it gets the next line from this box.

2. सम या विषम

शेषफल ऑपरेटर इस्तेमाल करता है: संख्या सम तब है जब n % 2 == 0। 0 से तुलना (1 से नहीं) ऋणात्मक संख्याओं के लिए भी काम करती है, क्योंकि C++ में -7 % 2 का मान -1 है।

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 std::cin or std::getline), it gets the next line from this box.

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

स्पष्ट if शृंखला और std::max तथा initializer list वाला एक-लाइनर दिखाता है। देखें तुलना और लॉजिकल ऑपरेटर।

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 std::cin or std::getline), it gets the next line from this box.

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

std::swap सही औज़ार है। अंकगणित और XOR वाली तरकीबें इंटरव्यू के क्लासिक जवाब हैं, पर उन्हें पढ़ना कठिन है और अंकगणित वाली ओवरफ़्लो कर सकती है।

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 std::cin or std::getline), it gets the next line from this box.

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

12 * 4 जैसे एक्सप्रेशन, प्रति लाइन एक, पढ़ता है और ऑपरेटर कैरेक्टर पर switch से उनका मूल्यांकन करता है। शून्य से भाग क्रैश करने की जगह रिपोर्ट किया जाता है। देखें switch स्टेटमेंट।

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 std::cin or std::getline), it gets the next line from this box.

6. लीप ईयर

वर्ष लीप ईयर तब है जब वह 4 से विभाज्य हो, सिवाय शताब्दी वर्षों के, जब तक वे 400 से विभाज्य न हों। एक बूलियन एक्सप्रेशन तीनों नियम समेट लेता है।

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 std::cin or std::getline), it gets the next line from this box.

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

ये लूप, फ़ंक्शन, रिकर्शन और गणित फ़ंक्शन का अभ्यास कराते हैं।

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

n! को लूप और रिकर्शन, दोनों से निकालता है। unsigned long long 20! तक के फ़ैक्टोरियल रख सकता है; 21! अब 64 बिट में नहीं समाता। देखें इंटीजर टाइप।

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 std::cin or std::getline), it gets the next line from this box.

8. फ़िबोनाची सीक्वेंस

सिर्फ़ आख़िरी दो वैल्यू रखकर पहले कुछ पद प्रिंट करता है, जो O(n) समय और O(1) मेमोरी है। साधारण रिकर्सिव वर्शन वही पद घातांकीय (exponential) बार दोबारा निकालता है।

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 std::cin or std::getline), it gets the next line from this box.

9. प्राइम जाँच

भाजक सिर्फ़ वर्गमूल तक जाँचता है, और 2 के बाद सिर्फ़ विषम। लूप की शर्त d * d <= n लिखने से फ़्लोटिंग-पॉइंट sqrt से बचाव होता है।

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 std::cin or std::getline), it gets the next line from this box.

10. N तक के सभी प्राइम (Sieve of Eratosthenes)

std::vector<bool> में हर प्राइम के गुणजों को composite (संयुक्त) चिह्नित करता है। N तक के हर प्राइम को ढूँढने में O(N log log N) लगता है, जो हर संख्या को अलग से जाँचने से कहीं तेज़ है। देखें std::vector।

C++ 10. Sieve of Eratosthenes

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 std::cin or std::getline), it gets the next line from this box.

11. GCD और LCM

यूक्लिड का एल्गोरिद्म (a, b) को (b, a % b) से तब तक बदलता है जब तक b शून्य न हो जाए। LCM ओवरफ़्लो से बचने के लिए गुणा से पहले भाग देता है। <numeric> में C++17 के std::gcd और std::lcm वही करते हैं।

C++ 11. 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 std::cin or std::getline), it gets the next line from this box.

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

% 10 से आख़िरी अंक अलग करता है और / 10 से उसे हटा देता है। यही लूप बॉडी नीचे पैलिंड्रोम और आर्मस्ट्रॉन्ग जाँच को चलाती है। देखें while लूप।

C++ 12. संख्या उलटना

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 std::cin or std::getline), it gets the next line from this box.

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

संख्या पैलिंड्रोम तब है जब वह अपने ही उलटे के बराबर हो। ऋणात्मक संख्याएँ माइनस चिह्न की वजह से कभी पैलिंड्रोम नहीं होतीं।

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 std::cin or std::getline), it gets the next line from this box.

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

आर्मस्ट्रॉन्ग संख्या अपने अंकों के योग के बराबर होती है, जहाँ हर अंक को अंकों की संख्या की घात तक उठाया जाता है: 153 = 1³ + 5³ + 3³। प्रोग्राम पहले अंक गिनता है, फिर इंटीजर घात इस्तेमाल करता है ताकि फ़्लोटिंग-पॉइंट राउंडिंग न घुसे।

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 std::cin or std::getline), it gets the next line from this box.

15. परफ़ेक्ट संख्याएँ

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

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 std::cin or std::getline), it gets the next line from this box.

16. तेज़ घातांक (Fast exponentiation)

आधार का वर्ग करता है और घातांक को आधा, इसलिए power(3, 40) में चालीस की जगह लगभग छह क़दम लगते हैं। मॉड्युलर वर्शन वही है जिससे क्रिप्टोग्राफ़ी विशाल घात निकालती है। देखें रिकर्शन।

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 std::cin or std::getline), it gets the next line from this box.

17. गुणा सारणी

निश्चित-चौड़ाई फ़ॉर्मैट स्पेक वाले नेस्टेड लूप कॉलम को सीध में रखते हैं। देखें std::format से फ़ॉर्मैटिंग।

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 std::cin or std::getline), it gets the next line from this box.

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

ax² + bx + c = 0 को विविक्तकर (discriminant) से हल करता है और तीनों मामले सँभालता है: दो वास्तविक मूल, एक दोहराया मूल, या दो सम्मिश्र (complex) मूल।

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 std::cin or std::getline), it gets the next line from this box.

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

ये बिटवाइज़ ऑपरेटर और स्ट्रिंग कन्वर्ज़न का अभ्यास कराते हैं।

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

std::format b, o और x टाइप से कन्वर्ट करता है। हाथ से लिखा फ़ंक्शन बार-बार आधार से भाग देता है और 2 से 36 तक किसी भी आधार के लिए काम करता है।

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 std::cin or std::getline), it gets the next line from this box.

20. बाइनरी (या किसी भी आधार) से वापस दशमलव

std::stoi तीसरे आर्ग्युमेंट के रूप में आधार लेता है। हाथ वाला वर्शन चल रही वैल्यू को आधार से गुणा करता है और हर अंक जोड़ता है, जो Horner की विधि है।

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 std::cin or std::getline), it gets the next line from this box.

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

n & (n - 1) सबसे निचला सेट बिट साफ़ करता है, इसलिए लूप हर 1-बिट के लिए एक बार चलता है। C++20 के <bit> हेडर में std::popcount और std::has_single_bit पहले से हैं।

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 std::cin or std::getline), it gets the next line from this box.

स्ट्रिंग

ये स्ट्रिंग, स्ट्रिंग मेथड, कैरेक्टर और std::unordered_map का अभ्यास कराते हैं।

22. स्ट्रिंग उलटना

तीन तरीक़े: रिवर्स इटरेटर से उलटी कॉपी, जगह पर std::ranges::reverse, और दो-इंडेक्स वाला swap लूप जो दिखाता है कि एल्गोरिद्म क्या करता है।

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 std::cin or std::getline), it gets the next line from this box.

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

दोनों सिरों से अक्षरों की तुलना करता है, जो अक्षर या अंक नहीं है उसे छोड़ते हुए और केस को अनदेखा करते हुए। unsigned char कास्ट ज़रूरी है: <cctype> फ़ंक्शन को नकारात्मक char देना अपरिभाषित व्यवहार है।

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 std::cin or std::getline), it gets the next line from this box.

24. स्वर और व्यंजन गिनना

हर अक्षर को लोअरकेस करता है और contains (C++23) से उसे स्वरों की स्ट्रिंग में जाँचता है। जो कैरेक्टर अक्षर नहीं हैं उन्हें अलग गिना जाता है।

C++ 24. स्वर और व्यंजन

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 std::cin or std::getline), it gets the next line from this box.

25. शब्द आवृत्ति

>> से शब्द पढ़ता है (जो व्हाइटस्पेस पर बाँटता है), उन्हें std::map में गिनता है ताकि आउटपुट वर्णक्रम में रहे, और सबसे आम शब्द ढूँढता है। देखें std::map।

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 std::cin or std::getline), it gets the next line from this box.

26. एनाग्राम जाँच

दो शब्द एनाग्राम हैं जब वे वही अक्षर उतनी ही बार इस्तेमाल करते हैं। ऐरे में अक्षर गिनना O(n) है; दोनों स्ट्रिंग को सॉर्ट करके तुलना करना छोटा विकल्प है।

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 std::cin or std::getline), it gets the next line from this box.

27. कैरेक्टर आवृत्ति

std::unordered_map से हर कैरेक्टर गिनता है, फिर जोड़ियों को वेक्टर में कॉपी करके गिनती के आधार पर सॉर्ट करता है। यह ऐसा पैटर्न है जिसे आप अक्सर इस्तेमाल करेंगे, क्योंकि हैश मैप का कोई उपयोगी क्रम नहीं होता।

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 std::cin or std::getline), it gets the next line from this box.

28. हर शब्द को कैपिटलाइज़ करना

जब अक्षर से पहले का कैरेक्टर स्पेस हो (या वह पहला कैरेक्टर हो) तब उसे अपरकेस करता है, और बाक़ी को लोअरकेस।

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 std::cin or std::getline), it gets the next line from this box.

29. रन-लेंथ कंप्रेशन

एक जैसे कैरेक्टर के रन गिनकर aaabccdddd को a3b1c2d4 में बदलता है। std::back_inserter के साथ std::format_to फ़ॉर्मैट किया टेक्स्ट सीधे नतीजे की स्ट्रिंग में जोड़ देता है।

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 std::cin or std::getline), it gets the next line from this box.

ऐरे और वेक्टर

ये std::vector, std::array और सर्च व सॉर्टिंग एल्गोरिद्म का अभ्यास कराते हैं।

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

std::accumulate वैल्यू जोड़ता है (0.0 से शुरू होकर ताकि योग double हो), और std::ranges::minmax एक ही पास में दोनों सिरे ढूँढता है। देखें न्यूमेरिक एल्गोरिद्म।

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 std::cin or std::getline), it gets the next line from this box.

31. दूसरा सबसे बड़ा एलिमेंट

एक पास सबसे बड़ी और दूसरी सबसे बड़ी अलग-अलग वैल्यू रखता है। जब हर एलिमेंट बराबर हो या सिर्फ़ एक हो, तब std::optional "कोई जवाब नहीं" बताता है।

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 std::cin or std::getline), it gets the next line from this box.

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

लीनियर सर्च हर एलिमेंट जाँचता है (O(n)) और किसी भी डेटा पर काम करता है। बाइनरी सर्च हर चरण में सॉर्टेड रेंज को आधा करता है (O(log n)); std::ranges::lower_bound स्टैंडर्ड वर्शन है।

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 std::cin or std::getline), it gets the next line from this box.

33. बबल सॉर्ट

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

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 std::cin or std::getline), it gets the next line from this box.

34. ऐरे को रोटेट करना

std::ranges::rotate चुने हुए एलिमेंट को आगे ले आता है। उलटने की तरकीब (दोनों हिस्से उलटें, फिर पूरा) वही काम जगह पर O(1) अतिरिक्त मेमोरी से करती है।

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 std::cin or std::getline), it gets the next line from this box.

35. डुप्लिकेट हटाना

सॉर्ट करें, फिर std::ranges::unique अनूठी वैल्यू को आगे पैक कर देता है और मिटाने के लिए बची हुई पूँछ लौटाता है। मूल क्रम बनाए रखना हो तो देखी हुई वैल्यू std::unordered_set में याद रखें। देखें std::set।

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 std::cin or std::getline), it gets the next line from this box.

36. दो सॉर्टेड वेक्टर मर्ज करना

दो इंडेक्स से दोनों इनपुट पर चलता है और हमेशा छोटा अगला एलिमेंट लेता है: मर्ज सॉर्ट का हृदय। std::ranges::merge वही करता है।

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 std::cin or std::getline), it gets the next line from this box.

मैट्रिक्स

ये बहुआयामी ऐरे का अभ्यास कराते हैं।

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

मैट्रिक्स को std::vector<std::vector<int>> के रूप में रखता है। गुणनफल की प्रविष्टि (i, j) पहले मैट्रिक्स की पंक्ति i और दूसरे के स्तंभ j का डॉट प्रोडक्ट है, इसलिए भीतरी साइज़ मेल खाने चाहिए।

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 std::cin or std::getline), it gets the next line from this box.

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

पंक्तियों और स्तंभों की अदला-बदली करता है: एलिमेंट (i, j) (j, i) पर चला जाता है, इसलिए 2×3 मैट्रिक्स 3×2 बन जाता है। पंक्ति-दर-पंक्ति छापने से आकार दिखता है।

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 std::cin or std::getline), it gets the next line from this box.

पैटर्न और क्लास

ये नेस्टेड लूप, क्लास और ऑपरेटर ओवरलोडिंग का अभ्यास कराते हैं।

39. पिरामिड और पास्कल का त्रिभुज

पिरामिड हर पंक्ति को आगे के स्पेस से बीच में रखता है। पास्कल के त्रिभुज की हर प्रविष्टि उसी पंक्ति की पिछली प्रविष्टि से C(n, k+1) = C(n, k) * (n - k) / (k + 1) द्वारा निकाली जाती है, जो इंटीजर में बिलकुल सटीक रहती है।

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 std::cin or std::getline), it gets the next line from this box.

40. Fraction क्लास

एक छोटा वैल्यू टाइप: कंस्ट्रक्टर चिह्न को सामान्य करता है और std::gcd से संक्षिप्त करता है, operator+ और operator* नई भिन्नें लौटाते हैं, और std::formatter स्पेशलाइज़ेशन std::println को उसे प्रिंट करने देता है। यह कंस्ट्रक्टर, ऑपरेटर ओवरलोडिंग और एक्सेप्शन को एक साथ जोड़ता है।

C++ 40. Fraction क्लास

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 std::cin or std::getline), it gets the next line from this box.

क्योंकि कंस्ट्रक्टर explicit नहीं है, half + half == 1 अपने-आप 1 को Fraction{1, 1} में बदल देता है। संख्या जैसे टाइप के लिए यह सुविधाजनक है; ज़्यादातर दूसरी क्लास के लिए एक-आर्ग्युमेंट कंस्ट्रक्टर को explicit चिह्नित करें ताकि कन्वर्ज़न आपको चौंकाएँ नहीं।

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

Quick check · 4 questions

of 4 answered You scored / 4 · perfect!

  1. 1. Sieve (प्रोग्राम 10) n के गुणजों को n * n से काटना शुरू करता है। यह सुरक्षित क्यों है?

    k < n वाले किसी भी संयुक्त संख्या k * n का एक प्राइम गुणनखंड n से छोटा होता है (k का कोई गुणनखंड), इसलिए उस प्राइम का पास उसे पहले ही चिह्नित कर चुका होता है।
  2. 2. n = 1200 के लिए संख्या उलटने वाला लूप क्या निकालता है?

    उलटी वैल्यू इंटीजर के रूप में बनती है, और इंटीजर में शुरू के शून्य नहीं होते, इसलिए 1200 उलटकर 21 बनता है।
  3. 3. प्रोग्राम 23 में std::isalnum और std::tolower कॉल करने से पहले कैरेक्टर को unsigned char में कास्ट क्यों किया जाता है?

    <cctype> फ़ंक्शन ऐसा int लेते हैं जो unsigned char के रूप में निरूपित हो सके या EOF के बराबर हो। signed char में 127 से ऊपर के बाइट नकारात्मक होते हैं।
  4. 4. unsigned long long नतीजे के साथ factorial(21) कौन-सी वैल्यू बनाता है?

    C++ में unsigned ओवरफ़्लो भली-भाँति परिभाषित है (यह मॉड्यूलो 2^64 में लपेटता है), इसलिए आपको चुपचाप ग़लत संख्या मिलती है। 20! सबसे बड़ा फ़ैक्टोरियल है जो समाता है।

सारांश

  • संख्या के क्लासिक (प्राइम, GCD, अंक, घात) %, / और जल्दी रुकने वाले लूप पर टिके हैं: वर्गमूल की सीमाएँ, यूक्लिड का एल्गोरिद्म और घातांक को आधा करना।
  • टाइप सोच-समझकर चुनें: तेज़ी से बढ़ने वाली वैल्यू के लिए चौड़े इंटीजर, इंडेक्स के लिए std::size_t, <cctype> कॉल से पहले unsigned char।
  • स्ट्रिंग के क्लासिक कैरेक्टर टेस्ट, दो-इंडेक्स स्कैन और मैप या ऐरे से गिनती को मिलाते हैं।
  • ऐरे के क्लासिक हाथ से लिखा एल्गोरिद्म दिखाते हैं, पर असली कोड में आप स्टैंडर्ड वर्शन (std::ranges::sort, unique, rotate, merge, lower_bound) इस्तेमाल करते हैं।
  • Fraction जैसी छोटी वैल्यू क्लास सामान्यीकरण करने वाले कंस्ट्रक्टर, ऑपरेटर ओवरलोडिंग और std::formatter के साथ इस्तेमाल में सुखद बन जाती हैं।

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

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

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