Skip to content

Latest commit

 

History

History

Problem_05

Folders and files

NameName
Last commit message
Last commit date

parent directory

..
 
 
 
 
 
 
 
 

Exercise 1:

Write a program Hellos.java that takes the number of lines to print as a command-line argument. You may assume that the argument is less than 1000.

Hint: consider using i % 10 and i % 100 to determine whether to use "st", "nd", "rd", or "th" for printing the ith Hello.

Example:

% java Hellos 4

1st Hello
2nd Hello
3rd Hello
4th Hello

Exercise 2:

Write a program FivePerLine.java that, using one for loop and one if statement, prints the integers from 1000 to 2000 with five integers per line. Only last line may have less than 5 numbers.

Hint: use the % operator.

Bonus Exercise:

A couple beginning a family decides to keep having children until they have at least one of either sex. Assume that the probability p of having a boy or girl is 1/2. Write a program BoysAndGirls.java find total number of children by simulation.