fork download
  1. // C code
  2.  
  3. // This program will provide options for a user to calculate the square
  4.  
  5. // or cube of a positive Integer input by a user.
  6.  
  7. // Developer: Faculty CMIS102
  8.  
  9. // Date: Jan 31, XXXX
  10.  
  11. #include <stdio.h>
  12.  
  13. int Square(int value);
  14. int Cube(int value);
  15. int main ()
  16.  
  17. {
  18.  
  19. /* variable definition: */
  20.  
  21. int intValue, menuSelect,Results;
  22.  
  23. intValue = 1;
  24.  
  25. // While a positive number
  26.  
  27. while (intValue > 0)
  28.  
  29. {
  30.  
  31. printf ("Enter a positive Integer\n: ");
  32.  
  33. scanf("%d", &intValue);
  34.  
  35. if (intValue > 0)
  36.  
  37. {
  38.  
  39. printf ("Enter 1 to calculate Square, 2 to Calculate Cube \n: ");
  40.  
  41. scanf("%d", &menuSelect);
  42.  
  43. if (menuSelect == 1)
  44.  
  45. {
  46.  
  47. // Call the Square Function
  48.  
  49. Results = Square(intValue);
  50.  
  51. printf("Square of %d is %d\n",intValue,Results);
  52.  
  53. }
  54.  
  55. else if (menuSelect == 2)
  56.  
  57. {
  58.  
  59. // Call the Cube function
  60.  
  61. Results = Cube(intValue);
  62.  
  63. printf("Cube of %d is %d\n",intValue,Results);
  64.  
  65. }
  66.  
  67. else
  68.  
  69. printf("Invalid menu item, only 1 or 2 is accepted\n");
  70.  
  71. }
  72.  
  73. }
  74.  
  75. return 0;
  76.  
  77. }
  78.  
  79. /* function returning the Square of a number */
  80.  
  81. int Square(int value)
  82.  
  83. {
  84.  
  85. return value*value;
  86.  
  87. }
  88.  
  89. /* function returning the Cube of a number */
  90.  
  91. int Cube(int value)
  92.  
  93. {
  94. return value*value*value;
  95.  
  96. }
Success #stdin #stdout 0s 4428KB
stdin
10
1
10
2
-99
stdout
Enter a positive Integer
: Enter 1 to calculate Square, 2 to Calculate Cube 
: Square of 10 is 100
Enter a positive Integer
: Enter 1 to calculate Square, 2 to Calculate Cube 
: Cube of 10 is 1000
Enter a positive Integer
: