Posted By

AlexanderRavikovich on 11/18/11


Tagged


Versions (?)

Programm that take 3 numbers from user between 20 and 48, and print weight of each number


 / Published in: C
 

In ths program i also check for wrong numbers, and prevent entering equal numbers.

  1. #include <stdio.h>
  2.  
  3. void main()
  4. {
  5. // Progamm 1
  6. // initializing variables
  7. int unum1=0, unum2=0, unum3=0, index=0, large=0, medium=0, small=0;
  8.  
  9. /* Ask to enter first number, with validating */
  10. do
  11. {
  12. /* If we ask for 1st time, print request */
  13. if (!index)
  14. {
  15. printf("Enter first number between 20 and 48:\n");
  16. }
  17. /* Put entered number in variable unum1 */
  18. scanf("%d", &unum1);
  19. /* If unum1 is not in condition, say to user enter another bumber */
  20. if (!(unum1 > 20 && unum1 < 48))
  21. {
  22. printf("Your number is wrong. Try again:\n");
  23. }
  24. /* Set index to 1, to avoid repeating line "Enter first number between 20 and 48" */
  25. index=1;
  26. }
  27. while (!(unum1 > 20 && unum1 < 48)); /* <----- till entered number is not in condition, run code in do {} */
  28.  
  29. /* Set index to 0, to use this variable again for next valdating loop */
  30. index = 0;
  31.  
  32. /* Ask to enter second number, with validating.
  33. Code is almost as previous, only now for second number */
  34. do
  35. {
  36. if (!index)
  37. {
  38. printf("Enter second number between 20 and 48:\n");
  39. }
  40.  
  41. scanf("%d", &unum2);
  42.  
  43. if (!(unum2 > 20 && unum2 < 48))
  44. {
  45. printf("Your number is wrong. Try again:\n");
  46. }
  47. /* If entered number is equal to first number, say to user enter another number */
  48. else if (unum2 == unum1)
  49. {
  50. printf("Enter different number from previous:\n");
  51. }
  52.  
  53. index=1;
  54. }
  55. while (!(unum2 > 20 && unum2 < 48) || unum2==unum1); /* I added check for preventing entering the same numbers */
  56.  
  57. /* Set index to 0,to use this variable again for next valdating loop */
  58. index=0;
  59. /* Ask to enter third number, with validating.
  60. Code is almost as previous two,
  61. only now for third number */
  62. do
  63. {
  64. if (!index)
  65. {
  66. printf("Enter third number between 20 and 48:\n");
  67. }
  68.  
  69. scanf("%d", &unum3);
  70.  
  71. if (!(unum3 > 20 && unum3 < 48))
  72. {
  73. printf("Your number is wrong. Try again:\n");
  74. }
  75. /* If entered number is equal to first or second number, say to user enter another number */
  76. else if (unum3 == unum1 || unum3 == unum2)
  77. {
  78. printf("Enter different number from two previous:\n");
  79. }
  80.  
  81. index=1;
  82. }
  83. while (!(unum3 > 20 && unum3 < 48) || (unum3 == unum1 || unum3 == unum2)); /* Check for preventing entering the same numbers as unum1 and unum2 */
  84.  
  85. /* When we know that all three number is in our condition,
  86. write code for printing large, medium and small number.
  87. */
  88.  
  89. /* If 1st number larger them 2nd and 3rd, the 1st number is LARGE */
  90. if (unum1 > unum2 && unum1 > unum3)
  91. {
  92. /* Set variable "large" = unum1; */
  93. large = unum1;
  94. /* If 3rd number smaller than 2nd number, 3rd number is SMALL, and 2st number is MEDIUM */
  95. if (unum3 < unum2)
  96. {
  97. medium = unum2;
  98. small = unum3;
  99. }
  100. /* If 3rd number is large than 2nd number, 3rd number is MEDIUM, and 2st number is SMALL */
  101. else
  102. {
  103. medium = unum3;
  104. small = unum2;
  105. }
  106. }
  107.  
  108. /* If 2nd number larger them 1st and 3rd, the 2st number is LARGE */
  109. if (unum2 > unum1 && unum2 > unum3)
  110. {
  111. large = unum2;
  112. if (unum3 < unum1)
  113. {
  114. medium = unum1;
  115. small = unum3;
  116. }
  117. else
  118. {
  119. medium = unum3;
  120. small = unum1;
  121. }
  122. }
  123.  
  124. /* If 3rd number larger them 1st and 2nd, the 3rd number is LARGE */
  125. if (unum3 > unum1 && unum3 > unum2)
  126. {
  127. large = unum3;
  128. if (unum1 < unum2)
  129. {
  130. medium = unum2;
  131. small = unum1;
  132. }
  133. else
  134. {
  135. medium = unum1;
  136. small = unum2;
  137. }
  138. }
  139.  
  140. /* Printing result */
  141. printf("%d - is Large\n%d - is Medium\n%d - is Small\n", large, medium, small);
  142. }

Report this snippet  

You need to login to post a comment.