SankalandTech Tutorials
  • Twitter
  • Facebook
  • Snapchat
  • Instagram

C Program to Calculate Employee Salary


C Program to Calculate Employee Salary Learning C and looking for a practical application? This program demonstrates how to handle employee data, specifically calculating a monthly salary based on hours worked and hourly rate. You'll see how to accept an employee's ID, their total worked hours, and the amount received per hour, then display the calculated salary formatted to two decimal places. It's a great exercise for mastering basic input/output and arithmetic operations in C!


#include <stdio.h> // Essential for console input (scanf) and output (printf) int main() { char employeeID[11]; // Character array to store Employee ID (Max 10 chars + null terminator) int workedHours; // Total hours worked in the month float hourlyRate; // Amount received per hour (can be decimal) float salary; // Calculated monthly salary // Input Employee ID printf("Input the Employees ID(Max. 10 chars): "); scanf("%s", employeeID); // Reads string input for ID // Input total worked hours printf("Input the working hrs: "); scanf("%d", &workedHours); // Reads integer input for hours // Input salary amount per hour printf("Salary amount/hr: "); scanf("%f", &hourlyRate); // Reads float input for hourly rate // Calculate the salary salary = workedHours * hourlyRate; // Display the result in the specified format printf("Employees ID = %s\n", employeeID); printf("Salary = U$ %.2f\n", salary); // %.2f formats to two decimal places return 0; // Program finished successfully } Test Data & Expected Output: Test Data: Input the Employees ID(Max. 10 chars): 0342 Input the working hrs: 8 Salary amount/hr: 15000 Expected Output: Employees ID = 0342 Salary = U$ 120000.00


How This Program Works: Core Logic & Concepts
This C program clearly demonstrates how to handle different data types for practical calculations. It focuses on taking various inputs and presenting a formatted output.

Variables:
char employeeID[11]: Stores the employee's ID as a string (an array of characters). [11] allows for up to 10 characters plus a null terminator (\0), which marks the end of the string.
int workedHours: Stores the total hours as a whole number (int).
float hourlyRate: Stores the hourly pay as a floating-point number (float), allowing for decimal values.
float salary: Stores the calculated salary, also as a float, to handle decimal cents.

Input (scanf):
scanf("%s", employeeID): Reads the employee ID. %s is used for reading strings. Important: When reading a string with %s, you do not use the & (address-of) operator before the array name, as the array name itself already represents its starting address.
scanf("%d", &workedHours): Reads the integer hours. %d is for integers.
scanf("%f", &hourlyRate): Reads the floating-point hourly rate. %f is for floats.

Calculation:
salary = workedHours * hourlyRate;: A straightforward multiplication to get the total salary. C automatically handles the multiplication of int and float by promoting the int to a float before calculation.

Output (printf):
printf("Employees ID = %s\n", employeeID);: Prints the stored employeeID. %s is used to display strings.
printf("Salary = U$ %.2f\n", salary);: Prints the calculated salary.
The %.2f format specifier is crucial here:
%f: Indicates a floating-point number.
.2: Specifies that the number should be displayed with exactly two digits after the decimal point, which is ideal for currency.
U$: Simply prints the currency symbol as a literal string.
This program provides a solid foundation for handling different data types, user input, calculations, and formatted output—all essential skills in C programming.



Conclusion
You've successfully created a practical C program to calculate an employee's monthly salary! This exercise reinforces key C concepts like handling diverse input types (strings, integers, floats), performing basic arithmetic, and formatting output for readability. It's a fundamental skill with direct application in real-world data processing. Keep experimenting with different input values, or consider enhancing it to handle multiple employees or calculate taxes!


Basic and Operators practice program assignments in C Language :
Practice/Assignment set 1:

