From f7c19c5a570f82042c0a0a55c29af144802e532f Mon Sep 17 00:00:00 2001 From: avi Date: Sat, 13 Jul 2024 23:49:42 +0530 Subject: [PATCH] While and do while examples added --- docs/day-05/do-while-loop.md | 159 ++++++++++++++++++++++++++++++++++- package-lock.json | 19 +++++ 2 files changed, 177 insertions(+), 1 deletion(-) diff --git a/docs/day-05/do-while-loop.md b/docs/day-05/do-while-loop.md index 3f225d43..d43d9ce6 100644 --- a/docs/day-05/do-while-loop.md +++ b/docs/day-05/do-while-loop.md @@ -58,6 +58,65 @@ int main() { return 0; } ``` +### Example 2: Display Even Numbers from 2 to 10 +```cpp +#include + +int main() { + int i = 2; + while (i <= 10) { + std::cout << i << std::endl; + i += 2; + } + return 0; +} +``` + +### Example 3: Calculate the Sum of Natural Numbers from 1 to 10 +```cpp +#include + +int main() { + int i = 1; + int sum = 0; + while (i <= 10) { + sum += i; + i++; + } + std::cout << "Sum = " << sum << std::endl; + return 0; +} +``` +### Example 4: Check if a Number is Prime + +```cpp +#include + +int main() { + int num; + bool isPrime = true; + std::cout << "Enter a number: "; + std::cin >> num; + + int i = 2; + while (i <= num / 2) { + if (num % i == 0) { + isPrime = false; + break; + } + i++; + } + + if (isPrime) + std::cout << num << " is a prime number." << std::endl; + else + std::cout << num << " is not a prime number." << std::endl; + + return 0; +} +``` + + ## do...while Loop @@ -96,7 +155,7 @@ A `do...while` loop in C++ is similar to a `while` loop, except that the conditi +----------------------+ ``` -### Example 2: Display Numbers from 1 to 5 +### Example 5: Display Numbers from 1 to 5 ```cpp #include @@ -110,3 +169,101 @@ int main() { return 0; } ``` + +### Example 6: Display Odd Numbers from 1 to 9 + +```cpp +#include + +int main() { + int i = 1; + do { + std::cout << i << std::endl; + i += 2; + } while (i < 10); + return 0; +} +``` +### Example 7: Display Multiplication Table of a Given Number + +```cpp +#include + +int main() { + int num; + std::cout << "Enter a number: "; + std::cin >> num; + + int i = 1; + do { + std::cout << num << " * " << i << " = " << num * i << std::endl; + i++; + } while (i <= 10); + + return 0; +} +``` +### Example 8: Sum of Digits of a Number + +```cpp +#include + +int main() { + int num, sum = 0; + std::cout << "Enter a number: "; + std::cin >> num; + + do { + sum += num % 10; + num /= 10; + } while (num != 0); + + std::cout << "Sum of digits = " << sum << std::endl; + + return 0; +} +``` +### Example 9: Reverse a Number +```cpp +#include + +int main() { + int num, reversed = 0; + std::cout << "Enter a number: "; + std::cin >> num; + + do { + int digit = num % 10; + reversed = reversed * 10 + digit; + num /= 10; + } while (num != 0); + + std::cout << "Reversed number = " << reversed << std::endl; + + return 0; +} +``` + +### Example 10: Find the Greatest Common Divisor (GCD) of Two Numbers + +```cpp +#include + +int main() { + int a, b; + std::cout << "Enter two numbers: "; + std::cin >> a >> b; + + do { + if (a > b) + a -= b; + else + b -= a; + } while (a != b); + + std::cout << "GCD = " << a << std::endl; + + return 0; +} +``` + diff --git a/package-lock.json b/package-lock.json index b8910313..d7703b0b 100644 --- a/package-lock.json +++ b/package-lock.json @@ -12562,6 +12562,12 @@ "url": "https://opencollective.com/webpack" } }, + "node_modules/search-insights": { + "version": "2.15.0", + "resolved": "https://registry.npmjs.org/search-insights/-/search-insights-2.15.0.tgz", + "integrity": "sha512-ch2sPCUDD4sbPQdknVl9ALSi9H7VyoeVbsxznYz6QV55jJ8CI3EtwpO1i84keN4+hF5IeHWIeGvc08530JkVXQ==", + "peer": true + }, "node_modules/section-matter": { "version": "1.0.0", "resolved": "https://registry.npmjs.org/section-matter/-/section-matter-1.0.0.tgz", @@ -13523,6 +13529,19 @@ "is-typedarray": "^1.0.0" } }, + "node_modules/typescript": { + "version": "5.5.3", + "resolved": "https://registry.npmjs.org/typescript/-/typescript-5.5.3.tgz", + "integrity": "sha512-/hreyEujaB0w76zKo6717l3L0o/qEUtRgdvUBvlkhoWeOVMjMuHNHk0BRBzikzuGDqNmPQbg5ifMEqsHLiIUcQ==", + "peer": true, + "bin": { + "tsc": "bin/tsc", + "tsserver": "bin/tsserver" + }, + "engines": { + "node": ">=14.17" + } + }, "node_modules/undici-types": { "version": "5.26.5", "resolved": "https://registry.npmjs.org/undici-types/-/undici-types-5.26.5.tgz",