You are on page 1of 4

Name: Hafsah Ayub

Class: 1-B-BS(CS)
Enrollment No: 02-134191-032

LAB:08 Function-I
Exercise 1: Write a C++ Program that contains one user defined function month().
Source code:
#include<iostream>
using namespace std;
void month(int x);
int main()
{
int x;
cout<<"Enter an integer from 1 to 12 ";
cin>>x;
month(x);
return 0;
}
void month(int x)
{
if(x==1)
{
cout<<"The month is January ";
}
else if (x==2)
{
cout<<"The month is February ";
}
else if (x==3)
{
cout<<"The month is March ";
}
else if (x==4)
{
cout<<"The month is April ";
}
else if (x==5)
{
cout<<"The month is May ";
}
else if (x==6)
{
cout<<"The month is June ";
}
else if (x==7)
{
cout<<"The month is July ";
}
else if (x==8)
{
cout<<"The month is August ";
}
else if (x==9)
{
cout<<"The month is September ";
}
else if (x==10)
{
cout<<"The month is October ";
}
else if (x==11)
{
cout<<"The month is November ";
}
else if (x==12)
{
cout<<"The month is December ";
}
else
{
cout<<"default";
}
Return;
}
Output:

Exercise 2: Write a C++ Program that contains one user defined function
cal_grades().
Source code:
#include<iostream>
using namespace std;
char cal_grades(int x);
int main()
{
int x;
cout<<"Enter marks from 0-100 ";
cin>>x;
char grade = cal_grades(x);
int per=(x/100)*100;
cout<<"The grade is "<<grade;
return 0;
}
char cal_grades(int x)
{
char grade;
if(x>=85)
return 'A';
else if(x<=85 && x>=75)
return 'B';
else if(x<=75 && x>=50)
return 'C';
else if (x<50 && x>=30)
return 'D';
else
{
return 'Fail';
}
}
Output:

Exercise 3: Write a C++ Program that contains four user defined function(s)
addition(), subtraction(), division(),
multiplication().

Source code:
#include<iostream>
using namespace std;
int addition (int x, int y);
int subtraction (int x, int y);
float multiplication (float x, float y);
float division (float x, float y);
int main()
{
int a,b,c;
cout<<"Enter first number ";
cin>>a;
cout<<"Enter second number ";
cin>>b;
cout<<"Press 1 for Addition "<<endl;
cout<<"Press 2 for Subtraction "<<endl;
cout<<"Press 3 for Multiplication "<<endl;
cout<<"Press 4 for Division "<<endl;
cin>>c;
int add, sub;
float mul, div;
switch(c)
{
case 1:
add = addition (a, b);
cout<<"The sum of "<<a<<" and "<<b<<" is "<<add;
break;
case 2:
sub = subtraction (a, b);
cout<<"The difference of "<<a<<" and "<<b<<" is "<<sub;
break;
case 3:
mul = multiplication (a, b);
cout<<"Product of "<<a<<" and "<<b<<" is "<<mul;
break;
case 4:
div = division (a, b);
cout<<"Division of "<<a<<" and "<<b<<" is "<<div;
break;
default:
cout<<"Enter two integers ";
break;
}
return 0;
}
int addition (int x, int y)
{
return (x+y);
}
int subtraction (int x, int y)
{
return (x-y);
}
float multiplication (float x, float y)
{
return (x*y);
}
float division (float x, float y)
{
return (x/y);
}
Output: