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

Mid Assignment

Uploaded by

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

Mid Assignment

Uploaded by

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

SOLVE :

#include <iostream>
using namespace std;

int main() {
int totalClassesHeld, classesAttended;
double attendanceRate;

// Prompting user for inputs


cout << "Enter the total number of classes held: ";
cin >> totalClassesHeld;
cout << "Enter the number of classes attended by the student: ";
cin >> classesAttended;

// Prevent division by zero


if (totalClassesHeld == 0) {
cout << "Invalid input! Total number of classes cannot be zero."
<< endl;
return 1;
}
// Compute attendance rate
attendanceRate = (static_cast<double>(classesAttended) /
totalClassesHeld) * 100.0;

// Display results
cout << "Attendance Percentage: " << attendanceRate << "%" << endl;

// Decision making based on attendance threshold


if (attendanceRate >= 80.0) {
cout << "The student is allowed to sit for the exam." << endl;
} else {
cout << "The student is not allowed to sit for the exam." <<
endl;
}

return 0;
}

OUTPUT :
SOLVE :

#include <iostream>

#include <string>
using namespace std;

int main() {
const int DAYS_IN_WEEK = 7;
double temperatures[DAYS_IN_WEEK];
string daysOfWeek[DAYS_IN_WEEK] = {"Monday", "Tuesday", "Wednesday",
"Thursday", "Friday", "Saturday", "Sunday"};

double totalTemperature = 0.0;


int hotDays = 0;
double lowestTemperature;
int coldestDayIndex = 0;

// Input temperatures for the week


cout << "Enter the temperatures for the week (in Celsius):" << endl;
for (int i = 0; i < DAYS_IN_WEEK; i++) {
cout << daysOfWeek[i] << ": ";
cin >> temperatures[i];

// Sum up temperatures
totalTemperature += temperatures[i];

// Count hot days (above 30°C)


if (temperatures[i] > 30) {
hotDays++;
}
// Find the coldest day
if (i == 0 || temperatures[i] < lowestTemperature) {
lowestTemperature = temperatures[i];
coldestDayIndex = i;
}
}

// Calculate the average temperature


double averageTemperature = totalTemperature / DAYS_IN_WEEK;

// Display results
cout << "\nAverage temperature for the week: " << averageTemperature
<< "°C" << endl;
cout << "Number of hot days (above 30°C): " << hotDays << endl;
cout << "Coldest day: " << daysOfWeek[coldestDayIndex] << " with a
temperature of " << lowestTemperature << "°C" << endl;

return 0;
}

OUTPUT :
SOLVE :

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

// Function to display a menu and handle user choices


void displayMenu(const vector<int>& numbers) {
int choice;
do {
cout << "\nMenu Options:\n";
cout << "1. Print all even numbers\n";
cout << "2. Print all odd numbers\n";
cout << "3. Print the sum of all numbers\n";
cout << "4. Exit\n";
cout << "Enter your choice: ";
cin >> choice;

switch (choice) {
case 1: {
cout << "Even numbers: ";
for (int num : numbers) {
if (num % 2 == 0) {
cout << num << " ";
}
}
cout << endl;
break;
}
case 2: {
cout << "Odd numbers: ";
for (int num : numbers) {
if (num % 2 != 0) {
cout << num << " ";
}
}
cout << endl;
break;
}
case 3: {
int sum = 0;
for (int num : numbers) {
sum += num;
}
cout << "Sum of all numbers: " << sum << endl;
break;
}
case 4:
cout << "Exiting program. Goodbye!\n";
break;
default:
cout << "Invalid choice. Please try again.\n";
}
} while (choice != 4);
}

int main() {
int n;
vector<int> numbers;

cout << "Enter the number of elements (up to 10): ";


cin >> n;

if (n < 1 || n > 10) {


cout << "Invalid input! Please enter a number between 1 and 10.\
n";
return 1;
}

cout << "Enter " << n << " integers:\n";


for (int i = 0; i < n; ++i) {
int num;
cin >> num;
numbers.push_back(num);
}

displayMenu(numbers);

return 0;
}
OUT[PUT :

You might also like