ADVERTISEMENT
Calculate Bonus On Salary
This code determines the bonus and updated salary for an individual based on their gender and initial salary. It demonstrates the use of conditional statements in C for making decisions based on user input.
#include<stdio.h>
#include<string.h>
main()
{
int i,j;
float salary,bonus;
char gender;
printf("Enter M for Male and F for Female\n");
scanf("%c",&gender);
printf("Enter Salary\n");
scanf("%f",&salary);
if(gender=='M' || gender=='m')
{
if(salary>10000)
bonus=(float)(salary*0.05);//0.05--5%
else
bonus=(float)(salary*0.07);
}
if(gender=='F' || gender=='f')
{
if(salary>10000)
bonus=(float)(salary*0.1);//0.1--10%
else
bonus=(float)(salary*0.12);
}
salary+=bonus;
printf("Bonus=%.2f\nSalary=%.2f\n",bonus,salary);
}C Programming ExamplesOUTPUT
Enter M for Male and F for Female
m
Enter Salary
75000
Bonus=3750.00
Salary=78750.00C Programming ExamplesExplanation:
This C program calculates the bonus and updated salary based on gender (Male or Female) and the initial salary. Here’s a step-by-step explanation of the code:
Header Files:
- The program includes two standard C library headers: <stdio.h> for input and output operations and <string.h> for string manipulation, although the latter is not used in the code.
Main Function:
- The main function is the entry point of the program.
Variable Declaration:
Three variables are declared:
- gender (character): To store the gender, ‘M’ for Male and ‘F’ for Female.
- salary (float): To store the initial salary.
- bonus (float): To store the calculated bonus.
User Input:
- The program uses printf and scanf functions to take user input. It prompts the user to enter their gender (‘M’ for Male or ‘F’ for Female) and their salary.
- Bonus Calculation:
Bonus Based on the Following Conditions:
- If the gender is Male (‘M’ or ‘m):
- If the salary is greater than 10,000, the bonus is 5% of the salary.
- If the salary is 10,000 or less, the bonus is 7% of the salary.
- If the gender is Female (‘F’ or ‘f):
- If the salary is greater than 10,000, the bonus is 10% of the salary.
- If the salary is 10,000 or less, the bonus is 12% of the salary.
Updating Salary:
- The bonus is added to the initial salary to calculate the updated salary.
Output:
- The program prints the calculated bonus and updated salary to the console using printf. The values are displayed with two decimal places.
End of Program:
- After printing the results, the program reaches the end of the main function, concluding the program’s execution.