Saturday, March 15, 2014

C Program to calculate gross salary (DA=40%, HRA=20%, Input Salary)

Problem Question


Ramesh's basic salary is input through the keyboard. His dearness allowance is 40% of basic salary, and house rent allowance is 20% of basic salary. Write a program to calculate his gross salary.

Explanation of Problem


We got a very simple problem at hand. We need the user to enter a value which is Ramesh's Basic Salary. Then we have to add to it, it's 40% and 20% to get the gross salary.

Code


/*Ramesh's Gross Salary *
*@Language: ANSI C *
*@Compiler: GNU GCC *
*@IDE: CodeBlocks 12.11 *
*@Author: Toxifier *
*@URL: http://letsplaycoding.blogspot.com/ *
*@Date: 15-03-2014 **/

#include <stdio.h>

int main()
{
    printf("\n\nEnter Ramesh's salary: ");
    int rameshSalary = 0;
    scanf("%d", &rameshSalary);
    printf("Dearness allowance: 40 percent and House Rent allowance: 20 percent\nCalculating...\n\n");
    float grossSalary = rameshSalary + rameshSalary * 0.2 + rameshSalary*0.4;
    printf("Ramesh's gross salary = %f\n", grossSalary);
    system("pause");
    return 0;
}

Explanation of Code


#include <stdio.h> -> This statement calls the C Preprocessor to link the STDIO header file which has some basic functions we use in any C program, like printf and scanf.

int main() -> The starting point/entry point of the program execution. We use this function to call any other function in a C program. In ANSI C specification, main must return an integer value, that's why the return type is 'int'.

printf() -> This function takes as argument a string and displays that on the user's terminal. The first argument is the string to be displayed, while rest of the arguments are different values(generally variables/constants declared in our program) which are to be displayed.

scanf() -> This function makes the program to wait until the user enters some value and presses 'space' or 'enter' key. This is is a standard input function which takes as argument, a string and the addresses of the memory where to store the value entered by the user.

&rameshSalary -> Denotes the address of he variable 'rameshSalary'.

system("pause") -> I use this function to display the Press any key to continue message. Please note, this function may or may not work on your compiler, and it's use is completely optional. If you use CodeBlocks, you won't need this in this program. If you use DevC++, then you'll need this, and if you use TurboC then this function won't work at all!

return 0 -> In ANSI C, main generally returns zero when there is a normal execution and termination of the program.

Output(s)



1 comment:

  1. informative post! I really like and appreciate your work, thank you for sharing such a useful facts and information about pay policy hrm strategies, keep updating the blog, hear i prefer some more information about jobs for your career hr jobs in hyderabad .

    ReplyDelete

Need help?