homework02 conditionalstatements loops
TRANSCRIPT
-
8/9/2019 Homework02 ConditionalStatements Loops
1/3
Homework 2: Conditional statements (if, switch) Loops
(for, while, do while)
1. Write a program that enters co-ordinates of three points A(x0,y0), B(x1, y1), C(x2, y2),
checks if they are three a!id ertex co-ordinates of a triang!e
2. Write a program that enters a month, year, prints o"t n"m#er of days in the month
o $t is kno%n
o month 1,&,',,,10,12 * n"m#er of days is &1
o month +,,,11 * n"m#er of days is &0
o month 2 * n"m#er of days of !eap year* 2
* n"m#er of days of the others* 2o !eap year is year +00 // 0
or year + // 0 and year 100 / 0
&. Write a program that enters a day, month, year, checks if it is a!id
+. Write a program that prints o"t fo!!o%ing text*
What is o"r co"ntrys name at the inh 3ien 4oang age5
a. 6an !ang
#. ai co iet
c. An namAnd reads a character from the command !ine
$f that character is 7B8 or 7#8 then prints o"t 76ery good8
9ther%ise prints o"t 7:o" are %rong. ;ood !"ck to yo" next time8
'.
-
8/9/2019 Homework02 ConditionalStatements Loops
2/3
1
2 1
& 2 1
+ & 2 1
' + & 2 1
. Write an a!gorithm to accept a n"m#er #et%een 1 and and disp!ay a pyramid. ?or
examp!e, if the n"m#er entered is ', the fo!!o%ing pyramid %i!! #e disp!ayed*
1
1 2 1
1 2 & 2 1
1 2 & + & 2 1
1 2 & + ' + & 2 1
. Write a program to read an integer n"m#er (n>0). Write fo!!o%ing methods*
- Checking %hether a n"m#er is a perfect n"m#er or not5
- ;enerate the first n perfect n"m#ers.
. Write a method to enter a integer n"m#er (n>0) and resere that n"m#er.
Guide:3he reerse n"m#er is an integer n"m#er so"rce from the orgine one #"t the
order of digits are reersed.
Example: 3he reerse n"m#er of*
12&+' is '+&21
200 is 002
10 is 1
&00+00 is +00&
10. Write a method to check %hether a a!id n"m#er or not5
Guide: 3he a!id n"m#eris a positie integer n"m#er %hich has & digits. 9ther%ise
is ina!id n"m#er
Example:
12& is a!id
2& -> ina!id
+' -> ina!id
Input* an integer n"m#er from key#oard
Output*
-
8/9/2019 Homework02 ConditionalStatements Loops
3/3
- $f the inp"t n"m#er is ina!id (kh@ng hp !), print o"t to conso!e the
message* DEAF
- $f the inp"t n"m#er is a!id print o"t the reerse n"m#er of the inp"t.
Example
Input* 01
Output* DEA
---
Input* 0
Output*
11. Write a program to find 4ighest Common ?actor (4C?) and Geast Common
H"!tip!e of t%o integer n"m#ers.
12. Write an a!gorithm to find the 4ighest Common ?actor (4C?) of three n"m#ers.
Input* 3hree integer n"m#ers from key#oard
Output* 4ighest Common ?actor (4C?) of three n"m#ers.
Example
Input* 2' 1' '
Output* '
---
Input 2 +
Output 1
13. Io!e !inear eJ"ation in one variable
#x K c / 0
Note: creat the folloing constructors:
public !inearE"uation#$
public !inearE"uation#float b% float c$
1&. Io!e sJ"are eJ"ation in one variable
ax2K #x K c / 0
public '"uareE"uation#$
public '"uareE"uation#float a% float b% float c$