0% found this document useful (0 votes)
6 views7 pages

C++ Programs for Lab Exams

Uploaded by

fobivi6334
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
6 views7 pages

C++ Programs for Lab Exams

Uploaded by

fobivi6334
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

Programs for lab exam

Electricity Bill
#include <iostream>

using namespace std;

int main() {
int unitsConsumed;
double billAmount = 0.0;
double pendingBill;

cout << "Enter the number of units consumed: ";


cin >> unitsConsumed;

if (unitsConsumed <= 100) {


billAmount = unitsConsumed * 5.0;
} else if (unitsConsumed <= 300) {
billAmount = (100 * 5.0) + ((unitsConsumed - 100) * 7.5);
} else {
billAmount = (100 * 5.0) + (200 * 7.5) + ((unitsConsumed - 300) * 10.0);
}

cout << "Enter any pending electricity bill amount (if none, enter 0): ";
cin >> pendingBill;

double totalBill = billAmount + pendingBill;

cout << "Current Electricity Bill: " << billAmount << " PKR" << endl;
cout << "Pending Electricity Bill: " << pendingBill << " PKR" << endl;
cout << "Total Electricity Bill: " << totalBill << " PKR" << endl;

return 0;
}
Fibonacci Series
#include <iostream>

using namespace std;

int main() {
int n;

cout << "Enter how many Fibonacci numbers to show: ";


cin >> n;

if (n <= 0) {
cout << "Please enter a positive number." << endl;
return 1;
}

if (n == 1) {
cout << "Fibonacci Series: 0" << endl;
return 0;
}

if (n == 2) {
cout << "Fibonacci Series: 0 1" << endl;
return 0;
}

int first = 0;
int second = 1;

cout << "Fibonacci Series: " << first << " " << second << " ";

for (int i = 3; i <= n; ++i) {


int next = first + second;
cout << next << " ";
first = second;
second = next;
}

cout << endl;

return 0;
}
Leap Year
#include <iostream>

using namespace std;

int main() {
int year;

cout << "Enter a year: ";


cin >> year;

// Leap year conditions:


// 1. Divisible by 4 but not by 100, OR
// 2. Divisible by 400

if ((year % 4 == 0 && year % 100 != 0) || (year % 400 == 0)) {


cout << year << " is a leap year." << endl;
} else {
cout << year << " is not a leap year." << endl;
}

return 0;
}
Even / Odd
#include <iostream>

using namespace std;

int main() {
int number;

cout << "Enter an integer: ";


cin >> number;

// Check if the number is perfectly divisible by 2


if (number % 2 == 0) {
cout << number << " is an even number." << endl;
} else {
cout << number << " is an odd number." << endl;
}

return 0;
}
Switch
#include <iostream>

using namespace std;

int main() {
int num1, num2;

cout << "Enter the first integer: ";


cin >> num1;

cout << "Enter the second integer: ";


cin >> num2;

cout << "Before swapping: num1 = " << num1 << ", num2 = " << num2 << endl;

int swap = num1;


num1 = num2;
num2 = temp;

cout << "After swapping: num1 = " << num1 << ", num2 = " << num2 << endl;

return 0;
}
Average and Grades of marks
#include <iostream>

using namespace std;

int main() {
int numSubjects;
double sum = 0;

cout << "Enter the number of subjects: ";


cin >> numSubjects;

if (numSubjects <= 0) {
cout << "Please enter a positive number of subjects." << endl;
return 1;
}

double marks;
for (int i = 0; i < numSubjects; ++i) {
cout << "Subject " << i + 1 << ": ";
cin >> marks;
sum += marks;
}

double average = sum / numSubjects;


char grade;

if (average >= 90) {


grade = 'A';
} else if (average >= 80) {
grade = 'B';
} else if (average >= 70) {
grade = 'C';
} else if (average >= 60) {
grade = 'D';
} else {
grade = 'F';
}

cout << "\n--- Results ---" << endl;


cout << "Total Marks: " << sum << endl;
cout << "Number of Subjects: " << numSubjects << endl;
cout << "Average Marks: " << average << endl;
cout << "Grade: " << grade << endl;

return 0;
}
Sum of all numbers in given range
#include <iostream>
using namespace std;

int main() {
int start, end, sum = 0;

// Taking input from user


cout << "Enter the start number: ";
cin >> start;

cout << "Enter the end number: ";


cin >> end;

// Adding numbers from start to end


for (int i = start; i <= end; i++) {
sum = sum + i; // or sum += i;
}

// Showing the result


cout << "The sum of numbers from " << start << " to " << end << " is: " << sum << endl;

return 0;
}

You might also like