Sie sind auf Seite 1von 18

INDEPENDENT UNIVERSITY, BANGLADESH

DEPARTMENT OF COMPUTER SCIENCE & ENGINEERING


SUMMER 2020
ASSIGNMENT 2
DUE: 09-08-2020, 11.59 PM.
TOPIC: DECISION STATEMENTS
POINT: 100P (10X10:100P)
Name: Syed M Ateef Hassan Id: 2020374 Sec: 14 Point:

Instructions:
1. Plagiarism will not be tolerated.
2. Do not copy from any online source or from a person.
3. If you could not do any problem, leave it blank.
4. Partial marks will be given for partially solved solutions.
5. Do not forget to fill your name, id and section. Please leave
the point block as it is. Failure to fill up your name, id and
section will invalid the whole assignment.
6. You also have to rename the assignment file with your id. For
instance, the file name is “ID_ _ Assignment 2 Summer 2020” and
your id is 12345. You have to rename it as “ID_12345_Assignment 2
Summer 2020”. Unable to follow this instruction may lead to
cancel your assignment.

Problem Set:

1.Take a number form the user and check whether the number is
positive, negative or neither.

2.Write program that takes a number as input and checks whether


it is an integer or floating point number.
3.Writer a program that checks if a number is within the range
(35,75) and divisible by 5 but not by 2 (Note that the range
includes 35 but excludes 75)
INPUT OUTPUT

36 No
65 Yes

80 No
90 Yes

4.Write a program that checks if a number is within the range


(10,40) and divisible by 5 or within the range (60, 90) and
divisible by 7.

INPUT OUTPUT

36 No

65 Yes

80 No

90 Yes

5.Write a program that implements the following system. Make


sure that you understand the meaning of the brackets. ( means
including the value,) means excluding value.
MARK RANGE GRADE
[85, 100] A
[80,85) A-
[75,80) B+
[70,75) B
[65,70) B-
[60,65) C+
[55,60) C
[50,55) C-
[45,50) D+
[40,45) D
[0,40) F

6.Write a program that takes the user’s age as input and then
finds out what stage of human life they are in using the table
given below.
AGE RANGE STAGE
Less than 3 years Infancy
From 3 years to less than 12 years Childhood
From 12 years to less than 20 years Adolescence
From 20 years to less than 40 years Young adulthood
From 40 years to less than 65 years Mature adulthood
65 years and above Late adulthood

Program input/output
INPUT OUTPUT
20 Young adulthood
6 Childhood
12 Adolescence
1 Infancy
65 Late adulthood

7.Write a program that a character from the user and checks if


it is a lower-case letter, an upper-case letter, a digit or a
symbol. (Hint: learn about ASCII table)
INPUT OUTPUT
a Lowercase latter
9 Digit
% Symbol
H Uppercase latter
0 Digit
, symbol

8. Write a program thar asks the user to enter the x and y coordinates
of a point in 2-dimensional space. Then find out where the point is
located. Possible locations are the four quadrants, the two axes and
the origin. See the input-output table below.

INPUT OUTPUT
2nd 1st quadrant
X = -5 3nd quadrant quadrant
Y= -3
X-axis
X=-2.5 x-axsis
origin
Y=0
X=0 Origin
3nd 4th quadrant
Y=0 quadrant
X=0 y-Axis

Y=12
X=1.32 1st quadrant

Y=2.14

9. Write a program that takes the lengths of the three sides of


a triangle from the user and checks if the sides from a right
triangle. (a right triangle is a triangle is which one angle is
90 degrees)
INPUT OUTPUT

3 5 4 Right triangle

1 1 1 Not a right triangle

13 5 12 Right triangle
4.5 2.0 4.5 Not a right triangle

10. Write a program that ask the user to select an option for a
menu. The menu will have the following options:
1. Area of a circle
2. Are of a rectangle
3. Area of triangle
4. Volume of a cylinder
5. Volume of a sphere
6. Volume of a cube

The use will select an option by entering the corresponding serial number. For
example, let’s assume the use enters 2. That means the user wants to computer the
area of a rectangle. So, then the program asks the user to enter the length and
width of the rectangle. Once the user provides the requested inputs, the program
computes and prints the result.

INPUT OUTPUT
1. Area of circle

2. Area of a rectangle

3. Area of a triangle

4. Volume of a cylinder

5. Volume of a sphere

6. Volume of a cube Area of the


rectangle is 25
Select an option (1-6) : 2

<< Area of a rectangle>>

Enter length: 2.5


Enter width: 10
1. Area of circle

2. Area of a rectangle

3. Area of a triangle

4. Volume of a cylinder

5. Volume of a sphere

6. Volume of a cube

Select an option (1-6) : 2 Volume of the


sphere is 4.849
<< Area of a rectangle>>

Enter length: 2.5

Enter width: 10

Solution of problems:
Problem 1
#include <iostream>

using namespace std;

