0% found this document useful (0 votes)
5 views6 pages

IICT Lab Task Week 14

The document presents a series of C++ programming tasks submitted by Saad Ishfaq, covering fundamental concepts such as calculating the sum of natural numbers, checking for prime numbers, finding factorials, and computing electricity bills based on unit consumption. Each task includes code snippets and results, demonstrating the use of variables, loops, conditionals, and user input validation. The conclusion emphasizes the importance of readability, simplicity, and good programming practices in learning foundational programming skills.

Uploaded by

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

IICT Lab Task Week 14

The document presents a series of C++ programming tasks submitted by Saad Ishfaq, covering fundamental concepts such as calculating the sum of natural numbers, checking for prime numbers, finding factorials, and computing electricity bills based on unit consumption. Each task includes code snippets and results, demonstrating the use of variables, loops, conditionals, and user input validation. The conclusion emphasizes the importance of readability, simplicity, and good programming practices in learning foundational programming skills.

Uploaded by

unknownusert805
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 6

Introduction to Information and

Communication Technology
LAB Task 14

Submitted by: Saad Ishfaq.


Registration number: CS-23-196.
Class: BSCS-Fall-23-section D.
Department: BS (COMPUTER SCIENCE).
Submitted to: Mr.Asif
Qno1: Write a program in C++ to find the sum of first 10 natural numbers.
 Code:
#include<iostream>
using namespace std;
int main(){
int n=10;
int sum=0;
for(int i=1;i<=n;i++){
sum+=i;
}
cout<<"The sum of first 10 natural numbers is :"<<sum<<endl;
return 0;
}

 Result:

Qno2: Write a program in C++ to check whether a number is prime or not.


 Code:
#include <iostream>
using namespace std;

int main() {
int num_to_check;
bool is_prime = true;

cout << "Enter a number: ";


cin >> num_to_check;

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:

Qno3: Write a program in C++ to find the factorial of a number.


 Code:
#include <iostream>
using namespace std;

int main() {
int num;
long long factorial = 1;

std::cout << "Enter a number: ";


std::cin >> num;

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.

You might also like