C language code to guess Numbers

  • 2020-11-26 18:53:20
  • OfStack

This article Shared C language code for you to guess the specific code, for your reference, the specific content is as follows

Topic describes

A guessing game, as the name suggests, requires one to guess an unknown but certain number. Where this unknown but deterministic number is randomly generated by the program, and when the number is generated we guess the number, and the program tells us to guess high, low, or right and prompts us to guess.

Code implementation


#include <stdio.h>
#include <stdlib.h>
#include <time.h>

void Guess_num(void)
{
 int n;
 char begin;
 int count = 1;
 srand((int)time(NULL));
 int m = (rand() % 100) + 1;
 puts(" The game starts, please enter the number :");
 while (1)
 {
  scanf_s("%d", &n);
  if (n == m)
  {
   printf(" Got it. Used it  %d  Times! \n", count);
   if (count == 1)
   {
    printf(" You're a god! worship \n");
    getchar();
    printf(" You have reached the highest level, do you still need to play? Y/N \n");
    scanf_s("%c", &begin);
    if (begin == 'Y' || begin == 'y')  // Repeat play 1 A nested loop 
    {
     Guess_num();
    }
    else
    {
     printf(" Thank you. Bye. !\n");
    }
   }
   else if (count <= 5)
   {
    printf(" You're a king! Very nice \n");
    getchar();
    printf(" Need to challenge the highest level? Y/N \n");
    scanf_s("%c", &begin);
    if (begin == 'Y' || begin == 'y')
    {
     Guess_num();
    }
    else
    {
     printf(" Thank you. Bye. !\n");
    }
   }
   else if (count <= 10)
   {
    printf(" You're a master! Heap praise \n");
    getchar();
    printf(" Need to challenge the highest level? Y/N \n");
    scanf_s("%c", &begin);
    if (begin == 'Y' || begin == 'y')
    {
     Guess_num();
    }
    else
    {
     printf(" Thank you. Bye. !\n");
    }
   }
   else if (count <= 15)
   {
    printf(" You're a diamond! Nu praise \n");
    getchar();
    printf(" Need to challenge the highest level? Y/N \n");
    scanf_s("%c", &begin);
    if (begin == 'Y' || begin == 'y')
    {
     Guess_num();
    }
    else
    {
     printf(" Thank you. Bye. !\n");
    }
   }
   else
   {
    getchar();
    printf(" Your skills still need to be improved. Replay?  Y/N\n");
    scanf_s("%c", &begin);
    if (begin == 'Y' || begin == 'y')
    {
     Guess_num();
    }
    else
    {
     printf(" Thank you. Bye. !\n");
    }
   }
   break;
  }
  else if (n < m)
  {
   puts(" Is too small !");
   puts(" Enter again :");
  }
  else
  {
   puts(" It's too big !");
   puts(" Enter again :");
  }
  count++;// counter 


 }
}


int main(void)
{

 Guess_num();
 system("pause");
 return 0;
}

More interesting classic games to realize the special topic, to share with you:

C++ classic game summary

python classic game summary

python Tetris game collection

JavaScript classic games don't stop playing

java classic game summary

javascript classic game summary


Related articles: