IICT Lab Task Week 14
IICT Lab Task Week 14
Communication Technology
LAB Task 14
Result:
int main() {
int num_to_check;
bool is_prime = true;
if (num_to_check <= 1) {
is_prime = false;
} else {
for (int i = 2; i * i <= num_to_check; ++i) {
if (num_to_check % i == 0) {
is_prime = false;
break;
}
}
}
if (is_prime) {
cout << num_to_check << " is a prime number." << std::endl;
} else {
cout << num_to_check << " is not a prime number." << std::endl;
}
return 0;
}
Result:
int main() {
int num;
long long factorial = 1;
if (num < 0) {
cout << "Factorial is not defined for negative numbers." << std::endl;
} else {
for (int i = 1; i <= num; ++i) {
factorial *= i;
}
cout << "Factorial of " << num << " is: " << factorial << std::endl;
}
return 0;
}
Result:
Q4: A program that takes units as input from the user, and unit rates are
assigned by the programmer and the program calculates the electricity bill.
(using if statement).
Code:
#include <iostream>
using namespace std;
int main() {
int units;
double unitRate;
double totalBill;
cout << "Enter the number of units consumed: ";
cin >> units;
if (units <= 50) {
unitRate = 0.50;
} else if (units <= 150) {
unitRate = 0.75;
} else if (units <= 250) {
unitRate = 1.20;
} else {
unitRate = 1.50;
}
totalBill = units * unitRate;
cout << "Electricity Bill Calculation:\n";
cout << "Units Consumed: " <<units<< " units\n";
cout << "Unit Rate: Rs. " << unitRate << " per unit\n";
cout << "Total Bill: Rs. " << totalBill<< endl;
return 0;}
Result:
Conclusion:
In the series of C++ programs provided, I
covered a range of fundamental programming
concepts.
Each program addressed a specific problem,
showcasing the use of variables, input/output
operations, loops, conditionals, and basic
algorithmic logic.
The programs included determining prime
numbers, calculating factorials, and calculating
electricity bills based on different unit rates.
The structure of the programs emphasized
readability and simplicity, promoting good
programming practices.
By incorporating if statements, the programs
demonstrated conditional execution, enabling
different branches of code based on specific
conditions.
Overall, these C++ programs serve as practical
illustrations of essential programming
constructs, suitable for learning and reinforcing
foundational programming skills.
These examples also highlighted the importance
of user input validation, ensuring the robustness
of the programs.
We have learned a lot from all these Programs
which made us use different Functions.