Tuesday, 8 January 2013

C language games


INTERESTING PROGRAMS IN C LANGUAGE








Beeps the speaker

#include <dos.h>
#include <stdio.h>
#include <stdlib.h>
int menu(void);


main()
{
while(1)
{
/*get selection and execute the relevant statement*/
switch(menu())
{
case 1:
{
puts("sound the speaker 1\n");
sound(2000);
sleep(2);
nosound();
break;
}
case 2:
{
puts("sound that speaker 2\n");
sound(4000);
sleep(2);
nosound();
break;
}
case 3:
{
puts("You are quitting\n");
exit(0);
break;
}
default:
{
puts("Invalid menu choice\n");
break;
}
}
}
return 0;
}

/*menu function*/
int menu(void)
{
int reply;
/*display menu options*/
puts("Enter 1 for beep 1.\n");
puts("Enter 2 for beep 2.\n");
puts("Enter 3 to quit.\n");
/*scan for user entry*/
scanf("%d", &reply);

return reply;
}
--------------x---------------------x---------------------x------------------------x-------------------

HANG MAN

#include <stdio.h>

int main()
{

 int mybday = 20;            //declare variable to be guessed
 int guess;              //declare variable to store guessed date
 int max = 31;              //declare variable to test for invalid input
 int min = 1;              //declare variable to test for invalid input

 printf("Try to guess the day of my Birthday: ");    //display the intro and
directions
 scanf("%d",&guess);            //`get the users guess and store it in the
variable guess

 if (guess == mybday)
 {
   printf("You Win!!");          //print you win if guess is correct
 }
 else if (guess < min)
 {
   printf("Thats NOT A DATE!!");        //print `Error for invalid input
 }
 else if (guess > max)
 {
   printf("There isn't %d days in a month!",guess);  //print Error for
invalid input
 }
 else if (guess > mybday)
 {
   printf("Too High!");          //print too high if guess is above answer
 }
 else if (guess < mybday)
 {
   printf("Too Low!");          //print too low if guess is below answer
 }
 else
 {
   printf("Try again!!");          //Print try again if guess if all else is not
true (not possible)
 }

 return 0;
}

No comments:

Post a Comment