int main () {

int x;

cout << "Please input your number" << endl;

cin >> x;

if (x==0)

cout<< "Neither positive or negative" << endl;

else if (x>0)

cout << "Positive" << endl;

else

cout << "Negative" << endl;

return 0;

Problem 2
#include <iostream>

using namespace std;

int main () {

float x;

cout << "Please input your number" << endl;

cin >> x;

if (x-int(x)==0)

cout<< "Integer" << endl;

else

cout << "Floating-point number" << endl;

return 0;}
Problem 3
#include <iostream>

using namespace std;

int main () {

int x;

cout << "Please input your number" << endl;

cin >> x;

if (x >= 35 && x < 75)

{if (x % 5 == 0 && x % 2 !=0)

{cout << "Yes" << endl;}}

else

cout << "No" << endl;

return 0;

}
Problem 4
#include <iostream>

using namespace std;

int main () {

int x;

cout << "Please input your number:" << endl;

cin >> x;

if (x > 10 && x < 40)

{if (x % 5 == 0)

{cout << "Yes" << endl;}

else

{cout << "No" << endl;}}

else if (x > 60 && x < 90)

{if (x % 7 == 0)

{cout << "Yes" << endl;}

else

{cout << "No" << endl;}}

else

{cout << "No" << endl;}

return 0;

}
Problem 5
#include <iostream>

using namespace std;

int main () {

int x;

cout << "Please input the mark:" << endl;

cin >> x;

if (x >= 0 && x < 40)

cout << "F\n";

else if (x >= 40 && x < 45)

cout << "D\n";

else if (x >= 45 && x < 50)

cout << "D+\n";

else if (x >= 50 && x < 55)

cout << "C-\n";

else if (x >= 55 && x < 60)

cout << "C\n";

else if (x >= 60 && x < 65)

cout << "C+\n";

else if (x >= 65 && x < 70)

cout << "B-\n";

else if (x >= 70 && x < 75)

cout << "B\n";

else if (x >= 75 && x < 80)

cout << "B+\n";

else if (x >= 80 && x < 85)

cout << "A-\n";

else if
cout << "A\n";

return 0;

Problem 6
#include <iostream>

using namespace std;

int main () {

int x;

cout << "Please input the mark:" << endl;

cin >> x;

if (x >= 0 && x < 3)

cout << "Infancy\n";

else if (x >= 3 && x < 12)

cout << "Childhood\n";

else if (x >= 12 && x < 20)

cout << "Adolescence\n";

else if (x >= 20 && x < 40)

cout << "Young adulthood\n";

else if (x >= 40 && x < 65)

cout << "Mature adulthood\n";

else

cout << "Late adulthood\n";

return 0;

}
Problem 7
#include <iostream>

using namespace std;

int main () {

cout << "Please enter your charecter:\n";

char x;

cin >> x;

if (int(x)>= 48 && int(x)<= 57)

cout << "Digit\n";

else if (int(x)>= 65 && int(x)<= 90)

cout << "Uppercase Letter\n";

else if (int(x)>= 97 && int(x)<= 122)

cout << "Lowercase Letter\n";

else

cout << "Symbol\n";

return 0;

}
Problem 8
#include <iostream>

using namespace std;

int main () {

cout << "Please enter the x cordinate:\n";

float x, y;

cin >> x;

cout << "Please enter the y cordinate:\n";

cin >> y;

if (x == 0 && y == 0)

cout << "Origin\n";

else if (x > 0 && y > 0)

cout << "1st Quadrant\n";

else if (x < 0 && y > 0)

cout << "2nd Quadrant\n";

else if (x < 0 && y < 0)

cout << "3rd Quadrant\n";

else

cout << "4th Quadrant\n";

return 0;

}
Problem 9
#include <iostream>

#include <cmath>

using namespace std;

int main () {

cout << "Please enter the side lengths of the triangle:\n";

double A, B, C;

cin >> B >> A >> C;

if (A >= B && A >= C)

{double C1 = A;

double A1 = C;

double B1 = B;

if (pow(A1, 2) + pow(B1, 2) == pow(C1, 2))

cout << "Right Angled Triangle\n";

else

cout << "Not a Right Angled Triangle\n";}

else if (B >= A && B >= C)

{double C1 = B;

double A1 = A;

double B1 = C;

if (pow(A1, 2) + pow(B1, 2) == pow(C1, 2))

cout << "Right Angled Triangle\n";

else

cout << "Not a Right Angled Triangle\n";}

else if (C >= A && C >= B)

{double C1 = C;
double A1 = A;

double B1 = B;

if (pow(A1, 2) + pow(B1, 2) == pow(C1, 2))

cout << "Right Angled Triangle\n";

else

cout << "Not a Right Angled Triangle\n";}

return 0;

}
Problem 10
using namespace std;

int main() {

int select;

cout<<"Select an option(1-6) from the menu below.\n";

cout << "1. Area of a circle\n2. Are of a rectangle\n3. Area of triangle\n";

cout << "4. Volume of a cylinder\n5. Volume of a sphere\n6. Volume of a cube\n";

cin>>select;

switch(select){

case 1: {

double r;

cout << "Enter the radius of the circle:\n";

cin >> r;

cout << "Area of circle: "<< 3.142*r*r << endl;

break;

case 2: {

double r, a;

cout << "Enter the Base of the Rectangle: \n";

cin >> r;

cout << "Enter the Base of the Rectangle: \n";

cin >> a;

cout << "Area of Rectangle: "<< r*a << endl;

break;

case 3: {
double r, a;

cout << "Enter the Base of the Triangle: \n";

cin >> r;

cout << "Enter the height of the Triangle: \n";

cin >> a;

cout << "Area of Triangle: "<<0.5*r*a << endl;

break;

case 4: {

double r, a;

cout << "Enter the Radius of Cylinder: \n";

cin >> r;

cout << "Enter the height of the Cylinder: \n";

cin >> a;

cout << "Volume of Cylinder: " << 3.142*pow(r, 2)*a << endl;

break;

case 5: {

double r;

cout << "Enter the Radius of Sphere: \n";

cin >> r;

cout << "Volume of Sphere: " << 3.142*pow(r, 3)*4/3 << endl;

break;

case 6: {

double r;

cout << "Enter the base of cube: \n";

cin >> r;
cout << "Volume of Cube: " << pow(r, 3) << endl;

break;

return 0;

Das könnte Ihnen auch gefallen