Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Title:- While and do while examples added #530

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
159 changes: 158 additions & 1 deletion docs/day-05/do-while-loop.md
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,65 @@ int main() {
return 0;
}
```
### Example 2: Display Even Numbers from 2 to 10
```cpp
#include <iostream>

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 <iostream>

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 <iostream>

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

Expand Down Expand Up @@ -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 <iostream>
Expand All @@ -110,3 +169,101 @@ int main() {
return 0;
}
```

### Example 6: Display Odd Numbers from 1 to 9

```cpp
#include <iostream>

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 <iostream>

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 <iostream>

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 <iostream>

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 <iostream>

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;
}
```

19 changes: 19 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.