You are on page 1of 3

QUEZON CITY UNIVERSITY

COLLEGE OF COMPUTER SCIENCE AND INFORMATION TECHNOLOGY

AL101 - ALGORITHMS AND COMPLEXITY

NAME: Vida, John Paul S.


STUDENT NO: 20-2167
YEAR/SECTION: 3rd year / SBIT - 3L
DATE: 3-10-23

INSTRUCTIONS:

Create a program that will determine if the user guesses the correct number generated randomly by the
system or not. You also need to draw a system Flowchart and the Pseudocode of this program.

FLOWCHART:
QUEZON CITY UNIVERSITY
COLLEGE OF COMPUTER SCIENCE AND INFORMATION TECHNOLOGY

PSEUDOCODE:

Set the secret number


Get the guess from the user
While the guess is incorrect:
If the guess is too big, tell the user it is greater than
If the guess is too small, tell the user it is less than
Get a new guess
Congratulate the user on guessing the correct number

SYSTEM’S SCREENSHOT (OUTPUT):

.
package example;
import java.util.Scanner;

public class guessing {


public static void
guessingNumberGame()
{
// Scanner Class
Scanner sc = new Scanner(System.in);

// Generate the numbers


int number = 1 + (int)(100
* Math.random());

// Given K trials
int K = 5;

int i, guess;

System.out.println(
"A number is chosen"
+ " between 1 to 100."
+ "Guess the number"
QUEZON CITY UNIVERSITY
COLLEGE OF COMPUTER SCIENCE AND INFORMATION TECHNOLOGY

+ " within 5 trials.");

// Iterate over K Trials


for (i = 0; i < K; i++) {

System.out.println(
"Guess the number:");

// Take input for guessing


guess = sc.nextInt();

// If the number is guessed


if (number == guess) {
System.out.println(
"Congratulations!"
+ " You guessed the number.");
break;
}
else if (number > guess
&& i != K - 1) {
System.out.println(
"The number is "
+ "greater than " + guess);
}
else if (number < guess
&& i != K - 1) {
System.out.println(
"The number is"
+ " less than " + guess);
}
}

if (i == K) {
System.out.println(
"You have guess"
+ " correct number.");

System.out.println(
"The number was " + number);
}
}

// Driver Code
public static void
main(String arg[])
{

// Function Call
guessingNumberGame();
}
}

You might also like