While Loop
While Loop
#include <iostream>
using namespace std;
int main() {
int num = 1;
return 0;
}
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;
}
#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;
}
#include <iostream>
using namespace std;
int main() {
int num = 1;
return 0;
}
#include <iostream>
using namespace std;
int main() {
int countdown = 10;
if (countdown == 5) {
cout << "Halfway there!" << endl; // Alert when countdown reaches 5
}
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;
}
return 0;
}
+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
++++++++++++++++++++++++++++++++++++++++++
#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;
}
#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;
}
#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;
}
return 0
#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;
}
#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);
return 0;
}
#include <iostream>
using namespace std;
int main() {
int countdown;
return 0;
}