7 Programs of PF
7 Programs of PF
#include <iostream>
using namespace std;
double fallingDistance(int time) {
double gravity = 9.8;
return 1/2* gravity * time * time; }
int main() {
for (int i = 1; i <= 10; ++i) {
cout<<"The object has fallen"<<fallingDistance(i)<<"meters
in"<<i<<"seconds."<<endl;
}
return 0;}
Kinetic Energy
#include <iostream>
using namespace std;
double kineticEnergy(double mass, double velocity) {
return 0.5 * mass * velocity * velocity;
}
int main() {
double mass, velocity;
cout << "Enter the mass of the object (in kilograms): ";
cin >> mass;
cout << "Enter the velocity of the object (in meters per second): ";
cin >> velocity;
cout << "The kinetic energy of the object is: " <<
kineticEnergy(mass,velocity)<<"joules." << endl;
return 0;
}
Celsius Temperature Table
#include <iostream>
#include <iomanip>
using namespace std;
double celsius(double fahrenheit) {
return (fahrenheit - 32.0) * 5.0 / 9.0;}
int main()
cout << "Fahrenheit \t Celsius" << endl;
cout << "-------------------------" << endl;
for (int f = 0; f <= 20; ++f) {
double c = celsius(f);
cout << fixed << setprecision(2);
cout << f << "\t\t" << c << endl; }
return 0;}
Present Value
#include <iostream>
using namespace std;
#include <cmath>
bool isPrime(int num) {
if (num <= 1) {
return false; }
for (int i = 2; i <= sqrt(num); ++i) {
if (num % i == 0) {
return false; }
}
return true;}
int main() {
int num;
std::cout << "Enter a number to check if it's prime: ";
std::cin >> num;
if (isPrime(num)) {
std::cout << num << " is a prime number." << std::endl;
} else {
std::cout << num << " is not a prime number." << std::endl; }
return 0;}
Lowest Score Drop
#include <iostream>
void getScore(int& score) {
do {
std::cout << "Enter a test score (0-100): ";
std::cin >> score;
} while (score < 0 || score > 100);}
void calcAverage(int scores[]) {
int lowestIndex = findLowest(scores);
int sum = 0;
for (int i = 0; i < 5; ++i) {
if (i != lowestIndex) {
sum += scores[i]; }
} double average = static_cast<double>(sum) / 4;
std::cout << "Average of the four highest scores: " << average <<
std::endl;
}
int findLowest(int scores[]) {
int lowestIndex = 0;
for (int i = 1; i < 5; ++i) {
if (scores[i] < scores[lowestIndex]) {
lowestIndex = i; }
}return lowestIndex;}
int main() {
int scores[5];
for (int i = 0; i < 5; ++i) {
getScore(scores[i]);
}
calcAverage(scores);
return 0;
}
Stock Profit
#include <iostream>
using namespace std;
double calculateProfitLoss(int NS, double PP, double PC, double SP, double
SC);
int main() {
int NS;
double PP, PC,SP, SC , profitLoss;
cout << "Enter the number of shares: ";
cin >> NS;
cout << "Enter the purchase price per share: ";
cin >> PP;
cout << "Enter the purchase commission paid: ";
cin >> PC;
cout << "Enter the sale price per share: ";
cin >> SP;
cout << "Enter the sale commission paid: ";
cin >> SC;
profitLoss = calculateProfitLoss(NS, PP, PC, SP, SC);
if (profitLoss > 0) {
cout << "The sale of the stock resulted in a profit of $" << profitLoss
<< endl;
} else if (profitLoss < 0) {
cout << "The sale of the stock resulted in a loss of $" << -profitLoss
<< endl;
} else {
cout << "There was no profit or loss from the sale of the stock." <<
endl;}
return 0;}
double calculateProfitLoss(int NS, double PP, double PC, double SP, double
SC) {
double profit = ((NS * SP) - SC) - ((NS * PP) + PC);
return profit;}
Population
#include <iostream>
using namespace std;
// Function prototype
double calculatePopulation(int P, double B, double D);
int main() {
int P; // Starting size of the population
double B; // Annual birth rate
double D; // Annual death rate
int years;
cout << "Enter the starting size of the population (must be at least
2): ";
cin >> P;
while (P < 2) {
cout << "Invalid input. Enter a number greater than or equal to 2: ";
cin >> P; }
cout << "Enter the annual birth rate (as a percentage, e.g., 0.05
for 5%): ";
cin >> B;
while (B < 0) {
cout << "Invalid input. Enter a non-negative number for the birth
rate: ";
cin >> B; }
cout << "Enter the annual death rate (as a percentage, e.g., 0.05 for
5%): ";
cin >> D;
while (D < 0) {
cout << "Invalid input. Enter a non-negative number for the death
rate: ";
cin >> D; }
cout << "Enter the number of years to display (must be at least 1): ";
cin >> years;
while (years < 1) {
cout << "Invalid input. Enter a number greater than or equal to 1: ";
cin >> years;
}
cout << "Year\tPopulation" << endl;
cout << "-----------------" << endl;
for (int i = 0; i <= years; i++) {
cout << i << "\t" << P << endl;
P = calculatePopulation(P, B, D);
} return 0;
}double calculatePopulation(int P, double B, double D) {
return P * (1 + B) * (1 - D);}