1.Write a program in C to Input integer, float and character values using one scanf().
2.Write a program in C language to find the Area of Circle.
3.Write a program in C language to Input an integer value and print with padding by Zeros in C
4. Write a program in C language to Input float value and print it with specified digit after decimal point in C
5.Write a program in C language to find sum of the digits of any three digit number.
6. Write a program in C language to Input an unsigned integer value using scanf().
7.Write a program in C language to Find area of Triangle
8.Write a program in C language to Input a hexadecimal value using scanf() in C
9.Write a program in C language to Input octal value using scanf()
10.Write a program in C language to Convert temperature given in farenheight to degree celcius.
11.Write a program in C language to Input octal value using scanf()
12.Write a program in C language to Input decimal, octal and hexadecimal values in character variables using scanf() in C
13.Write a program in C language to Input an integer value in any format (decimal, octal or hexadecimal) using '%i' in C
14.Write a program in C language to Input side value and find area of Square.
15.Write a program in C language to Input individual characters using scanf() in C
16.Write a program in C language to Read a memory address using scanf() and print its value in C
17.Write a program in C language to Skip characters while reading integers using scanf() in C
18.Print your name, date of birth and mobile number
19. Write a C program to convert specified days into years, weeks and days. Note: Ignore leap year.

21.Write a C program that accepts two item's weight and number of purchases (floating point values) and calculates their average value.

22.Write a C program that accepts an employee's ID, total worked hours in a month and the amount he received per hour. Print the ID and salary (with two decimal places) of the employee for a particular month.
Test Data :
Input the Employees ID(Max. 10 chars): 0342
Input the working hrs: 8
Salary amount/hr: 15000
Expected Output:
Employees ID = 0342
Salary = U$ 120000.00


23.Write a C program that accept an students roll number and six subject markes, find total and average marks.

24.Write a C program that accepts an employee's ID, basic salary in a month and calculate total salary.
The formulat for
Total salary=(basic salary-tax)+House Rent allowance+Bonus+Traveling allowance+other allowance
tax 10% of basic salary
House Rent allowance 17% of basic salary
Bonus 7% of basic salary
Traveling allowance 8% of basic salary
other allowance 10% of basic salary
25.Write a C program to convert a given integer (in days) to years, months and days, assuming that all months have 30 days and all years have 365 days.
Test Data :
Input no. of days: 2535
Expected Output:
6 Year(s)
11 Month(s)
15 Day(s)


Practice/Assignment set 2:
1. C program to find sum of the square of all natural numbers from 1 to N.
Series: 1^2+2^2+3^2+4^2+..N^2
2. C program to find sum of the all natural numbers from 1 to N.
Series: 1+2+3+4+..N
3.C program to find the sum of Natural Number/Factorial of Number of all natural numbers from 1 to N.
Series: 1/1! + 2/2! + 3/3! + 4/4! + ... N/N!
4) C program to find sum of following series:
1+ 1/2 + 1/3 + 1/4 + 1/5 + .. 1/N
5) C program to find sum of following series:
1 + 3^2/3^3 + 5^2/5^3 + 7^2/7^3 + ... till N terms


Previous Topic:-->> Write C program to show EVEN numbers from 1 to N using while loop and do while loop. || Next topic:-->>Write a program in C to print all uppercase alphabets using do while and while loop.

