Let us c solution

0 min read

 [c]

Write a program for a matchstick game being played between the 
computer and a user.Your program should ensure that the computer
always wins.Rules for the game are as follows:-
--There are 21 match sticks
--The computer asks the player to pick 1,2,3 or 4 matchstick.
--After the person picks the computer does its picking.
--whenever is forced to pick up the last matchstick loses the game.  */

#include<stdio.h>
int main()
{
	int m=21,p,c;
	while(1)
	{
		printf("\nNo.of matches left=%d\n",m);
		printf("Pick up 1,2,3 or 4 matches:");
		scanf("%d",&p);
		if(p>4||p<1)
		continue;
		m=m-p;
		printf("No.of match left=%d\n",m);
		c=5-p;
		printf("Out of which computer picked up=%d\n",c);
		m=m-c;
		if(m==1)
		{
			printf("Number of matches left=%d\n",m);
			printf("You lost the game !!\n");
		}
	}
	return 0;
}
Welcome to prgrmramit.blogspot.com! I'm Amit Singh, an expert in AI, Data Science, and Machine Learning. I created this blog to share practical insights and tips for those eager to learn and gro…

You may like these posts

  •   Write a menu driven program which has following options:1.Factorial of a number 2.Prime or not 3.Odd or even 4.Exit Once a menu item is selected the appropriate action sh…
  •  Exercise[6B(e)]Write a program to generate all Pythagorean Triplets with side length less than or equal to 30. */ #include<stdio.h> #include<math.h> int…
  •   Exercise[6B(i)]Write a program to produce the following output: 1 2 3 4 5 6 7 8 9 10 …
  •  Exercise[6B(h)]Write a program to print 24 hours of day with suitable suffixes like AM,PM,Noon and Midnight. */ #include<stdio.h> int main() { int h; for(h…
  •  Exercise[6B(g)]Ramanujan number is the smallest number that can be expressed as sum of two cubes in two different ways.Write a program to print all such numbers up to a re…
  •  Exercise[6B(f)]Population of a town today is 100000.The population hd increased steadily at the rate of 10% per year for last 10 years.Write a program to determine the popu…

Post a Comment

Subscribe with Gmail


Premium By Raushan Design