Created
July 19, 2018 05:13
-
-
Save OmanCoding/6582e4477733346022bf9d226c73df48 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <stdlib.h> | |
#include <stdio.h> | |
#include <time.h> | |
int | |
main (void) | |
{ | |
int n; | |
int g; | |
char c; | |
srand (time (NULL)); | |
n = 1 + (rand () % 10); | |
puts ("I'm thinking of a number between 1 and 10."); | |
puts ("Try to guess it:"); | |
while (1) | |
{ | |
if (scanf ("%d", &g) != 1) | |
{ | |
/* ignore one char, in case user gave a non-number */ | |
scanf ("%c", &c); | |
continue; | |
} | |
if (g == n) | |
{ | |
puts ("Correct!"); | |
return 0; | |
} | |
puts (" That 's not my number. Try another guess:"); | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <iostream> | |
#include <cstdlib> | |
#include <ctime> | |
int main() | |
{ | |
srand(time(0)); | |
int n = 1 + (rand() % 10); | |
int g; | |
std::cout << "I'm thinking of a number between 1 and 10.\nTry to guess it! "; | |
while(true) | |
{ | |
std::cin >> g; | |
if (g == n) | |
break; | |
else | |
std::cout << "That's not my number.\nTry another guess! "; | |
} | |
std::cout << "You've guessed my number!" ; | |
return 0; | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
program guess_the_number | |
implicit none | |
integer::guess | |
real::r | |
integer::i, clock, count, n | |
integer, dimension(:), allocatable::seed | |
real, parameter::rmax = 10 ! nitialize random number generator: | |
call random_seed(size=n) | |
allocate (seed(n)) | |
call system_clock(count) | |
seed = count | |
call random_seed(put=seed) | |
deallocate (seed) ! ick a random number between 1 and rmax: | |
call random_number(r) ! between 0.0 and 1.0 | |
i = int((rmax - 1.0)*r + 1.0) ! between1 and rmax | |
! et user guess: | |
write (*, '(A)') 'I''m thinking of a number between 1 and 10.' | |
do | |
! oop until guess is correct | |
write (*, '(A)', advance='NO') 'Enter Guess: ' | |
read (*, '(I5)') guess | |
if (guess == i) exit | |
write (*, *) 'Sorry, try again.' | |
end do | |
write (*, *) 'You have guessed my number!' | |
end program guess_the_number | |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Program GuessTheNumber(input, output); | |
var number, guess: integer; | |
begin | |
randomize; | |
number:= random(10) + 1; | |
writeln ('I''m thinking of a number between 1 and 10, which you should guess.'); | |
write ('Enter your guess: '); | |
readln (guess); | |
while guess <> number do | |
begin | |
writeln ('Sorry, but your guess is wrong. Please try again.'); | |
write ('Enter your new guess: '); | |
readln (guess); | |
end; | |
writeln ('You made an excellent guess. Thank you and have a nice day.'); | |
end. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import random | |
target, guess = random.randint (1, 10), 0 | |
while target != guess: | |
guess = int (input ('Guess my number between 1 and 10 until you get it right: ')) | |
print ('Thats right!') |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
n = rand(10) + 1 | |
puts 'Guess the number: ' | |
puts 'Wrong! Guess again: ' until gets.to_i == n | |
puts 'Well guessed!' |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
val n = (math.random * 10 + 1).toInt | |
print("Guess the number: ") | |
while(readInt != n) print("Wrong! Guess again: ") | |
println("Well guessed!" ) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment