Skip to content

Instantly share code, notes, and snippets.

@OmanCoding
Created July 19, 2018 05:13
Show Gist options
  • Save OmanCoding/6582e4477733346022bf9d226c73df48 to your computer and use it in GitHub Desktop.
Save OmanCoding/6582e4477733346022bf9d226c73df48 to your computer and use it in GitHub Desktop.
#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:");
}
}
#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;
}
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
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.
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!')
n = rand(10) + 1
puts 'Guess the number: '
puts 'Wrong! Guess again: ' until gets.to_i == n
puts 'Well guessed!'
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