fork download
  1.  
  2. #include <stdio.h>
  3.  
  4. int main() {
  5. int n, h, i, p[10], choice;
  6.  
  7. printf("DINING PHILOSOPHER PROBLEM\n");
  8. printf("Enter the total no. of philosophers: ");
  9. scanf("%d", &n);
  10.  
  11. printf("How many are hungry: ");
  12. scanf("%d", &h);
  13.  
  14. for (i = 0; i < h; i++) {
  15. printf("Enter philosopher %d position: ", i + 1);
  16. scanf("%d", &p[i]);
  17. }
  18.  
  19. do {
  20. printf("\n1. One can eat at a time");
  21. printf("\n2. Two can eat at a time");
  22. printf("\n3. Exit");
  23. printf("\nEnter your choice: ");
  24. scanf("%d", &choice);
  25.  
  26. if (choice == 1) {
  27. for (i = 0; i < h; i++)
  28. printf("P %d is granted to eat\n", p[i]);
  29. }
  30. else if (choice == 2) {
  31. for (i = 0; i < h; i++) {
  32. if (i + 1 < h &&
  33. (p[i] % n != p[i + 1] % n) &&
  34. (p[i] % n != (p[i + 1] + 1) % n) &&
  35. (p[i] + 1) % n != p[i + 1] % n) {
  36. printf("P %d and P %d are granted to eat\n",
  37. p[i], p[i + 1]);
  38. }
  39. }
  40. }
  41. else if (choice != 3) {
  42. printf("Invalid choice!\n");
  43. }
  44.  
  45. } while (choice != 3);
  46.  
  47. return 0;
  48. }
  49.  
Success #stdin #stdout 0s 5284KB
stdin
5
3
2
4
5
1
2
3
stdout
DINING PHILOSOPHER PROBLEM
Enter the total no. of philosophers: How many are hungry: Enter philosopher 1 position: Enter philosopher 2 position: Enter philosopher 3 position: 
1. One can eat at a time
2. Two can eat at a time
3. Exit
Enter your choice: P 2 is granted to eat
P 4 is granted to eat
P 5 is granted to eat

1. One can eat at a time
2. Two can eat at a time
3. Exit
Enter your choice: P 2 and P 4 are granted to eat

1. One can eat at a time
2. Two can eat at a time
3. Exit
Enter your choice: