0% found this document useful (0 votes)
8 views14 pages

PF Assignment Muhammad Ali Huzaifa(242690)

The document contains multiple programming assignments in C++, covering topics such as Fibonacci series, BMI calculation, park ticket pricing, gross pay calculation, car loan monthly installments, and star pattern generation. Each section includes code snippets and explanations of the logic used to implement the respective functionalities. The assignments are structured with a main function and allow for user interaction through input prompts and loops.

Uploaded by

huzefa.ali.m
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)
8 views14 pages

PF Assignment Muhammad Ali Huzaifa(242690)

The document contains multiple programming assignments in C++, covering topics such as Fibonacci series, BMI calculation, park ticket pricing, gross pay calculation, car loan monthly installments, and star pattern generation. Each section includes code snippets and explanations of the logic used to implement the respective functionalities. The assignments are structured with a main function and allow for user interaction through input prompts and loops.

Uploaded by

huzefa.ali.m
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/ 14

[1]

[Assignment#01]
[Muhammad Ali Huzaifa]
[242690]
[Programming Fundamentals]
[Dr. Faheem Ullah]
[October 18, 2024]

Question 1
Fibonacci series
#include<iostream>

using namespace std;

int main(){

int n = 20; // Given, N=20

int f1=0, f2=1, f3;

cout<<f1<<", "<<f2<<", ";

for(int i=1; i<=18; i++) // for loop to print next 18 terms

f3=f2+f1;

cout<<f3<<", ";

f1=f2; // Update f1 to the previous f2

f2=f3; // Update f2 to the current term

return 0;

}
[2]

Question 2
BMI Calculator
#include<iostream>

using namespace std;

void bmiCal(){ //void function to calculate bmi

double w, h, s, b; //w for weight, h for height, s for height squared, b for BMI

cout<<"Enter your Weight (in kgs)"<<endl;

cin>>w;

cout<<"Enter your Height (in meters)"<<endl;

cin>>h;

s=h*h; // Calculate the square of height

b=w/s; // BMI calculation

cout<<"Your BMI is: "<<b<<endl;

if(b<18.5 && b>0){

cout<<"You are Underweight"<<endl;

}
[3]

else if(b==18.5 || b<24.9){

cout<<"You are Healthy"<<endl;

else if(b==25 || b<29.9){

cout<<"You are Overweight"<<endl;

else if(b>=30){

cout<<"You are Obese"<<endl;

else{

cout<<"Invalid Info"<<endl;

int main(){

char choice;

do{

bmiCal();

cout<<"Do you want to restart again? .....type (y/n)"<<endl;

cin>>choice; //nested do-while loop

while(choice != 'y' && choice != 'n' && choice != 'Y' && choice != 'N'){

cout<<"Invalid Info...Please type (y/n)"<<endl;

cin>>choice;

while(choice == 'y' || choice ==’Y’); //to restart if user enters y

cout<<"Existing the program"<<endl;

return 0;
[4]

Question 3
Park Ticket Price
#include<iostream>
#include<fstream>
using namespace std;

int main() {
int age;
int ticketprice;
char choice;

do {
// Ask for visitor's age
cout << "Enter age of the visitor: ";
cin >> age;

// Determine ticket price using switch


switch(age) {
case 0 ... 2:
ticketprice = 0;
break;
case 3 ... 11:
ticketprice = 5;
break;
case 12 ... 14:
[5]

ticketprice = 10;
break;
case 15 ... 18:
ticketprice = 15;
break;
default:
ticketprice = 20;
break;
}

// Display the ticket price


cout << "Ticket price for visitor aged " << age << " is Rs. " << ticketprice
<< endl;

// Open file to save ticket price (append mode)


ofstream outfile("tickets.txt", ios::app);
if (!outfile) {
cout << "Error opening file." << endl;
return 1; // Exit program on file error
}

// Write to file
outfile << "Visitor's age: " << age << ", Ticket price: Rs. " << ticketprice
<< endl;
outfile.close(); // Close file

// Ask if user wants to continue


cout << "Do you want to continue again? (y/n): ";
cin >> choice;

// Input validation for 'y' or 'n'


while (choice != 'y' && choice != 'n' && choice != 'Y' && choice != 'N') {
cout << "Invalid input. Please enter 'y' or 'n': ";
cin >> choice;
}

} while (choice == 'y' || choice == 'Y'); // Continue if user enters 'y' or 'Y'

cout << "Exiting the program..." << endl;

return 0;
}
[6]

Question 4
Gross Pay
#include<iostream>

using namespace std;

void grossPay(){

int h = 40; //40 Hours

double r; //hourly rate

double t; //Total Hours

int i; //Extra Hours

t=h+i;

cout<<"Enter no. of hours the employee worked: ";

cin>>t;

i=t-h; // Calculate overtime hours

cout<<"Enter hourly rate of employee: ";

cin>>r; // Calculate gross pay: Regular pay for first 40 hours + 1.5x
pay for overtime

cout<<"Your GrossPay is: Rs "<<h*r + i*(r+r/2)<<endl;

int main(){

char choice;

do{

grossPay(); //call the grossPay function


[7]

cout<<"Do you want to restart the program?....type (y/n)"<<endl;

cin>>choice;

while(choice != 'y' && choice != 'n' && choice != 'Y' && choice != 'N'){

cout<<"Invalid Info...Please type 'Y/y' or 'N/n'"<<endl;

cin>>choice;

while(choice == 'Y' || choice == 'y');

cout<<"Exiting the program"<<endl;

return 0;

Question 5
Car Loan
#include<iostream>

using namespace std;

void monthlyInst(){

double p; //vehicle price

double d; //down payment


[8]

double m; //months

double a; //payable amount

double n; //new payable amount

double i; //interest rate

double z; //monthly installments

cout<<"Enter the vehicle price(in rupees): ";

cin>>p;

cout<<"Enter the down payment(in rupees): ";

cin>>d;

cout<<"No. of months, you want to lease the car: ";

cin>>m; // Set interest rate

if(m<=12){

i=0.10;

else if(m>12 && m<=24){

i=0.15;

else if(m>24 && m<=36){

i=0.20;

else if(m>36 && m<=48){

i=0.25;

else if(m>48 && m<=60){

i=0.30;

else{

cout<<"No. of months cannot be more than 60...Please Enter Valid Info"<<endl;


[9]

a = p - d; // Calculate the amount payable after down payment

n = a + (i * a); // Add interest to the payable amount

z = n / m; // Calculate the monthly installment

cout<<"Your monthly installment is: Rs "<<z<<endl;

int main(){

char choice;

do{

monthlyInst(); // Call the monthlyInst function

cout<<"Do you want to Restart the program?...Type 'Y/y' or 'N/n'"<<endl;

cin>>choice;

while(choice != 'y' && choice != 'n' && choice != 'Y' && choice != 'N'){

cout<<"Invalid Info...Please type 'Y/y' or 'N/n'"<<endl;

cin>>choice;

while(choice == 'y' || choice == 'Y');

cout<<"Exiting the Program"<<endl;

return 0;

}
[10]

Question 6
Patterns of Stars

A.
#include<iostream>

using namespace std;

int main(){ // Outer loop controls the number of rows

for(int i = 1; i<=10; i++){

for(int j = 1; j <=i; j++){ // Inner loop prints '*' for


each column

cout<<"*";

cout<<endl; // Move to the next line after printing


each row

return 0;

B. #include<iostream>
using namespace std;

int main() {

// Outer loop controls the number of rows

for (int i = 1; i <= 10; i++) {

for (int j = 1; j <= 10 - i; j++) {

cout << " ";

} // Inner loop prints '*' for each column


[11]

for (int j = 1; j <= i; j++) {

cout << "* ";

cout << endl; // Move to the next line after printing each row
}

return 0;

C.
#include<iostream>

using namespace std;

int main(){

// Outer loop controls the number of rows

for(int i=1; i<=10; i++){

for(int j=1; j<=10; j++){

if( i + j >= 11){

cout<<" ";

else{

cout<<" *"; // Inner loop prints '*'

cout << endl; // Move to the next line after printing each row

return 0;

}
[12]

D.
#include<iostream>

using namespace std;

int main() { // Outer loop controls the number of rows

for (int i = 10; i >= 1; i--) {

for (int j = 1; j <= 10 - i; j++) {

cout << " ";

for (int j = 1; j <= i; j++) {

cout << "* "; } // Inner loop prints '*'

cout << endl; } // Move to the next line after printing each row

return 0;

E.
#include<iostream>

using namespace std;

int main(){

for(int i = 1; i<=10; i++){ // Outer loop controls the


number of rows

for(int j = 1; j<=(10-i); j++){

cout<<" ";

for(int k =1; k<=i; k++){

cout<<"* "; // Inner loop prints '*'


[13]

cout<<endl;

} // Move to the next line after printing each row

return 0;

F.
#include<iostream>

using namespace std;


int main(){
for(int i = 1; i<=10; i++){ // Outer loop controls the
number of rows
for(int j = 1; j<=i; j++){

cout<<" ";

for(int k =1; k<=(10-i); k++){

cout<<"* "; // Inner loop prints '*'


}

cout<<endl; // Move to the next line after printing each row


}

return 0;

G.
#include<iostream>

using namespace std;

int main(){ // Outer loop controls the number of rows

for(int i = 1; i<=7; i++){

for(int j = 1; j<=(7-i); j++){

cout<<" ";
[14]

for(int k =1; k<=i; k++){

cout<<"* "; // Inner loop prints '*'


}

cout<<endl;

for(int i = 1; i<=7; i++){

for(int j = 1; j<=i; j++){

cout<<" ";

for(int k = 1; k<=(7-i); k++){

cout<<"* ";} // Inner loop prints '*'

cout<<endl; // Move to the next line after printing each row

return 0;

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy