C++ Loop Programs - Assignment Solutions
1. Parking Fee System
#include <iostream>
using namespace std;
int main() {
int hours;
while (true) {
cout << "Enter hours parked (0 to stop): ";
cin >> hours;
if (hours == 0) break;
cout << "Fee: " << hours * 50 << endl;
}
}
2. Load-Shedding Countdown
#include <iostream>
using namespace std;
int main() {
int minutes;
cout << "Enter remaining minutes: ";
cin >> minutes;
while (minutes >= 0) {
cout << minutes << endl;
minutes--;
}
}
3. Water Tank Filling
#include <iostream>
using namespace std;
int main() {
int water = 0;
while (water < 100) {
cout << "Water level: " << water << " liters" << endl;
water += 5;
}
cout << "Tank Full!" << endl;
}
4. Savings Account Balance
#include <iostream>
using namespace std;
int main() {
int deposit, balance = 0;
while (balance < 5000) {
cout << "Enter amount to deposit: ";
cin >> deposit;
balance += deposit;
}
cout << "Target reached! Balance = " << balance << endl;
}
5. Temperature Alert System
#include <iostream>
using namespace std;
int main() {
int temp;
while (true) {
cout << "Enter room temperature: ";
cin >> temp;
if (temp >= 22 && temp <= 25) break;
cout << "Adjust AC" << endl;
}
}