C Language

  • Home
  • Why C Language
  • History of C Language
  • Applications of C Language
  • Introduction To C
    • What is Program?
    • Structure of C Program
    • Working Of C Program
    • CHARACTER SET
    • VARIABLES AND IDENTIFIERS
    • BUILT-IN DATA TYPES
    • OPERATORS AND EXPRESSIONS
    • CONSTANTS AND LITERALS
    • SIMPLE ASSIGNMENT STATEMENT
    • BASIC INPUT/OUTPUT STATEMENT
    • SIMPLE 'C' PROGRAMS
    • Assignments
  • Operators in C Programming
    • Arithmetic Operators
    • Assignment Operators
    • Increment and Decrement Operators
    • Relational Operators
    • Logical Operators
    • Bitwise Operators
    • Other Operators
    • Assignments
  • CONDITIONAL STATEMENTS
    • DECISION MAKING WITHIN A PROGRAM
    • CONDITIONS
    • IF STATEMENT
    • IF-ELSE STATEMENT
    • IF-ELSE LADDER
    • NESTED IF-ELSE
    • SWITCH CASE
    • Assignments
  • LOOPS STATEMENTS
    • Introduction to Loops
    • GO TO Statement
    • Do while Loop
    • While Loop
    • Nested While Loop
    • Difference Between While and Do while
    • Difference Between Goto and loop
    • while loop assignments
    • C FOR Loop
    • C For loop examples
    • Nested for loop
    • Nested for loop examples
    • Infinite while Loops
    • Infinite for Loops
    • Continue in Loops
    • break in Loops
    • difference while do..while & for
    • Assignments
  • Arrays
    • One Dimensional Array
    • Declaring 1D Arrays
    • Initilization of 1D arrays
    • Accessing element of one 1D Array
    • Read and Display 1D Arrays
    • Two Dimensional Arrays
    • Declare 2D Arrays
    • Read and Display 2D Arrays
    • Assignments/Examples
  • Functions
    • Introduction
    • Need For User-Defined Function
    • Multiple Function Program
    • Modular Programming
    • Elements Of User Defined Function
    • Function Definition
    • Function Declaration
    • Types of functions
    • Nesting of Function
    • Recursion
    • Passing Array To Functions
    • Scope,Visibility and Lifetime of Variables
    • Assignments
  • Structure
    • Introduction
    • Array vs Structure
    • Defining Structure
    • Declaring Structure Variables
    • Type Defined Structure
    • Accessing Structure Members
    • Structure Initilization
    • Copying & Comparing Structure Variables
    • Array of Structure
    • Arrays Within Structure
    • Structures Within Structures
    • Structures and Functions
    • Structure Examples/Assignments
  • Union
    • Define Union
    • Create and use Union
    • Difference Between Structure and Union
    • Union Examples
    • Union FAQ
  • Pointers
    • What Are Pointers In C?
    • How Do We Use Pointers In C?
    • Declaration Of A Pointer
    • The Initialization Of A Pointer
    • Syntax Of Pointer Initialization
    • Use Of Pointers In C
    • The Pointer To An Array
    • The Pointer To A Function
    • The Pointer To A Structure
    • Types Of Pointers
    • The Null Pointer
    • The Void Pointer
    • The Wild Pointer
    • The Near Pointer
    • The Huge Pointer
    • The far Pointer
    • dangling pointer
    • Accessing Pointers- Indirectly And Directly
    • Pros Of Using Pointers In C
    • Cons Of Pointers In C
    • Applications Of Pointers In C
    • The & Address Of Operator In C
    • How To Read The Complex Pointers In C?
    • Practice Problems On Pointers
  • File Processiong
    • File Handling In C
    • Types Of Files In C
    • Operations Done In File Handling
    • File Examples
    • Binary Files
    • count words,lines in a file
    • Copy files
    • Update File
    • count vowels in a file
  • Preprocessor
    • Macro substitution division
    • File Inclusion
    • Conditional Compilation
    • Other directives
    • Examples
  • Dynamic Memory Allocation
    • malloc
    • calloc
    • free
    • realloc
    • Examples
  • Storage Classes
  • Graphics
  • Frequently Asked Interview Questions (FAQ)
    • Introduction To C FAQ
    • Operators FAQ
    • Conditional Statements FAQ
    • Loops FAQ
    • Arrays FAQ
    • Function FAQ
    • Structure FAQ
    • Pointers FAQ
    • Files FAQ
    • Storage classes FAQ
    • Dynamic Memory FAQ
  • Programs/Assignments
    • Introduction To C
    • Operators
    • Conditional Statements
    • Loops
    • Arrays
    • Function
    • Structure
    • Pointers
    • Files
    • Storage classes
    • Dynamic Memory
  • Case Studies
  • Multiple Choice Questions
    • Introduction To C MCQ
    • Operators MCQ
    • Conditional Statements MCQ
    • Loops MCQ
    • Arrays MCQ
    • Function MCQ
    • Structure MCQ
    • Pointers MCQ
    • Files MCQ
    • Storage classes MCQ
    • Dynamic Memory MCQ
    • More MCQ

Get in touch

  • tech2dsm@gmail.com

© Sankalan Data Technologies. All rights reserved.