Arithmetic Operator in C : A Beginner's Guide


C Arithmetic operators are used to perform mathematical calculations like addition, subtraction, multiplication, division and modulus in C programs.

  1. Addition(+) : This operator is a binary operator and is used to add two operands.
  2. Subtraction(-) : This operator is a binary operator and is used to subtract two operands.
  3. Multiplication(*) : This operator is a binary operator and is used to multiply two operands.
  4. Division(/) : This is a binary operator that is used to divide the first operand(dividend) by the second operand(divisor) and give the quotient as result.
  5. Modulus(%) : This is a binary operator that is used to return the remainder when the first operand(dividend) is divided by the second operand(divisor).

The code is a C program that calculates the gross salary of an employee based on their basic salary, dearness allowance (DA), and house rent allowance (HRA).Here is an explanation of each line of the code:

  • float bs,da,hra,gs; declares four variables bs, da, hra, and gs of type float. These variables represent the basic salary, dearness allowance, house rent allowance and gross salary respectively.
  • printf("\nEnter Your Basic Salary : "); prints a prompt asking the user to enter their basic salary.
  • scanf("%f",&bs); uses the scanf() function to read a float value from the user and assigns it to the variable bs. The %f is a format specifier that tells scanf() to expect a float input.
  • da=bs*0.4; calculates the dearness allowance (40% of basic salary) and assigns the value to da.
  • hra=bs*0.2; calculates the house rent allowance (20% of basic salary) and assigns the value to hra.
  • gs=bs+da+hra; calculates the gross salary (basic salary + dearness allowance + house rent allowance) and assigns the value to gs.
  • printf("\nBasic Salary : %0.2f",bs); prints the value of bs, which is the basic salary.
  • printf("\nDA : %0.2f",da); prints the value of da, which is the dearness allowance.
  • printf("\nHRA : %0.2f",hra); prints the value of hra, which is the house rent allowance.
  • printf("\nGross Salary : %0.2f",gs); prints the value of gs, which is the gross salary.
  • return 0; The return 0; statement is used to indicate that the main function has completed successfully. The value 0 is returned as the exit status of the program, which indicates a successful execution.

When you run this code, it will prompt the user to enter their basic salary, then it will calculate and print the dearness allowance, house rent allowance and gross salary in the console.

Source Code

#include<stdio.h>
int main()
{
    float bs,da,hra,gs;
    printf("\nEnter Your Basic Salary : ");
    scanf("%f",&bs);
    da=bs*0.4;
    hra=bs*0.2;
    gs=bs+da+hra;
    printf("\nBasic Salary : %0.2f",bs);
    printf("\nDA : %0.2f",da);
    printf("\nHRA : %0.2f",hra);
    printf("\nGross Salary : %0.2f",gs);
    return 0;
}
 
 
To download raw file Click Here

Output

Enter Your Basic Salary : 10000

Basic Salary : 10000.00
DA : 4000.00
HRA : 2000.00
Gross Salary : 16000.00

List of Programs


Sample Programs


Switch Case in C


Conditional Operators in C


Goto Statement in C


While Loop Example Programs


Looping Statements in C

For Loop Example Programs


Array Examples in C

One Dimensional Array


Two Dimensional Array in C


String Example Programs in C


Functions Example Programs in C