Skip to content
TheChatArchiveThe Chat Archive
gpt-4o-2024-05-13· OpenAIAi2 WildChat-4.8M (AI2)

Implement a program in c language for loan approval system that determines whether a…

  1. User

    Implement a program in c language for loan approval system that determines whether a loan application should be approved, denied, or sent for manual review based on several input conditions. Design test cases to determine the loan status using a decision table.

  2. Assistant

    To implement a loan approval system in C, we should define the conditions under which a loan will be approved, denied, or sent for manual review. Here's a sample set of conditions we can use:

    1. Credit Score: If the score is above 700, the loan can be approved.
    2. Income: If the income is above $50,000, the loan can be approved.
    3. Loan Amount: If the requested amount is less than 50% of the annual income, the loan can be approved.
    4. Employment Status: If the applicant is employed, the loan can be approved.

    Based on these conditions, we can create a decision table and a C program to determine the loan status:

    Decision Table

    Credit ScoreIncomeLoan AmountEmploymentResult
    > 700> 50000< 50% of incomeEmployedApproved
    <= 700> 50000< 50% of incomeEmployedManual Review
    > 700<= 50000< 50% of incomeEmployedManual Review
    > 700> 50000>= 50% of incomeEmployedManual Review
    > 700> 50000< 50% of incomeUnemployedManual Review
    ............Denied / Manual ...

    The table can be expanded and more detailed with any other set of rules.

    C Program Implementation

    Here's a simple implementation based on this decision table:

    #include <stdio.h>
    
    enum LoanStatus {
        APPROVED,
        DENIED,
        MANUAL_REVIEW
    };
    
    enum EmploymentStatus {
        EMPLOYED,
        UNEMPLOYED
    };
    
    enum LoanStatus evaluateLoan(int creditScore, double income, double loanAmount, enum EmploymentStatus employmentStatus) {
        if (creditScore > 700 && income > 50000 && loanAmount < (0.5 * income) && employmentStatus == EMPLOYED) {
            return APPROVED;
        } else if (creditScore <= 700 || income <= 50000 || loanAmount >= (0.5 * income) || employmentStatus == UNEMPLOYED) {
            return MANUAL_REVIEW;
        }
        return DENIED;
    }
    
    const char* getStatusString(enum LoanStatus status) {
        switch (status) {
            case APPROVED: return "Approved";
            case DENIED: return "Denied";
            case MANUAL_REVIEW: return "Manual Review";
            default: return "Unknown";
        }
    }
    
    int main() {
        // Sample test cases
        struct {
            int creditScore;
            double income;
            double loanAmount;
            enum EmploymentStatus employmentStatus;
            const char* expectedStatus;
        } testCases[] = {
            {750, 60000, 20000, EMPLOYED, "Approved"},
            {650, 60000, 20000, EMPLOYED, "Manual Review"},
            {750, 40000, 20000, EMPLOYED, "Manual Review"},
            {750, 60000, 35000, EMPLOYED, "Manual Review"},
            {750, 60000, 20000, UNEMPLOYED, "Manual Review"},
            // Add more test cases here...
        };
    
        for (int i = 0; i < sizeof(testCases)/sizeof(testCases[0]); i++) {
            enum LoanStatus result = evaluateLoan(testCases[i].creditScore, testCases[i].income, testCases[i].loanAmount, testCases[i].employmentStatus);
            printf("Test Case %d: Expected: %s, Got: %s\n", i + 1, testCases[i].expectedStatus, getStatusString(result));
        }
    
        return 0;
    }
    

    Explanation

    • Loan Status Enum: We define an enumeration to represent the possible loan statuses: Approved, Denied, and Manual Review.
    • Employment Status Enum: We define an enumeration to represent employment status: Employed and Unemployed.
    • evaluateLoan Function: This function takes in the credit score, income, loan amount, and employment status, and returns the loan status based on the predefined conditions.
    • getStatusString Function: Converts the enumeration value to a string for easy printing.
    • Main Function: Defines a set of test cases and uses the evaluateLoan function to check the results against expected results.

    This implementation covers some simple rules and test cases, which can be expanded based on more specific business requirements.