Computer Programming Lab 8
Lab 08: Functions – I
* 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”.
Task 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>
#include <iomanip>
using namespace std;
month (int month_of_year)
{
switch(month_of_year)
{
case
1:
{
cout<<"January"<<endl;
break;
}
case
2:
{
cout<<"Febuary"<<endl;
break;
}
case
3:
{
cout<<"March"<<endl;
break;
}
case
4:
{
cout<<"April"<<endl;
break;
}
case
5:
{
cout<<"May"<<endl;
break;
}
case
6:
{
cout<<"June"<<endl;
break;
}
case
7:
{
cout<<"July"<<endl;
break;
}
case
8:
{
cout<<"August"<<endl;
break;
}
case
9:
{
cout<<"September"<<endl;
break;
}
case
10:
{
cout<<"October"<<endl;
break;
}
case
11:
{
cout<<"November"<<endl;
break;
}
case
12:
{
cout<<"December"<<endl;
break;
}
default:
cout<<"Error!
Try Again"<<endl;
}
}
int main()
{
int
X,month_of_year;
cout<<setw(35)<<setfill('=')<<"="<<endl;
cout<<"Enter
a number between 1-12"<<endl;
cin>>X;
cout<<setw(35)<<setfill('=')<<"="<<endl;
month_of_year=X;
month(month_of_year);
cout<<setw(35)<<setfill('=')<<"="<<endl;
return
0;
}
Result:
Task 2:
Source Code:
#include <iostream>
#include <iomanip>
using namespace std;
char grades(int marks_subject)
{
if(marks_subject>=90&&marks_subject<=100)
{
cout<<"your
marks are "<<marks_subject<<" and grade is
A"<<endl;
}
else if
(marks_subject<90&&marks_subject>=80)
{
cout<<"your
marks are "<<marks_subject<<" and grade is
B+"<<endl;
}
else if
(marks_subject<80&&marks_subject>=70)
{
cout<<"your
marks are "<<marks_subject<<" and grade is
B"<<endl;
}
else if
(marks_subject<70&&marks_subject>=60)
{
cout<<"your
marks are "<<marks_subject<<" and grade is
C"<<endl;
}
else if
(marks_subject<60&&marks_subject>=50)
{
cout<<"your
marks are "<<marks_subject<<" and grade is
D"<<endl;
}
else if
(marks_subject<50&&marks_subject>=0)
{
cout<<"your
marks are "<<marks_subject<<" and grade is
F"<<endl;
}
else
{
cout<<"You
are topper! just kidding...Error"<<endl;
}
return
0;
}
int main()
{
int
marks_subject;
cout<<setw(43)<<setfill('=')<<"="<<endl;
cout<<"Enter Obtained(0 - 100) Marks for One
Subject"<<endl;
cin>>marks_subject;
cout<<setw(43)<<setfill('=')<<"="<<endl;
grades(marks_subject);
cout<<setw(43)<<setfill('=')<<"="<<endl;
return 0;
}
Result:
Task 3:
Source Code:
#include <iostream>
#include <iomanip>
using namespace std;
int addition(int add,int x,int y,char choice)
{
if(choice=='+')
{
add=x+y;
cout<<x<<"+"<<y<<"
= "<<add<<endl;
}
return
add;
}
int subbtraction(int sub, int x, int y,char choice)
{
if(choice=='-')
{
sub=x-y;
cout<<x<<"+"<<y<<"
= "<<sub<<endl;
}
return
sub;
}
float division (float x, float y, float div,char choice)
{
if(choice=='/')
{
div=x/y;
cout<<x<<"/"<<y<<"
= "<<div<<endl;
}
return
div;
}
float multiplication(float x, float y, float mul,char
choice)
{
if(choice=='*')
{
mul=x*y;
cout<<x<<"*"<<y<<"
= "<<mul<<endl;
}
return
mul;
}
int main()
{
int
x,y,add,sub;
char
choice;
float
mul,div;
cout<<setw(43)<<setfill('=')<<"="<<endl;
cout<<"
Calculator"<<endl;
cout<<setw(43)<<setfill('=')<<"="<<endl;
cin>>x;
cin>>y;
cout<<setw(43)<<setfill('=')<<"="<<endl;
cout<<"Enter '+ for Additon"<<endl;
cout<<"Enter '-' for
Subbtraction"<<endl;
cout<<"Enter '/' for divison"<<endl;
cout<<"Enter '*' for multiplication"<<endl;
cout<<setw(43)<<setfill('=')<<"="<<endl;
cin>>choice;
cout<<setw(43)<<setfill('=')<<"="<<endl;
addition(add,x,y,choice);
subbtraction(sub,x,y,choice);
division(x,y,div,choice);
multiplication(x,y,mul,choice);
cout<<setw(43)<<setfill('=')<<"="<<endl;
return 0;
}
Result:
Task 4:
Source
Code:
#include <iostream>
#include <iomanip>
using namespace std;
my_power(int base, int power,int ans=1)
{
for(int
i=0;i<power;i++)
{
ans=ans*base;
}
cout<<"Result
of Power "<<power<<" and Base
"<<base<<" = "<<ans<<endl;
return
ans;
}
int main()
{
int
base,power,ans=1;
//Enter
Base
cout<<setw(43)<<setfill('=')<<"="<<endl;
cout<<"Enter
Any Base:";
cin>>base;
cout<<setw(43)<<setfill('=')<<"="<<endl;
//enter
power
cout<<"Enter
Any Power: ";
cin>>power;
cout<<setw(43)<<setfill('=')<<"="<<endl;
my_power(base,power,ans=1);
cout<<setw(43)<<setfill('=')<<"="<<endl;
return 0;
}
Comments
Post a Comment