C
  • Introduction
    • Fundamentals of a Program
    • Overview of C
    • Features of C
  • Installing Required Software
    • Setting Up VSCode for Windows
    • Setting Up VSCode for macOS
    • Setting Up VSCode for Ubuntu
  • Starting to write code
    • Compiling and Running Your Code
    • Creating Our First C Program
    • Errors and Warnings
    • Program: Writing a C Program to Display Your Name
    • Structure of a C Program
  • Basic Concepts
    • Comments in C
    • Preprocessor in C
    • The #include Statement
    • Displaying Output
    • Reading Input from the Terminal
    • Enums and Chars
    • Data Types and Variables
    • Format Specifiers
    • Command Line Arguments
    • Program: Calculating the Area of a Triangle
  • Operators
    • Converting Minutes to Years and Days
    • Basic Operators
    • Bitwise Operators
    • Program: Byte Sizes of Basic Data Types
    • cast and sizeof Operators
    • Operator Precedence
  • Control Flow
    • If-Else Statements
    • Program: Weekly Pay Calculation
    • Switch Statement
    • For Loop
    • While and Do-While Loops
    • Nested Loops and Loop Control
    • Program: Guess the Number
  • Arrays
    • Introduction to Arrays
    • Program: Prime Number Generator
    • Multidimensional Arrays
    • Program: simple Weather Program
    • Variable Length Arrays (VLAs)
  • Functions
    • Overview of Functions
    • Defining Functions
    • Arguments and Parameters
    • Returning Data from Functions
    • Variable Scoping
    • Program: Tic Tac Toe Game
    • Recursion
  • Strings
    • Defining a String
    • Constant Strings in C
    • Common String Functions
    • Program: Bubble Sort
    • Searching, Tokenizing, and Analyzing Strings
    • Converting Strings
  • Debugging
    • What is Debugging
    • Understanding the Call Stack
    • Common C Mistakes
    • Understanding Compiler Errors
  • Pointer
    • Defining Pointers
    • Accessing Pointers
    • Program: Pointer Demonstration
    • Pointers and Const
    • Void Pointers
    • String Pointers
    • Array Pointers
    • Utilizing Pointers with Functions
    • Pointer Arithmetic
  • Dynamic Memory Allocation
    • malloc, calloc, and realloc
    • Program: User Input String
    • Memory Deallocation
  • Structure
    • Structures and Arrays
    • Nested Structures
    • Structures and Pointers
    • Structures and Functions
    • Program: Structure pointers and Functions
  • File Input and Output
    • Accessing Files
    • Reading from a File
    • Program: Finding the Total Number of Lines in a Text File
    • Writing to a Text File
    • Finding Your Position in a File
    • Program: Converting Characters in a File to Uppercase
    • Program: Printing the Contents of a File in Reverse Order
  • The Standard C Library
    • Various Functions in C
    • Math Functions in C
    • Utility Functions in C
Powered by GitBook
On this page
  • Instructions
  • Algorithm
  • Pseudocode
  • Program

Was this helpful?

  1. Control Flow

Program: Weekly Pay Calculation

Instructions

In this programming assignment, you are tasked with creating a C program that calculates the weekly pay of an employee. The program should prompt the user to input the number of hours worked in a week via the keyboard. The output should display the gross pay, taxes, and net pay. Assumptions for the calculations are as follows:

  • Basic pay rate: $12.00 per hour.

  • Overtime (in excess of 40 hours): Time and a half.

  • Tax rates:

    • 15% of the first $300.

    • 20% of the next $150.

    • 25% of the rest.

Your goal is to implement this program in C and provide a clear, well-documented solution.

Algorithm

  1. Input:

    • Prompt the user to enter the number of hours worked in a week.

  2. Calculate Gross Pay:

    • If the hours worked are less than or equal to 40, calculate the gross pay using the basic pay rate.

    • If the hours worked exceed 40, calculate the gross pay with time and a half for overtime.

  3. Calculate Taxes:

    • Determine the tax amount based on different income thresholds and tax rates.

  4. Calculate Net Pay:

    • Subtract the calculated taxes from the gross pay to obtain the net pay.

  5. Output:

    • Display the Weekly Payroll Summary, including Gross Pay, Taxes, and Net Pay.

Pseudocode

CONSTANTS:
    basicPayRate = 12.00
    overtimeRate = 1.5
    taxRate1 = 0.15
    taxRate2 = 0.20
    taxRate3 = 0.25
    taxThreshold1 = 300.0
    taxThreshold2 = 150.0

VARIABLES:
    hoursWorked, grossPay, taxes, netPay

INPUT:
    Prompt user to enter hoursWorked

CALCULATE GROSS PAY:
    IF hoursWorked <= 40:
        grossPay = hoursWorked * basicPayRate
    ELSE:
        grossPay = (40 * basicPayRate) + ((hoursWorked - 40) * basicPayRate * overtimeRate)

CALCULATE TAXES:
    IF grossPay <= taxThreshold1:
        taxes = grossPay * taxRate1
    ELSE IF grossPay <= (taxThreshold1 + taxThreshold2):
        taxes = (taxThreshold1 * taxRate1) + ((grossPay - taxThreshold1) * taxRate2)
    ELSE:
        taxes = (taxThreshold1 * taxRate1) + (taxThreshold2 * taxRate2) + ((grossPay - taxThreshold1 - taxThreshold2) * taxRate3)

CALCULATE NET PAY:
    netPay = grossPay - taxes

OUTPUT:
    Display Weekly Payroll Summary:
        Gross Pay: grossPay
        Taxes: taxes
        Net Pay: netPay

Program

#include <stdio.h>

int main() {
    // Constants
    const float basicPayRate = 12.00;
    const float overtimeRate = 1.5;
    const float taxRate1 = 0.15;
    const float taxRate2 = 0.20;
    const float taxRate3 = 0.25;
    const float taxThreshold1 = 300.0;
    const float taxThreshold2 = 150.0;

    // Variables
    float hoursWorked, grossPay, taxes, netPay;

    // Input: Get the number of hours worked
    printf("Enter the number of hours worked in a week: ");
    scanf("%f", &hoursWorked);

    // Calculate gross pay
    if (hoursWorked <= 40) {
        grossPay = hoursWorked * basicPayRate;
    } else {
        grossPay = 40 * basicPayRate + (hoursWorked - 40) * basicPayRate * overtimeRate;
    }

    // Calculate taxes
    if (grossPay <= taxThreshold1) {
        taxes = grossPay * taxRate1;
    } else if (grossPay <= (taxThreshold1 + taxThreshold2)) {
        taxes = taxThreshold1 * taxRate1 + (grossPay - taxThreshold1) * taxRate2;
    } else {
        taxes = taxThreshold1 * taxRate1 + taxThreshold2 * taxRate2 + (grossPay - taxThreshold1 - taxThreshold2) * taxRate3;
    }

    // Calculate net pay
    netPay = grossPay - taxes;

    // Output: Display the results
    printf("\nWeekly Payroll Summary:\n");
    printf("Gross Pay: $%.2f\n", grossPay);
    printf("Taxes: $%.2f\n", taxes);
    printf("Net Pay: $%.2f\n", netPay);

    return 0;
}

This C program calculates weekly pay based on the provided requirements and displays the results in a structured format.

PreviousIf-Else StatementsNextSwitch Statement

Was this helpful?