In: Computer Science
Create the appropriate variables and algorithm to calculate the following. Choose your own values for the variables. Do not enter the code on this paper. Create a program that calculates and displays the calculation. Approximate pi (π) as 3.14159.
You do not need to create a new program for each question.
If certain math equations use the same variable name, then you only need to declare it once in your main function.
Separate each problem using comments, like seen in the example below.
Example 1: Area of a square/rectangle. Formula:
int main()
{
// Example 1
float area, length, width;
length = 20;
width = 5;
area = length * width;
cout << "The area of a square with length " << length << " and width "
<< width << " is " << area << " square units.";
// Problem 1
// Problem 2
return 0;
}
Source Code::
#include<bits/stdc++.h>
using namespace std;
int main()
{
float
area,length,width,height,base,radius,diagonal1,diagonal2,smallbase,volume,edge,perimeter;
float
x1,x2,y1,y2,value,midpoint,symmetry,x,y,a,b,sum,difference;
//area of rectangle and square
length=20;width=5;
area=length*width;
cout<<"The area of rectangle with length
"<<length<<" and width "<<width<<" is
"<<area<<" square units.\n";
length=width=20;
//square
area=length*width;
cout<<"The area of square with length
"<<length<<" and width "<<width<<" is
"<<area<<" square units.\n";
//area of triangle
height=10;base=10;
area=(1/2)*height*base;
cout<<"The area of triangle with base
"<<base<<" and height "<<height<<" is
"<<area<<" square units.\n";
//area of circle
radius=10;
area=3.14159*radius*radius;
cout<<"The area of circle with radius
"<<radius<<" is "<<area<<" square
units.\n";
//area of rhombus
diagonal1=10;diagonal2=10;
area=(1/2)*diagonal1*diagonal2;
cout<<"The area of rhombus with diagonals
"<<diagonal1<<" and "<<diagonal2<<" is
"<<area<<" square units.\n";
//area of trapezoid
base=30;height=50;
area=(base+smallbase)*height/2;
cout<<"The area of trapezoid with base
"<<base<<" and height "<<height<<" and
smallbase "<<smallbase<<" is "<<area<<"
square units.\n";
//area of parallelogram
base=100;height=50;
area=base*height;
cout<<"The area of parallelogram with base
"<<base<<" and height "<<height<<" is
"<<area<<" square units.\n";
//volume of sphere
radius=10;
volume=(4/3)*3.14159*radius*radius*radius;
cout<<"The volume of sphere with radius
"<<radius<<" is "<<volume<<" cubic
units.\n";
//volume of cone
radius=100;height=50;
volume=3.14159*radius*radius*height/3;
cout<<"The volume of cone with radius
"<<radius<<" and height "<<height<<" is
"<<volume<<" cubic units.\n";
//volume of cube
edge=10;
volume=edge*edge*edge;
cout<<"The volume of cube with edge
"<<edge<<" is "<<volume<<" cubic
units.\n";
//volume of cylinder
radius=10;height=10;
volume=3.14159*radius*radius*height;
cout<<"The volume of cylinder with radius
"<<radius<<" and height "<<height<<" is
"<<volume<<" cubic units.\n";
//volume of square pyramid
edge=10;height=20;
volume=edge*edge*height/3;
cout<<"The volume of square pyramid with
base edge "<<edge<<" and height
"<<height<<" is "<<volume<<" cubic
units.\n";
//volume of pyramid
length=10;width=10;height=10;
volume=length*width*height/3;
cout<<"The volume of pyramid with length
"<<length<<" and height "<<height<<" and
width "<<width<<" is "<<volume<<" cubic
units.\n";
//perimeter of rectangle
length=10;width=10;
perimeter=2*(length+width);
cout<<"The perimeter of rectangle with
lenght "<<length<<" and width "<<width<<"
is "<<perimeter<<" units\n";
//midpoint
x1=1;x2=2;y1=2;y2=3;
cout<<"The midpoint is at
("<<(x1+x2)/2<<" ,
"<<(y1+y2)/2<<")\n";
//Absolute value
value=-100;
cout<<"The Absolute value of value
"<<value<<" is "<<fabs(value)<<"
unit\n";
//slope
x1=1;x2=2;y1=2;y2=3;
cout<<"The slope is
"<<(y2-y1)/(x2-x1)<<endl;
//symmetry of parabola
b=10;a=10;
symmetry=(-b)/(2*a);
cout<<"The symmetry of parabola is
"<<symmetry<<endl;
//sum and difference of cube
x=10;y=10;
sum=(x+y)*(x*x-x*y+y*y);
difference=(x-y)*(x*x+x*y+y*y);
cout<<"The sum of cube with x
"<<x<<" and y "<<y<<" is
"<<sum<<" cubic units\n";
cout<<"The difference of cube with x
"<<x<<" and y "<<y<<" is
"<<difference<<" cubic units\n";
return 0;
}
OUTPUT::