fork download
  1. #include <stdio.h>
  2. // This program will provide options for a user to calculate the square
  3. // or cube of a positive Integer input by a user. // Start Main program Main
  4. // Declare variables Declare intValue, menuSelect,Results as Integer
  5. // Set intValue to positive value to start loop Set intVal = 1;
  6. // Loop While input is a positive number While intValue > 0 Print "Enter a positive Integer:” Input intValue
  7. // Only perform menu and function calls is integer is positive Print "Enter 1 to calculate Square, 2 to Calculate Cube " Input menuSelect If menuSelect == 1 Then // Call the Square Function Set Results = Square(intValue) Print intValue,Results Else If menuSelect == 2 Then // Call the Cube function set Results = Cube(intValue) Print intValue,Results Else Print “Invalid menu item, only 1 or 2 is accepted” End If End If END While
  8. // End of Main program End Program
  9. // Square Function Function Square(value) as Integer Set Square = value*value End Function
  10. // Cube Function Function Cube(value) as Integer Set Cube = value*value*value End Function
  11. int main(void)
  12. {
  13. /* variable definition: */
  14. int intValue, menuSelect,Results;
  15. intValue = 1;
  16. // While a positive number while (intValue > 0)
  17. {
  18. printf ("Enter a positive Integer\n: ");
  19. scanf("%d", &intValue);
  20.  
  21. {
  22. printf ("Enter 1 to calculate Square, 2 to Calculate Cube \n: ");
  23. scanf("%d", &menuSelect); if (menuSelect == 1)
  24. {
  25. // Call the Square Function Results = Square(intValue);
  26. printf("Square of %d is %d\n",intValue,Results);
  27. }
  28. else if (menuSelect == 2)
  29. {
  30. // Call the Cube function Results = Cube(intValue);
  31. printf("Cube of %d is %d\n",intValue,Results);
  32. }
  33. else
  34. printf("Invalid menu item, only 1 or 2 is accepted\n");
  35. }
  36. }
  37. return 0;
  38. }
  39. /* function returning the Square of a number */
  40. int Square(int value)
  41. {
  42. return value*value;
  43. }
  44. /* function returning the Cube of a number */
  45. int Cube(int value)
  46. { return value*value*value;
  47.  
  48. return 0;
  49. }
  50.  
Success #stdin #stdout 0s 9432KB
stdin
10

1

10

2

-1

12
stdout
Enter a positive Integer
: Enter 1 to calculate Square, 2 to Calculate Cube 
: Square of 10 is 0