exercise 2 1. write c++ program to calculate the area and perimeter of a rectangle. note that the...

18
Exercise 2 Exercise 2 1

Upload: tyra-horsman

Post on 14-Dec-2015

228 views

Category:

Documents


1 download

TRANSCRIPT

Exercise 2Exercise 2

11

Write C++ program to calculate the area and perimeter of a Write C++ program to calculate the area and perimeter of a rectangle.rectangle.Note that the area of the rectangle = length × width.Note that the area of the rectangle = length × width.

Perimeter = 2*(length+width)Perimeter = 2*(length+width)

22

#include <iostream>using namespace std;main(){

int length, width;cout<<"length = ";

cin>>length; cout<<"width = "; cin>>width; cout<<"perimeter = "<<2*(length+width)<<"\n"; cout<<"area = "<<length*width;}

Write C++ Program to calculate and print the area of a right-angled triangle .Write C++ Program to calculate and print the area of a right-angled triangle .

The base of the triangle = aThe base of the triangle = a

The height of triangle= bThe height of triangle= b

Equation of area= (1/2)*a*bEquation of area= (1/2)*a*b

#include<iostream>using namespace std;int main(){ int a,b; cout<<“Please enter the base of triangle \n”; cin>>a; cout<<“Please enter the height of triangle \n”; cin>>b; cout<<“Area of triangle= “<<(0.5)*a*b<<endl; return 0;}

Write c++ program that print the following form: Write c++ program that print the following form:

\ Hello " C ' pp ' \ Hello " C ' pp '   

#include <iostream>#include <iostream>

using namespace std;using namespace std;

void main()void main()

{{

cout<<" cout<<" \\\\ Hello Hello \" \" C C \'\' pp pp \' \' "; ";

}}

Write a program to print your name in the first line in the Write a program to print your name in the first line in the left side, and the address in the middle of the third line, left side, and the address in the middle of the third line, and nationality in the right side in the same line.and nationality in the right side in the same line.

#include <iostream>#include <iostream>

using namespace std;using namespace std;

void main()void main()

{{

cout<<“Sara Mohamed \n\n\t\t\t";cout<<“Sara Mohamed \n\n\t\t\t";

cout<<“Al Kharj \t\t\t Saudi";cout<<“Al Kharj \t\t\t Saudi";

}}

Write c++ program to accept integer number from user and then Write c++ program to accept integer number from user and then calculate the area of a circle.calculate the area of a circle.Note: The equation of area circle = π * R2, where π is a constant Note: The equation of area circle = π * R2, where π is a constant value of approximately 3.14.value of approximately 3.14.

perimeter = 2 * PI * Rperimeter = 2 * PI * R

#include <iostream>Using namespace std;main(){

float r, PI; PI = 3.1415926; cout<<“ Please enter radius of circle \n”; cin>>r; cout<<"area = "<< PI * r * r<<"\n";

cout<<" perimeter = "<< 2 * PI * r;}

Write a Program that does the following:-Write a Program that does the following:-– Promotes the user to enter 5 numbers.Promotes the user to enter 5 numbers.– Print the Numbers.Print the Numbers.– Print the sum and average of these five numbers.Print the sum and average of these five numbers.

#include<iostream>#include<iostream>

using namespace std;using namespace std;

int main()int main()

{{

int a,b,c,d,e,sum;int a,b,c,d,e,sum;

float avg;float avg;

cout<<"Enter the five numbers: \n";cout<<"Enter the five numbers: \n";

cin>>a>>b>>c>>d>>e;cin>>a>>b>>c>>d>>e;

sum = a + b + c + d + e;sum = a + b + c + d + e;

avg = sum/5;avg = sum/5;

cout<<"the sum is "<<sum;cout<<"the sum is "<<sum;

cout<<" and the average is "<<avg<<endl;cout<<" and the average is "<<avg<<endl;

return 0;return 0;

}}

  

Write c++ program to find the Perimeter and area of the square.The perimeter and area of the square are given by the following formula.Perimeter = sideLength* 4Area = sideLength * sideLengthInput square sideLengthProcessing Area= sideLength * sideLength Perimeter= sideLength*4Output Print Out the perimeter and area#include <iostream>using namespace std;int main(){ int length, width; cout<<“Please enter length of square \n”; cin>>length; cin>>width; cout<<“Area= “<<length*length<<endl; cout<<“Perimeter= “<<4*length<<endl; return 0;}

Write c++ program to print y Write c++ program to print y If you now y isIf you now y is

  (3x-7) if (x=-5)(3x-7) if (x=-5)

(5x2) if (x=2) or (x=5)(5x2) if (x=2) or (x=5)

(x-4x3) if (x=-4) or (x=4)(x-4x3) if (x=-4) or (x=4)

#include <iostream.h>#include <math.h>main() {

int x, y;cout<<"x = "; cin>>x;switch(x)

{case -5: y=3*x-7; break;

 case 2:case 5: y=5*pow(x,2);

break;

case -4:case 4: y=x-4*pow(x,3);

break;}cout<<"y = "<< y;

}

Write a program that reads sides of the triangle (L1, L2, L3) and then a. Equilateral printed word in the case of equal ribs (L1 = L2 and L1 = L3 and L2 = L3) b. Isosceles printed word in the case of an isosceles (L1 = L2 or L1 = L3 or L2 = L3) c. Scalene printed word in the case of different ribs (L1!=L2 and L1!=L3 and L2!=L3)

#include <iostream>#using namespace std;main(){

float L1, L2, L3;cout<<"Enter L1, L2, L3 \n";

cin>>L1>>L2>>L3;if(L1==L2&&L1==L3&&L2==L3) cout<<"Equilateral";elseif(L1==L2||L1==L3||L2==L3) cout<<"Isosceles";elseif(L1!=L2&&L1!=L3&&L2!=L3) cout<<"Scalene";

}

Write the output of (x)

X = 3 * ( 8 - 6 ) + 4 *4

Write the output (x)

X = 8 – 3 * 2 + 4 / 2 ^ 2

X = X = 8 – 3 * 2 + 4 / 4

X = X = 8 – 6 + 4 / 4

X = X = 8 – 6 + 1

X = X = 22 + 1 + 1

X = 3X = 3

Write the output (x)

X = ( 6 - 1 ) * 2 – 7 + 4

X = 5 * X = 5 * 22 – 7 + 4 – 7 + 4

X = X = 10 – 7 + 4

X = X = 3 + 4

X = X = 77

Write the output (x)

X = 5 + 2 * 4 / 2

X = X = 5 + 8 / 2

X = X = 5 + 4

X = 9X = 9

Write the output (x)

X = ( 3 + 2 ) * 2 * 2

X = X = 55 * 2 * 2

X = X = 5 * 4

X = 20X = 20

What is the result of each compare A = 1 , B = 5

exampleexampleResult of compareResult of compare

A = BA = Bfalsefalse

B = A + 4B = A + 4 truetrue

A < > 1A < > 1 falsefalse

A < BA < Btruetrue

A < 0A < 0falsefalse

A < = 3A < = 3truetrue

B < = 3B < = 3falsefalse

A > BA > Bfalsefalse

A > 0A > 0truetrue

B > = 5B > = 5truetrue

B > = 6B > = 6falsefalse

What is the result of each compare A = 1 , B = 2 , C = 3

exampleexampleCompare resultCompare result

A + 4 = B + CA + 4 = B + Ctruetrue

A + B < C – 1A + B < C – 1falsefalse

A + 3 < > B * C - 1A + 3 < > B * C - 1 truetrue

) )B + 3B + 3 * ( * ( 2 > C * A - 12 > C * A - 1 truetrue