fork download
  1. // C Code
  2.  
  3. // This program will provide options for a user to calculate the square or
  4. // cube, of a positive Integer, and a shrink that divides a positive Integer
  5. // by 2 that is input by a user.
  6. #include <stdio.h>
  7. // function prototypes
  8. int Square(int);
  9. int Cube(int);
  10. double Shrink(int);
  11.  
  12. int main(void)
  13. {
  14. /* variable definition: */
  15. int intValue, menuSelect, Results, shrinkResults;
  16. intValue = 1;
  17.  
  18.  
  19. //While a positive number
  20. while (intValue >0)
  21. {
  22. printf ("Enter a positive Integer\n:");
  23. scanf("%d", &intValue);
  24. if (intValue > 0)
  25. {
  26. printf("Enter 1 to calculate Square, 2 to calculate Cube, 3 to calculate Shrink \n:");
  27. scanf("%d", &menuSelect);
  28. if (menuSelect ==1)
  29. {
  30. // Call the Square Function
  31. Results = Square(intValue);
  32. printf("Square of %d is %d\n", intValue, Results);
  33. }
  34. else if (menuSelect ==2)
  35. {
  36. //Call the Cube function
  37. Results = Cube(intValue);
  38. printf("Cube of %d is %d\n", intValue, Results);
  39. }
  40. else if (menuSelect ==3)
  41. {
  42. //Call the Shrink function
  43. shrinkResults = Shrink(intValue);
  44. printf("Half of %d is %lf\n", intValue, shrinkResults);
  45. }
  46. else
  47. printf("Invalid menu item, only 1 or 2 or 3 is accepted\n");
  48. }
  49. }
  50. return 0;
  51. }
  52.  
  53. /* function returning the Square of a number */
  54. int Square(int value)
  55. {
  56. return value*value;
  57. }
  58.  
  59. /* function returning the Cube of a number */
  60. int Cube(int value)
  61. {
  62. return value*value*value;
  63. }
  64.  
  65. /* function returning the Shrink of a number */
  66. double Shrink(int value)
  67. {
  68. return (value)/2.0;
  69. }
  70.  
  71.  
Success #stdin #stdout 0s 10320KB
stdin
10
1
5
2
6
3
-4
-4
stdout
Enter a positive Integer
:Enter 1 to calculate Square, 2 to calculate Cube, 3 to calculate Shrink 
:Square of 10 is 100
Enter a positive Integer
:Enter 1 to calculate Square, 2 to calculate Cube, 3 to calculate Shrink 
:Cube of 5 is 125
Enter a positive Integer
:Enter 1 to calculate Square, 2 to calculate Cube, 3 to calculate Shrink 
:Half of 6 is 0.000000
Enter a positive Integer
: