0% found this document useful (0 votes)
14 views

While Loop

Uploaded by

mohandalabsi100
Copyright
© © All Rights Reserved
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
14 views

While Loop

Uploaded by

mohandalabsi100
Copyright
© © All Rights Reserved
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 6

Example 1: Print Even Numbers Between 1 and 10

#include <iostream>
using namespace std;

int main() {
int num = 1;

while (num <= 10) {


if (num % 2 == 0) { // Check if the number is even
cout << num << " ";
}
num++; // Increment the number
}

return 0;
}

Example 2: Keep Asking for a Password Until Correct


#include <iostream>
#include <string>
using namespace std;

int main() {
string password;
string correctPassword = "pass123";

while (true) {
cout << "Enter password: ";
cin >> password;

if (password == correctPassword) {
cout << "Access granted!" << endl;
break; // Exit the loop when the correct password is entered
} else {
cout << "Incorrect password. Try again." << endl;
}
}

return 0;
}

Example 3: Sum of Positive Numbers Until a Negative Number is Entered

#include <iostream>
using namespace std;

int main() {
int num;
int sum = 0;

while (true) {
cout << "Enter a number (negative to stop): ";
cin >> num;

if (num < 0) {
break; // Exit the loop if the number is negative
}
sum += num; // Add the number to the sum
}

cout << "Sum of positive numbers: " << sum << endl;
return 0;
}

Example 4: Find First Number Divisible by 3 Between 1 and 20

#include <iostream>
using namespace std;

int main() {
int num = 1;

while (num <= 20) {


if (num % 3 == 0) { // Check if the number is divisible by 3
cout << "First number divisible by 3: " << num << endl;
break; // Exit the loop after finding the first divisible number
}
num++;
}

return 0;
}

#include <iostream>
using namespace std;

int main() {
int countdown = 10;

while (countdown > 0) {


cout << countdown << endl;

if (countdown == 5) {
cout << "Halfway there!" << endl; // Alert when countdown reaches 5
}

countdown--; // Decrease the countdown by 1


}

return 0;
}

#include <iostream>
using namespace std;

int main() {
int choice = 0;

while (choice != 3) {
// Display menu options
cout << "Menu:" << endl;
cout << "1. Display a message" << endl;
cout << "2. Add two numbers" << endl;
cout << "3. Exit" << endl;
cout << "Enter your choice: ";
cin >> choice;

if (choice == 1) {
cout << "Hello, welcome to the program!" << endl;
}
else if (choice == 2) {
int num1, num2;
cout << "Enter two numbers: ";
cin >> num1 >> num2;
cout << "The sum is: " << num1 + num2 << endl;
}
else if (choice == 3) {
cout << "Exiting the program..." << endl;
}
else {
cout << "Invalid choice. Please select a valid option." << endl;
}

cout << endl; // Add a new line for readability


}

return 0;
}

+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
++++++++++++++++++++++++++++++++++++++++++

Example 2: Input Validation (Get Positive Number)

#include <iostream>
using namespace std;

int main() {
int number;

do {
cout << "Enter a positive number: ";
cin >> number;

if (number <= 0) {
cout << "Invalid input, please try again." << endl;
}
} while (number <= 0);

cout << "You entered a valid positive number: " << number << endl;
return 0;
}

Example 1: Input Validation (Guessing a Number)

#include <iostream>
using namespace std;

int main() {
int guess;
int target = 7;
do {
cout << "Guess the number (1-10): ";
cin >> guess;

if (guess != target) {
cout << "Wrong guess, try again!" << endl;
}
} while (guess != target);

cout << "Congratulations! You guessed the right number!" << endl;

return 0;
}

Example 2: Menu System

#include <iostream>
using namespace std;

int main() {
int choice;

do {
cout << "Menu:" << endl;
cout << "1. Display Message" << endl;
cout << "2. Add Numbers" << endl;
cout << "3. Exit" << endl;
cout << "Enter your choice: ";
cin >> choice;

switch (choice) {
case 1:
cout << "Hello, welcome to the menu!" << endl;
break;
case 2: {
int a, b;
cout << "Enter two numbers: ";
cin >> a >> b;
cout << "Sum: " << a + b << endl;
break;
}
case 3:
cout << "Exiting..." << endl;
break;
default:
cout << "Invalid choice, try again!" << endl;
}

cout << endl; // Adds spacing between menu prompts


} while (choice != 3);

return 0

Example 3: Sum of Positive Numbers

#include <iostream>
using namespace std;
int main() {
int num;
int sum = 0;

do {
cout << "Enter a positive number (negative to stop): ";
cin >> num;

if (num >= 0) {
sum += num;
}
} while (num >= 0);

cout << "The sum of the positive numbers is: " << sum << endl;

return 0;
}

Example 4: Password Check


This example checks if the user entered the correct password. It keeps asking for
the password until the correct one is entered.

#include <iostream>
#include <string>
using namespace std;

int main() {
string password;
string correctPassword = "pass123";

do {
cout << "Enter password: ";
cin >> password;

if (password != correctPassword) {
cout << "Incorrect password, try again." << endl;
}
} while (password != correctPassword);

cout << "Access granted!" << endl;

return 0;
}

Example 5: Countdown Timer


This example simulates a countdown timer from a user-specified number, displaying
each step until it reaches zero

#include <iostream>
using namespace std;

int main() {
int countdown;

cout << "Enter a number to start the countdown: ";


cin >> countdown;
do {
cout << countdown << "..." << endl;
countdown--;
} while (countdown >= 0);

cout << "Liftoff!" << endl;

return 0;
}

You might also like