fork download
  1. #include <stdio.h>
  2.  
  3. /*
  4.  * Program: Weekly Pay Calculator
  5.  * Purpose:
  6.  * - Prompt for hours worked per employee
  7.  * - Compute overtime hours, normal pay, overtime pay, and gross pay
  8.  * - Print a clean, aligned summary table
  9.  *
  10.  * Notes:
  11.  * - Overtime is paid at 1.5x for hours strictly above 40.0
  12.  * - Input validation ensures non-negative numeric hours
  13.  */
  14.  
  15. #define SIZE 5 // Total number of employees processed
  16. #define STD_HOURS 40.0f // Standard weekly hours before overtime begins
  17. #define OT_RATE 1.5f // Overtime rate (time-and-a-half)
  18.  
  19. int main(void)
  20. {
  21. // ---------------------------------------------------------------------
  22. // Employee static data (IDs and wage rates)
  23. // ---------------------------------------------------------------------
  24. long int clockNumber[SIZE] = {98401, 526488, 765349, 34645, 127615}; // unique employee IDs
  25. float wageRate [SIZE] = {10.60f, 9.75f, 10.50f, 12.25f, 8.35f}; // hourly wage for each employee
  26.  
  27. // ---------------------------------------------------------------------
  28. // Arrays for inputs and computed values
  29. // ---------------------------------------------------------------------
  30. float hours [SIZE] = {0}; // hours worked in the week
  31. float overtimeHrs[SIZE] = {0}; // overtime hours (hours above STD_HOURS)
  32. float normalPay [SIZE] = {0}; // pay at regular rate (up to STD_HOURS)
  33. float overtimePay[SIZE] = {0}; // pay at overtime rate
  34. float grossPay [SIZE] = {0}; // total pay = normalPay + overtimePay
  35.  
  36. // ---------------------------------------------------------------------
  37. // Title
  38. // ---------------------------------------------------------------------
  39. printf("\n*** Pay Calculator ***\n\n");
  40.  
  41. // ---------------------------------------------------------------------
  42. // INPUT: Prompt for hours per employee (clean, multi-line prompts)
  43. // - Validates numeric and non-negative values
  44. // ---------------------------------------------------------------------
  45. for (int i = 0; i < SIZE; i++) {
  46. printf("------------------------------------------------------------\n");
  47. printf("Employee Information\n");
  48. printf(" Clock Number : %ld\n", clockNumber[i]);
  49. printf(" Hourly Wage : $%.2f\n", wageRate[i]);
  50. printf("Request:\n");
  51. printf(" Please enter the hours worked this week\n");
  52. printf(" (use a dot for decimals, e.g., 40.50): ");
  53.  
  54. // Read and validate: must be numeric and >= 0.0
  55. while (scanf("%f", &hours[i]) != 1 || hours[i] < 0.0f) {
  56. int ch;
  57. // Clear out the rest of the invalid input line to avoid infinite loops
  58. while ((ch = getchar()) != '\n' && ch != EOF) { /* discard */ }
  59. printf(" -> Invalid input. Enter a non-negative number for hours: ");
  60. }
  61.  
  62. // Clear any trailing characters on the line (optional safety)
  63. int ch;
  64. while ((ch = getchar()) != '\n' && ch != EOF) { /* discard */ }
  65. }
  66.  
  67. // ---------------------------------------------------------------------
  68. // PROCESSING: Compute overtime hours and all pay components
  69. // ---------------------------------------------------------------------
  70. for (int i = 0; i < SIZE; i++) {
  71. if (hours[i] > STD_HOURS) {
  72. // Hours above the standard threshold are overtime
  73. overtimeHrs[i] = hours[i] - STD_HOURS;
  74.  
  75. // Normal pay uses only the standard hours cap
  76. normalPay[i] = STD_HOURS * wageRate[i];
  77.  
  78. // Overtime pay uses the overtime hours at the OT rate
  79. overtimePay[i] = overtimeHrs[i] * wageRate[i] * OT_RATE;
  80. } else {
  81. // No overtime if hours are within or below the standard threshold
  82. overtimeHrs[i] = 0.0f;
  83.  
  84. // All hours are paid at the normal rate
  85. normalPay[i] = hours[i] * wageRate[i];
  86. overtimePay[i] = 0.0f;
  87. }
  88.  
  89. // Total weekly gross pay
  90. grossPay[i] = normalPay[i] + overtimePay[i];
  91. }
  92.  
  93. // ---------------------------------------------------------------------
  94. // OUTPUT: Print aligned table of results
  95. // ---------------------------------------------------------------------
  96. printf("\n%-10s %8s %8s %8s %12s %12s %12s\n",
  97. "Clock#", "Wage", "Hours", "OT Hrs", "Normal Pay", "OT Pay", "Gross Pay");
  98. printf("--------------------------------------------------------------------------------\n");
  99.  
  100. for (int i = 0; i < SIZE; i++) {
  101. printf("%-10ld %8.2f %8.2f %8.2f %12.2f %12.2f %12.2f\n",
  102. clockNumber[i],
  103. wageRate[i],
  104. hours[i],
  105. overtimeHrs[i],
  106. normalPay[i],
  107. overtimePay[i],
  108. grossPay[i]);
  109. }
  110.  
  111. printf("--------------------------------------------------------------------------------\n\n");
  112. return 0;
  113. }
Success #stdin #stdout 0.01s 5316KB
stdin
51.0
42.5
37.0
45.0
0.0
stdout
*** Pay Calculator ***

------------------------------------------------------------
Employee Information
  Clock Number : 98401
  Hourly Wage  : $10.60
Request:
  Please enter the hours worked this week
  (use a dot for decimals, e.g., 40.50): ------------------------------------------------------------
Employee Information
  Clock Number : 526488
  Hourly Wage  : $9.75
Request:
  Please enter the hours worked this week
  (use a dot for decimals, e.g., 40.50): ------------------------------------------------------------
Employee Information
  Clock Number : 765349
  Hourly Wage  : $10.50
Request:
  Please enter the hours worked this week
  (use a dot for decimals, e.g., 40.50): ------------------------------------------------------------
Employee Information
  Clock Number : 34645
  Hourly Wage  : $12.25
Request:
  Please enter the hours worked this week
  (use a dot for decimals, e.g., 40.50): ------------------------------------------------------------
Employee Information
  Clock Number : 127615
  Hourly Wage  : $8.35
Request:
  Please enter the hours worked this week
  (use a dot for decimals, e.g., 40.50): 
Clock#         Wage    Hours   OT Hrs   Normal Pay       OT Pay    Gross Pay
--------------------------------------------------------------------------------
98401         10.60    51.00    11.00       424.00       174.90       598.90
526488         9.75    42.50     2.50       390.00        36.56       426.56
765349        10.50    37.00     0.00       388.50         0.00       388.50
34645         12.25    45.00     5.00       490.00        91.88       581.88
127615         8.35     0.00     0.00         0.00         0.00         0.00
--------------------------------------------------------------------------------