Guess number Game

Source: Internet
Author: User

#include <stdio.h> #include <stdlib.h> #include <time.h>int main () {int input =  1;while  (Input) {printf ("***********************\n");p rintf ("*****>1. Start Game *******\n");p rintf ("* * * *>0. Exit Game *******\n ");p rintf (" ***********************\n ");p rintf (" Please enter directive (1 or 0): \ n "); scanf ("%d ",  &input);switch  (input) {case 1:{ int n = 0; int num =  0;         srand (Time (NULL));//generates a random value based on timing  n =  rand ()  % 101;        while  (1)   {    printf ("Guess a number (1~100):"); &NBSP;&NBSP;&NBSP;SCANF ("%d",  &num);      if  (num > n)    {   printf ("Guess big \");       }  else if  (num < n)   {  printf ("Guess small");   } else &nbSp;           {     printf (" Congratulations Right! \ n ");     break;    }   } break;} default:{  printf ("Game quit!");    break;}}} System ("pause"); return 0;}

Improved, modular functions

#include <stdio.h> #include <stdlib.h> #include <time.h>void print_menu () {printf ("********** \ n ");p rintf (" *****>1. Start Game *******\n ");p rintf (" *****>0. Exit game *******\n ");p rintf (" ************** \ n ");p rintf (" Please enter directive (1 or 0): \ n ");} Int main () {      print_menu ();int input = 1;while  ( Input) {scanf ("%d",  &input);switch  (input) {case 1:{            print_menu ();   int n = 0;  int ch =  0;  srand (Time (NULL));//Generate random values based on timing   n = rand ()  % 101;   while  (1)  {    printf ("Guess a number (1~100):"); &NBSP;&NBSP;&NBSP;&NBSP;SCANF ("%d",  &ch);    if  (ch > n)  {  printf ("Guess Big");   }      else if  (ch < n)               { printf ("Guess small");       } else     {  printf ("Congratulations right! ");   break;      }     } break;} default:{  printf ("Game quit!");  break;}}} System ("pause"); return 0;}


This article is from the "incomparable Warm yang" blog, please be sure to keep this source http://10797127.blog.51cto.com/10787127/1707520

Guess number Game

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.