Sheet5 Java
Sheet5 Java
1. Write a Java program which asks the user to enter an integer and
checks if it’s odd or even. The program then prints to the user “odd”
if the integer is odd and prints “even” if the integer is even.
Sample output 1:
Enter an Integer please: 4
4 is even.
Sample output 2:
Enter an
Integer please:
7 7 is odd.
2. Number guessing game is a simple game where the user tries to
guess a randomly selected number. Write a Java program which
randomly generates an integer greater than or equal to 0 and less than
100. The program then prompts the user to guess the number. If the
user guesses the number correctly, the program outputs an
appropriate message. Otherwise, the program checks whether the
guessed number is less than or greater than the random number.
Sample output 1:
Enter your guess please: 52
Your guess is greater than the chosen number.
The chosen number is 30.
Sample output 2:
Enter your guess please: 20
Your guess is less than the
chosen number. The chosen number
is 43.
3. Write a java program that solves quadratic equations and prints
their roots. Recall that a quadratic equation is a polynomial equation
in terms of a variable x of the form ax 2+bx+c=0. The term b2-4ac is
known as the discriminant of a quadratic equation. The discriminant
tells the nature of the roots.
The program will read from the user, the coefficients a, b, and c, print
the equation in the form ax2+bx+c=0 with the values of the
coefficients and print the roots of the equation. If roots are not real
print them in terms of “i”. The real part equation is:
Sample output:
Enter coefficient
a: 2.3 Enter
coefficient b: 4
Enter coefficient c: 5.6
The form of the equation: 2.3x^2+4x +5.6=0
Roots are complex and different
The first root of the equation = -0.87+1.30i
The second root of the equation = -0.87-1.30i
Sample output:
Enter coefficient a: 2
Enter coefficient b: 3
Enter coefficient c: 1
The form of the equation:
2x^2+3x+1=0 Roots are real
and different
The first root of the equation = -0.5
The second root of the equation = -1.0
5. Write a java program that will print the sum, the average and
the product of numbers from 1 till n. The program will ask the
user to enter the value of n, then the program will return the
value of the sum, the value of the average and the value of the
product of numbers from 1 till n.
Sample output:
Enter a number n: 6
The sum of numbers from 1 till 6 is: 21.0
The average of numbers from 1 till 6 is: 3.5
The product of numbers from 1 till 6 is: 720.0
7. Write and test a java program that picks a random number between 0
and 9 and gives the user 3 chances to guess it.
If the user doesn’t guess the number correctly, the program will give
the user a hint to guess it, (i.e.: the program will print a message to tell
the user if the input number that the user entered is greater than or less
than the random number)
The user has 3 attempts only to guess the number, then the program
should display the chosen number and exit.
Example1:
I’m thinking about a number between 0 and 9. Can you guess it? (3
attempt(s) left):
6
Sorry. Wrong number.
Try to guess a number less than 6
I’m thinking about a number between 0 and 9. Can you guess it? (2
attempt(s) left):
4
Sorry. Wrong number.
Try to guess a number greater than 4
I’m thinking about a number between 0 and 9. Can you guess it? (1
attempt(s) left):
5
Congratulations! That was the correct number.
The correct number is 5
Example2:
I’m thinking about a number between 0 and 9. Can you guess it? (3
attempt(s) left):
9
Sorry. Wrong number.
Try to guess a number less than 9
I’m thinking about a number between 0 and 9. Can you guess it? (2
attempt(s) left):
7
Sorry. Wrong number.
Try to guess a number less than 7
I’m thinking about a number between 0 and 9. Can you guess it? (1
attempt(s) left):
6
r=4
Sorry. Wrong number.
Try to guess a number less than 6
The correct number is 4