Header Ads

Write a C++ Program to Print Employee Pay Using Function | Query Point Official

C++ Program: Print Employee Basic Pay, House Rent Allowance & Total Pay Using Function

Write a C++ program to prompt the user to enter the Scale and then print the Employee’s Basic Pay, House Rent Allowance and Total Pay by using a function.

 

Calculations are given as:

Scale

Salary

House Rent(% of salary)

Utility Allowance

1

50000

10

3000

2

70000

10

5000

3

90000

10

7000






Program:

#include <iostream>

using namespace std;


int employee(int scale){

    int salary, houseRent, allowance, totalPay;

    switch(scale)

    {

        case 1:

            salary = 50000;

            houseRent = salary * 10/100;

            allowance = 3000;

            totalPay = salary + houseRent + allowance;

            cout<<"The Basic Salary is     : "<< salary<<"\n"

                <<"The House Rent is       : "<<houseRent<<"\n"

                <<"The Utility Allowance is: "<< allowance<<"\n"

                <<"Net Payable Salary is   : "<< totalPay;

            break;

        case 2:

            salary = 70000;

            houseRent = salary * 10/100;

            allowance = 5000;

            totalPay = salary + houseRent + allowance;

        cout<<"The Basic Salary is     : "<< salary<<"\n"

                <<"The House Rent is       : "<<houseRent<<"\n"

                <<"The Utility Allowance is: "<< allowance<<"\n"

                <<"Net Payable Salary is   : "<< totalPay;

            break;

        case 3:

            salary = 90000;

            houseRent = salary * 10/100;

            allowance = 7000;

            totalPay = salary + houseRent + allowance;

            cout<<"The Basic Salary is     : "<< salary<<"\n"

                <<"The House Rent is       : "<<houseRent<<"\n"

                <<"The Utility Allowance is: "<< allowance<<"\n"

                <<"Net Payable Salary is   : "<< totalPay;

            break;

        default:

            cout<<"Incorrect Range... "<<"\n"<<"Enter the scale of employee(1-3)";

            break;

    }

    

}

int main() {

    int scale;

    cout << "Enter your scale (1-3): ";

    cin >> scale;

    employee(scale);

    return 0;

}


OUTPUT


If enter "1"

If enter "2"

If enter "3"


If enter "any other number"



Frequently Asked Questions (FAQs)

What does using namespace std do?

It imports all names in the std namespace into the current scope so you can use standard library objects like cout without prefixing std::. :contentReference[oaicite:1]{index=1}

Is it necessary to use using namespace std?

No. You can instead write std::cout and std::cin. Using the using directive is common in small programs but is generally not recommended in large projects. :contentReference[oaicite:2]{index=2}

How does the function employee() work?

The function takes the scale as input, uses a switch statement to determine pay components, and prints the results. It returns no value but prints directly to the console.

What happens if the user enters a number outside 1–3?

The default case of the switch statement prints an error message asking for a valid scale (1–3).

Why use a function in this program?

Using a function organizes the code and makes it reusable. It separates logic for calculating pay from the main input/output logic.


Explore more about INTRODUCTION TO PROGRAMMING in Computer Science Notes & MCQs.

Query Point Official – Smart Notes for Exams & Conceptual Learning

No comments

Powered by Blogger.