728x90
반응형
import random
import math

# Taking Inputs
lower = int(input("Enter Lower bound: - "))

# Taking Inputs
upper = int(input("Enter Upper bound: - "))

# generating random number between
# the lower and upper

x = random.randit(lower, upper)
print("\n\tYou've only ",
      round(math.log(upper - lower + 1, 2)),
      " chances to guess the integer!\n")

# Initializing the number of guesses.

count = 0

# for calculation of minimum number of
# guesses depends upon range

while count < math.log(upper - lower + 1, 2):
    count += 1
    
    # taking guessing number as input
    guess = int(input("Guess a number : - "))
    
    # Condition testing
    if x == guess:
        print("Congraturations you did it in ",
              count, " try")
        # Once guessed, loop will break
        break
    elif x > guess:
        print("You guessed too small!")
    elif x < guess:
        print("You guessed too high!")

    # If Guessing is more than required guesses,
    # show this output.
    
    if count >= math.log(upper - lower + 1, 2):
        print("\nThe number is %d" %x)
        print("\tBetter Luck Next time!")
#include <stdio.h>
#include <stdlib.h>
#include <time.h>

int main()
{
	int number, guess, nguesses = 1;
	srand(time(0));

	// Generates a random number between 1 and 100
	number = rand() % 100 + 1;

	// printf("The number is %d\n", number;
	// kiip running the loop
	// until the number is guessed

	do
	{
		printf("Guess the number between 1 to 100\n");
		scanf_s("%d", &guess);

		if (guess > number)
		{
			printf("you guessed to high\n");
		}
		else if (guess < number)
		{
			printf("you guessed to low\n");
		}
		else
		{
			printf("You guessed the correct number");
			printf("attempts : %d\n", nguesses);
		}
		nguesses++;
	} while (guess != number);



	return 0;
}
728x90
반응형

'Language > C & C++' 카테고리의 다른 글

Transfer an array to a function's arguments  (0) 2022.09.09
User-Defined Type  (0) 2022.09.08
malloc exam  (0) 2022.08.30
dynamic memory allocation  (0) 2022.08.30
the difference between Parameter and Argument  (0) 2022.08.30

+ Recent posts