#include <iostream>
using namespace
std;
double rectArea (double l, double w);
int main()
{
double length, width, area;
// This
program calculates the area of a rectangle
cout << "Enter the length of the rectangle: ";
cin >> length;
cout << "Enter the width of the rectangle: ";
cin >> width;
area = rectArea(length,
width);
cout << "The area of the rectangle is " <<
area << endl;
return 0;
}
double rectArea (double l, double w)
{
return l * w;
}
1. Write
a C++ program with function qualityPoints that inputs a student’s
average and
returns
4 if a student’s average is 90-100, 3 if the average is 80-89, 2 if the average
is 70-79, 1 if the average is 60-69, and 0 if the
average is lower than 60.
2. Write
a complete C++ program that has three functions named as follows:
- int square(int, int) - calculate an area of square which the arguments are length and width (length*width).
- double triangle(double, double) – calculate an area of triangle which the arguments are height and base (0.5*height*base).
- double circle(double) – calculate an area of circle (3.14159 * (radius * radius))
which the argument is radius.
Your program is terminated when required by user. The input/output
should be look like below:
Calculate an area of:
1. Square
2. Triangle
3. Circle
Please give your choice [1/2/3]: 2
Enter height and base : 5 10
The area of triangle is 25.0
Continue [y/n]: y
Calculate an area of:
4. Square
5. Triangle
6. Circle
Please give your choice [1/2/3]: 1
Enter height and base : 10 10
The area of triangle is 100
Continue [y/n]: n
Thank you!
Hint: Use do while
loop and switch statement. Both of these statements work same as java.
LAB 2 – EX 1
#include<iostream>
usingnamespacestd;
doubleave (double l);
int main()
{
double aver=0, res=0;
cout<<"Enter
the Average: ";
cin>> aver;
res = ave( aver );
cout<<"the
average is "<< res <<endl;
}
doubleave (double l)
{
if ( 100 <=l || l >=90 ){
return 4;
}
elseif ( 89 <=l || l >=80){
return 3 ;
}
elseif ( 79 <=l || l >=70){
return 2;
}
elseif ( 69 <=l || l >=60){
return 1;
}
else
return 0;
}
------------------------------------------------------------------------------------------------------------------
LAB 2 – EX 2
#include<iostream>
usingnamespacestd;
int square(int, int);
double triangle(double, double);
double circle(double);
int main()
{
int choice ;
int length, width, area;
double h , b , a ;
double r;
char boll ;
// This program calculates the area of
a rectangle
do{
cout<<"Calculate
an area of: "<<endl;
cout<<"1.
Square"<<endl;
cout<<"2.
Triangles "<<endl;
cout<<"3.
Circle"<<endl;
cout<<"Please
give your choice [1/2/3]: ";
cin>> choice;
switch(choice)
{
case 1 :
cout<<"Enter the length of the rectangle: ";
cin>> length;
cout<<"Enter
the width of the rectangle: ";
cin>> width;
area = square(length, width);
cout<<"The area of the rectangle is "<<
area <<endl;
break;
case 2 :
cout<<"Enter
the hight of the rectangle: ";
cin>> h;
cout<<"Enter
the base of the rectangle: ";
cin>> b;
a = triangle(h, b);
cout<<"The area of the rectangle is "<<
a <<endl;
break;
case 3 :
cout<<"Enter the redius
";
cin>> r;
cout<<"The r is "<<circle(r)
<<endl;
break;
default :
cout<<"Invalid
"<<endl;
}
cout<<"Continue
[y/n]: "<<endl;
cin>> boll;
} while ( boll == 'Y' );
// return 0;
}
int square(int l, int w){
return l * w;
}
double triangle(double h,
double b){
return 0.5 * h *b;
}
double circle(double r){
return (3.14159 * (r * r));
}
ليست هناك تعليقات:
إرسال تعليق