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

Sheet5 Java

1. Write programs to check if a number is even or odd, play a number guessing game to guess a randomly generated number between 0 and 100, and solve quadratic equations by calculating the discriminant and roots. 2. Write programs to print all even numbers from 0 to 100 and all multiples of 3 from 300 to 200, and to calculate the sum, average, and product of numbers from 1 to a user-input n. 3. Write programs to calculate the P(n,r) permutation formula and to play a number guessing game where the user has 3 attempts to guess a randomly generated number between 0 and 9, with hints if incorrect.

Uploaded by

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

Sheet5 Java

1. Write programs to check if a number is even or odd, play a number guessing game to guess a randomly generated number between 0 and 100, and solve quadratic equations by calculating the discriminant and roots. 2. Write programs to print all even numbers from 0 to 100 and all multiples of 3 from 300 to 200, and to calculate the sum, average, and product of numbers from 1 to a user-input n. 3. Write programs to calculate the P(n,r) permutation formula and to play a number guessing game where the user has 3 attempts to guess a randomly generated number between 0 and 9, with hints if incorrect.

Uploaded by

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

Sheet .5.

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.

• If discriminant is greater than 0, the equation has two real


different roots.
• If discriminant equals to 0, the roots are real and equal.
• If discriminant is less than 0, the roots are complex and different.

Hint: Find the absolute value of the ‘discriminant’ before


computing the square root of it to avoid negative sign under the
radical in case of complex roots.
The formula for calculating the roots of a quadratic equation is:

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:

And the imaginary 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

4. Write a java program that will print:


a. All the even numbers from 0 till 100 inclusive.
b. All the multiples of 3 from 300 down to 200.
Sample output:
Even numbers from 0 till 100 inclusive are:
0 2 4 6 8 10 12 14 16 18 20 22 24 26 28 30 32 34 36 38 40 42 44
46 48 50 52 54 56 58 60 62 64 66 68 70 72 74 76 78 80 82 84 86 88
90 92 94 96 98 100

Multiples of 3 from 300 down to 200


300 297 294 291 288 285 282 279 276 273 270 267 264 261 258 255
252 249 246 243 240 237 234 231 228 225 222 219 216 213 210 207
204 201

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

6. Write a java program that calculate the following formula:


P(n, r) = (n)! / (n-r)!
Sample Output:
Enter n: 5
Enter r:2
P(5, 2) =20.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

You might also like