Chapter - 4: The Loop Control Structure

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 matchsticks. - The computer asks the player to pick 1, 2, 3, or 4 matchsticks. - After the person picks, the computer does its picking. - Whoever is forced to pick up the last matchstick loses the game.


B
Sections
6
Exercises

A

B

C

D

E

1

2

3

4

5

6

7

8

9

#include<stdio.h>
#include<conio.h>
int main()
{
	int num,i,left;
	
	printf("You have 21 matchstick, pick between 1 to 4 : ");
	scanf("%d", &num);
	for(i=1;i<5;i++)
	{
		if(num>4)
		{
			printf("\nWrong choice!! \n\nChoose from 1 to 4.\n\nTry again!!");
			break;
		}
		left=21-5*i;
		printf("\nComputer picked %d matchsticks.\n\n%d matchstick left", 5-num, left);
		if(left==1)
		{
			printf(" and it's your turn\n\nYou loose the game.\n\nTry again!!");
			break;
		}
		printf("\n\nYour turn, pick matchsticks between 1 to 4 : ");
		scanf("%d", &num);
	}
	getch();
	return 0;
}

© 2020 Garbage Valuegarbage value logo