0% found this document useful (0 votes)
9 views

JAVA Practice Exercises

Uploaded by

killergenius369
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
9 views

JAVA Practice Exercises

Uploaded by

killergenius369
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 5

CMPS 201: Introduction to Programming Using JAVA

Extra Practice Exercises

Maurice J. KHABBAZ, Ph.D.


Practice Exercise 1
Write a JAVA Program that requests from the user to input an integer n and then
approximates the factorial of n using Stirling’s Formula which is given as follows:

𝑛𝑛 2𝜋𝑛
𝑛! ≈
𝑒𝑛
Practice Exercise 2
Write a JAVA Program that requests from the user to input a certain number of
seconds and then would determine and output the equivalent number of hours,
minutes and remaining seconds.
Practice Exercise 3
Design and implement a JAVA program called UnitAverage.java that reads 2
integers from the user and prints the average of their unit numbers (i.e., their digits).
For example, if the user enters 65 and 94, your program must print the average of
their unit numbers (5 and 4 respectively) which is 4.5.
Practice Exercise 4
Write a JAVA program, called Fractions.java, that asks the user to enter two fractions in the format
of: numerator / denominator. Your program should then add the two fractions and display the answer
as a simplified fraction. To achieve this, the program needs to indicate to the user the need to enter a
fraction as such: the numerator followed by a white space then /, then another white space, and finally
the denominator. An example of an entered fraction is 2 / 3. Observe that this is not taken as a
String object but as a stream of entered integers separated by non-integer characters. The two
fractions must be entered in this way.

Hints and Useful Notes:

𝑛 𝑛 𝑛 𝑛 𝑛1 ×𝑑2 +𝑛2 ×𝑑1


1) The addition of two fractions, for example, 𝑑1 and 𝑑2 is performed as: 𝑑1 + 𝑑2 =
1 2 1 2 𝑑1 ×𝑑2

2) To simplify a fraction, both the numerator and denominator need to be divided by their Greatest
Common Divisor (GCD). Intuitively, one way to compute the GCD of two numbers say a and b, check the
divisibility of both these numbers in a loop that exhaustively scans all numbers in the range 1 to the
minimum of a and b and make sure to record the biggest number that divides both of them. This latter is,
thus, the GCD of the two numbers a and b.

You might also like