2nd c++ Problem
1:-There are 21 matchsticks
2:-The computer asks the player to pick 1,2,3,4 matchsticks
3:-After the person picks,the computer does its picking.
whoever is forced to pick up the last match-stick loses the Game
Member • Dec 30, 2009
#include "stdio.h" int main() { int choice = 0; int sum = 0; while(sum != 20) { while((choice < 1) || (choice > 4)) { printf("Enter number of matchstick you want to pick (between 1 to 4): "); scanf("%d", &choice); if((choice < 1) || (choice > 4)) { printf("Invalid input\n"); } } sum += choice; printf("Matchstick left: %d\n", 21 - sum); printf("Matchsticks picked by computer: %d\n", 5 - choice); sum += 5 - choice; printf("Matchstick left: %d\n", 21 - sum); scanf("%c", &choice); choice = 0; } printf("Only one stick left and your turn to pick. You lost.\n"); return 0; }-Pradeep
Member • Dec 30, 2009
Member • Dec 31, 2009
Good ...you have correct outputpradeep_agrawalFind below my piece of code for same.
#include "stdio.h" int main() { int choice = 0; int sum = 0; while(sum != 20) { while((choice < 1) || (choice > 4)) { printf("Enter number of matchstick you want to pick (between 1 to 4): "); scanf("%d", &choice); if((choice < 1) || (choice > 4)) { printf("Invalid input\n"); } } sum += choice; printf("Matchstick left: %d\n", 21 - sum); printf("Matchsticks picked by computer: %d\n", 5 - choice); sum += 5 - choice; printf("Matchstick left: %d\n", 21 - sum); scanf("%c", &choice); choice = 0; } printf("Only one stick left and your turn to pick. You lost.\n"); return 0; }-Pradeep
Member • Dec 31, 2009