0% found this document useful (0 votes)
7 views

Cp Lab 8 Function

Uploaded by

hayatlucky064
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
7 views

Cp Lab 8 Function

Uploaded by

hayatlucky064
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 7

Bahria University, Islamabad

Department of Software Engineering


Computer Programming Lab (Fall-2024)
Teacher: Engr. Muhammad Amin Khan

Student : Hayat Nabi


Enrollment : 09-131242-097

Lab Journal: 8
Date: 10 - 13 - 2024

Documentation
Task Wise Marks Total
Task Marks Marks
No:
Assigned Obtained Assigned Obtained (20)

1 3
2 3
3 3 5
4 3
5 3

Comments:

Signature
Hayat Nabi Computer Programming Engr. M Amin Khan
09-131242-097 Lab # 08 Dept of SE, BUIC

Lab No: 8 - Functions


Task 1:
Write a program that includes functions to convert temperatures between Celsius, Fahrenheit,
and Kelvin. Each conversion should have its own function that takes the initial temperature
and returns the converted value.
Code:
#include<iostream>
using namespace std;

double F , C , K;

double F_To_C(double F){ // Fah to Centi


C = (F - 32) * 5/9;
return C;
}
double C_To_F(double C){ // Centi to Fahr
F = (C * 9/5 ) + 32;
return F;
}
double C_To_K(double C){ // Centi to Kelvin
K = C + 273;
return K;
}
double F_To_K(double F){ // fahr to Kelvin
K = (F - 32) * 5/9 + 273;
return K;
}
double K_To_F(double K){ // Kelvin to fahr
F = (K - 273) * 9/5 + 32;
return F;
}
double K_To_C(double K){ // Kelvin to Centi
C = K - 273;
return C;
}
int main(){
cout<<"F -> C : "<< F_To_C(50) <<"'C" <<endl;
cout<<"F -> K : "<< F_To_K(50) <<"'K" <<endl;
cout<<"C -> F: "<< C_To_F(50) <<"'F" <<endl;
cout<<"C -> K : "<< C_To_K(50) <<"'K" <<endl;
cout<<"K -> F : "<< K_To_F(50) <<"'F" <<endl;
cout<<"K -> C : "<< K_To_C(50) <<"'C" <<endl;
}

2
Hayat Nabi Computer Programming Engr. M Amin Khan
09-131242-097 Lab # 08 Dept of SE, BUIC

Screenshot:

Task 2:
Implement two functions: one to find the maximum and another to find the minimum of three
user-input numbers. The program should call these functions and display the maximum and
minimum values.

Code:

#include<iostream>
using namespace std;

int a,b,c , Max_Number , Min_Number;


int Max(int a , int b , int c){
if(a>b && a > c){
Max_Number = a ;
}else if(b >a && b >c){
Max_Number = b;
}else{
Max_Number = c ;
}
return Max_Number;
}
int Min(int a , int b , int c){
if(a<b && a < c){
Min_Number = a ;
}else if(b <a && b <c){
Min_Number = b;
}else{
Min_Number = c ;
}
return Min_Number;
}
int main(){
cout<<"Enter First Number (Non-Negative ) :";
cin>>a;
cout<<"Enter Second Number (Non-Negative ) : ";

3
Hayat Nabi Computer Programming Engr. M Amin Khan
09-131242-097 Lab # 08 Dept of SE, BUIC

cin>>b;
cout<<"Enter Third Number (Non-Negative ) : ";
cin>>c;

cout<<"Max Number is : "<<Max(a,b,c) <<endl;


cout<<"Min Number is : "<<Min(a,b,c);
}

Screenshot:

Task 3:
Create a function that takes an integer as input and returns true if it is a prime number and
false otherwise. Write a main program that allows the user to input a number and uses this
function to display whether the number is prime.

Code:
#include<iostream>
using namespace std;
int N ;
bool Check;
bool Prime_Check(int Number ){
Check = true;
for(int i=2;i<N ;i++){
if(N % i == 0 )
Check = false ;
}
return Check;
}
int main(){
cout<<"Enter Any Positive Number to Check Prime or Not : ";
cin>>N;
if(Prime_Check(N) == 1){
cout<<"Prime Number ";

4
Hayat Nabi Computer Programming Engr. M Amin Khan
09-131242-097 Lab # 08 Dept of SE, BUIC

}else{
cout<<"Non - Prime Number ";
}
}

Screenshot:

Task 4:
Create a program that calculates the monthly payroll for employees in a company. Using
Functions ……..

Code:
#include<iostream>
using namespace std;

string Name ;
int Worked_Hours;
double Hourly_Rate , Gross_pay;

double GrossPay(int Worked_Hours , double Hourly_Rate ){


double Income = Worked_Hours * Hourly_Rate ;
double tax;
if(Income >0 && Income < 2000){
tax = 0.10;
}else if(Income >= 2000 && Income <= 5000){
tax = 0.15;
}else{
tax = 0.20;
}
Gross_pay = Income - (Income * tax);
return Gross_pay;
}
int main(){
char op;
do{

5
Hayat Nabi Computer Programming Engr. M Amin Khan
09-131242-097 Lab # 08 Dept of SE, BUIC

cout<<"Enter Employee Name : ";


cin>>Name;
cout<<"Enter Employee Working Hours : ";
cin>>Worked_Hours;
cout<<"Enter Employee Hourly Rate : $";
cin>>Hourly_Rate;
cout<<"Summary : \n";
cout<<"Employee Name : "<<Name <<endl <<"Gross - Pay : $"<< GrossPay(Worked_Hours
, Hourly_Rate) <<endl;
cout<<"\n-------------------------------------------------------------------------------------------------\
n";
cout<<"Do You want to Calculate another Gross Pay (Y / N) : ";
cin>>op;
}while(op == 'y' || op =='Y');
}

Screenshot:

Task 5:
Write a program that calculates the average grade and final grade for multiple students based
on several test scores Using Funtions……

Code:
#include<iostream>
using namespace std;
int T1 , T2 , T3;
double Average(int T1 ,int T2 ,int T3){
double Test_Average = (T1 + T2 + T3 ) / 3;
return Test_Average;
}

int main(){

6
Hayat Nabi Computer Programming Engr. M Amin Khan
09-131242-097 Lab # 08 Dept of SE, BUIC

cout<<"Enter Test 1 Marks : ";


cin>>T1;
cout<<"Enter Test 2 Marks : ";
cin>>T2;
cout<<"Enter Test 3 Marks : ";
cin>>T3;
cout<<"Average Of Tests : "<<Average(T1 ,T2 ,T3) <<endl ;
if(Average(T1 ,T2 ,T3) >=90 && Average(T1 ,T2 ,T3) <100 ){
cout<<"Grade : A ";
}else if(Average(T1 ,T2 ,T3) >=80 && Average(T1 ,T2 ,T3) <90){
cout<<"Grade : B ";
}else if(Average(T1 ,T2 ,T3) >=70 && Average(T1 ,T2 ,T3) <80){
cout<<"Grade : C ";
}else if(Average(T1 ,T2 ,T3) >=60 && Average(T1 ,T2 ,T3) <70){
cout<<"Grade : D ";
}else{
cout<<"Grade F ";
}
}

Screenshot:

You might also like