You are on page 1of 10

CSL-113 : Computer Programming Lab

Semester : BS CS – 1A
Name : Muhammad Zain
Enroll no : (02-134182-037)

LAB 8

EXERCISE 1
Write a C++ Program that contains one user defined function month().

 In main() function:
o Read an integer input in between (1 to 12) and store
it month_of_year.
o Call month(month_of_year)  In month() function:
o Print the corresponding month of year in month(). o
Example: Value of parameter is 4… Print “April”.

SOURCE CODE
#include<iostream>
using namespace std;

int month(int month_of_year)


{
// int month_of_year;
//cout<<"Enter a number from 1-12 : ";
//cin>>month_of_year;
if (month_of_year ==1)
cout<<"January";
else if (month_of_year==2)
cout<< "February";
else if (month_of_year==3)
cout<<"March";
else if (month_of_year==4)
cout<<"April";
else if (month_of_year==5)
cout<<"May";

Department of computer science Semester BSCS-1A


CSL-113: Computer Programming Lab Lab 08: Funcion-1
CSL-113 : Computer Programming Lab
Semester : BS CS – 1A
Name : Muhammad Zain
Enroll no : (02-134182-037)

else if (month_of_year==6)
cout<<"June";
else if (month_of_year==7)
cout<<"July";
else if (month_of_year==8)
cout<<"August";
else if (month_of_year==9)
cout<<"September";
else if (month_of_year==10)
cout<<"October";
else if (month_of_year==11)
cout<<"November";
else if (month_of_year==12)
cout<<"December";
else if (month_of_year>12)
cout<<"Sorry I need a number from 1-12."<<endl;
else if(month_of_year<=12)
cout<< "month of year "<<month_of_year;

int main ()
{
int n ,month_of_year;
cout<<"enter number between 1 to 12 : ";
cin>>n;
cout<<"\n";

month_of_year=n;
Department of computer science Semester BSCS-1A
CSL-113: Computer Programming Lab Lab 08: Funcion-1
CSL-113 : Computer Programming Lab
Semester : BS CS – 1A
Name : Muhammad Zain
Enroll no : (02-134182-037)

month(month_of_year);

OUTPUT

Department of computer science Semester BSCS-1A


CSL-113: Computer Programming Lab Lab 08: Funcion-1
CSL-113 : Computer Programming Lab
Semester : BS CS – 1A
Name : Muhammad Zain
Enroll no : (02-134182-037)

EXERCISE 2
Write a C++ Program that contains one user defined function cal_grades().

• In main() function:
o Prompt user to enter obtained(0 - 100) marks for one subject. o Call
cal_grades(marks_subject).
o Print the corresponding Grade with respect to Marks.
• In user defined function:
o Perform conditioning with else if statement return char value. o Function
must return value.

SOURCE CODE
#include<iostream>
using namespace std;

char cal_grades(int marks_subject)


{
/*receives an marks_subject out of 100 from main and determines what letter grade to
return */
if (marks_subject >= 90 && marks_subject <= 100)
return 'A';
else if (marks_subject >= 80 && marks_subject <= 89)
return 'B';
else if (marks_subject >= 70 && marks_subject <= 79)
return 'C';
else if (marks_subject >= 65 && marks_subject <= 65)
return 'D';

Department of computer science Semester BSCS-1A


CSL-113: Computer Programming Lab Lab 08: Funcion-1
CSL-113 : Computer Programming Lab
Semester : BS CS – 1A
Name : Muhammad Zain
Enroll no : (02-134182-037)

else
return 'F';
}

int main()
{
int n,marks_subject;
cout<<"enter marks : ";
cin>>n;

marks_subject=n;

cout<<"\nyour grade is "<<cal_grades(marks_subject);


}
OUTPUT

Department of computer science Semester BSCS-1A


CSL-113: Computer Programming Lab Lab 08: Funcion-1
CSL-113 : Computer Programming Lab
Semester : BS CS – 1A
Name : Muhammad Zain
Enroll no : (02-134182-037)

EXERCISE 3

Write a C++ Program that contains four user defined function(s) addition(), subtraction(), division(),
multiplication(). Develop a calculator as follows

• In main() function:
o A menu with choices addition,subtraction,division and multiplication must be
displayed. o Get two numbers and a choice from user o Call the
respective functions with user given number as parameter using switch statement
o Print the result from addition(), subtraction, division(), multiplication().
• In user defined functions:
o Plus and Minus function get two integer values and return integer. o Multiply
and Divide functions get two integer values and return float.
SOURCE CODE
#include <iostream>
using namespace std;

int add(int n1,int n2)


{
int add;
// cout<<"\n\t\taddition\n\n";
cout<<"enter first number : ";
cin>>n1;
cout<<"enter second number : ";
cin>>n2;
cout<<"\n\n";
add = n1+n2;
return add;
Department of computer science Semester BSCS-1A
CSL-113: Computer Programming Lab Lab 08: Funcion-1
CSL-113 : Computer Programming Lab
Semester : BS CS – 1A
Name : Muhammad Zain
Enroll no : (02-134182-037)

int sub(int n1,int n2)


{
int sub;
cout<<"\n\t\tsubtraction\n\n";
cout<<"enter first number : ";
// cin>>n1;
cout<<"enter second number : ";
// cin>>n2;
cout<<"\n\n";
sub =n1-n2;
return sub;
}

int multiply(int n1,int n2)


{
int mul;
cout<<"\n\t\tmulitiplication\n\n";
cout<<"enter first number : ";
cin>>n1;
cout<<"enter second number";
cin>>n2;
cout<<"\n\n";
Department of computer science Semester BSCS-1A
CSL-113: Computer Programming Lab Lab 08: Funcion-1
CSL-113 : Computer Programming Lab
Semester : BS CS – 1A
Name : Muhammad Zain
Enroll no : (02-134182-037)

mul =n1*n2;
return mul;
}

int divi(int n1,int n2)


{
int div;
cout<<"\n\t\tdivision\n\n";
cout<<"enter first number : ";
cin>>n1;
cout<<"enter second number : ";
cin>>n2;
cout<<"\n\n";
div =n1/n2;
return div;o
}

int main()

{
int n,n1,n2;

cout<<"\t\tmenu";

Department of computer science Semester BSCS-1A


CSL-113: Computer Programming Lab Lab 08: Funcion-1
CSL-113 : Computer Programming Lab
Semester : BS CS – 1A
Name : Muhammad Zain
Enroll no : (02-134182-037)

cout<<"\n\nenter 1 for addition"<<"\nenter 2 for


subtration"<<"\nenter 3 for multiplication"<<"\nenter 4 for division\n\n";
cin>>n;

switch(n)
{
case 1 :
cout<<add(n1,n2);
break;
case 2 :
cout<<sub(n1,n2);
break;
case 3 :
multiply(n1,n2);
break;
case 4 :
divi(n1,n2);
break;
default :
cout<<"invalid press";
}
}

Department of computer science Semester BSCS-1A


CSL-113: Computer Programming Lab Lab 08: Funcion-1
CSL-113 : Computer Programming Lab
Semester : BS CS – 1A
Name : Muhammad Zain
Enroll no : (02-134182-037)

OUTPUT

Department of computer science Semester BSCS-1A


CSL-113: Computer Programming Lab Lab 08: Funcion-1

You might also like