Calcular uma Etapa de Amortização de Empréstimo em C

CBeginner
Pratique Agora

Introdução

Neste laboratório, você aprenderá a calcular um único passo de amortização de empréstimo em C. O laboratório cobre os passos fundamentais para o cálculo da amortização de empréstimos, incluindo a leitura do valor principal, taxa de juros e valor do pagamento, bem como o cálculo da parte de juros e da parte principal do pagamento. Ao final deste laboratório, você terá uma compreensão melhor da matemática financeira e dos cálculos de juros usando a programação em C.

Ler Principal, Taxa e Pagamento

Neste passo, você aprenderá como inserir e ler os parâmetros fundamentais para o cálculo da amortização de empréstimos em C: valor principal, taxa de juros e valor do pagamento.

Primeiro, vamos criar um arquivo de origem C para nosso programa de amortização de empréstimos:

cd ~/project
nano loan_amortization.c

Agora, adicione o seguinte código ao arquivo:

#include <stdio.h>

int main() {
    double principal, rate, payment;

    // Ler o valor principal
    printf("Digite o valor principal do empréstimo: ");
    scanf("%lf", &principal);

    // Ler a taxa de juros anual
    printf("Digite a taxa de juros anual (em percentagem): ");
    scanf("%lf", &rate);

    // Ler o valor do pagamento mensal
    printf("Digite o valor do pagamento mensal: ");
    scanf("%lf", &payment);

    // Exibir os valores inseridos
    printf("\nDetalhes do Empréstimo:\n");
    printf("Principal: $%.2f\n", principal);
    printf("Taxa de Juros Anual: %.2f%%\n", rate);
    printf("Pagamento Mensal: $%.2f\n", payment);

    return 0;
}

Compile e execute o programa:

gcc loan_amortization.c -o loan_amortization
./loan_amortization

Exemplo de saída:

Digite o valor principal do empréstimo: 10000
Digite a taxa de juros anual (em percentagem): 5.5
Digite o valor do pagamento mensal: 200

Detalhes do Empréstimo:
Principal: $10000.00
Taxa de Juros Anual: 5.50%
Pagamento Mensal: $200.00

Calcular a Parte de Juros e a Parte Principal

Neste passo, você aprenderá a calcular as partes de juros e principal de um pagamento de empréstimo usando fórmulas de matemática financeira.

Abra o arquivo anterior loan_amortization.c e modifique-o para incluir a lógica de cálculo:

cd ~/project
nano loan_amortization.c

Atualize o código com as funções de cálculo:

#include <stdio.h>

// Função para calcular os juros mensais
double calculateMonthlyInterest(double principal, double annualRate) {
    double monthlyRate = annualRate / 12 / 100;
    return principal * monthlyRate;
}

// Função para calcular a parte principal
double calculatePrincipalPortion(double payment, double monthlyInterest) {
    return payment - monthlyInterest;
}

int main() {
    double principal, rate, payment;
    double monthlyInterest, principalPortion;

    // O código de entrada anterior permanece o mesmo
    printf("Digite o valor principal do empréstimo: ");
    scanf("%lf", &principal);

    printf("Digite a taxa de juros anual (em percentagem): ");
    scanf("%lf", &rate);

    printf("Digite o valor do pagamento mensal: ");
    scanf("%lf", &payment);

    // Calcular as partes de juros e principal
    monthlyInterest = calculateMonthlyInterest(principal, rate);
    principalPortion = calculatePrincipalPortion(payment, monthlyInterest);

    // Exibir os resultados do cálculo
    printf("\nDetalhes do Pagamento do Empréstimo:\n");
    printf("Juros Mensais: $%.2f\n", monthlyInterest);
    printf("Parte Principal: $%.2f\n", principalPortion);
    printf("Principal Restante: $%.2f\n", principal - principalPortion);

    return 0;
}

Compile e execute o programa atualizado:

gcc loan_amortization.c -o loan_amortization
./loan_amortization

Exemplo de saída:

Digite o valor principal do empréstimo: 10000
Digite a taxa de juros anual (em percentagem): 5.5
Digite o valor do pagamento mensal: 200

Detalhes do Pagamento do Empréstimo:
Juros Mensais: $45.83
Parte Principal: $154.17
Principal Restante: $9845.83

In this step, you will learn how to track and print the updated principal balance after each loan payment, demonstrating the loan amortization process.

Open the previous loan_amortization.c file and modify it to include multiple payment iterations:

cd ~/project
nano loan_amortization.c

Update the code to simulate multiple loan payments:

#include <stdio.h>

// Previous functions remain the same
double calculateMonthlyInterest(double principal, double annualRate) {
    double monthlyRate = annualRate / 12 / 100;
    return principal * monthlyRate;
}

double calculatePrincipalPortion(double payment, double monthlyInterest) {
    return payment - monthlyInterest;
}

int main() {
    double principal, rate, payment;
    int totalPayments;

    // Input loan details
    printf("Enter the loan principal amount: ");
    scanf("%lf", &principal);

    printf("Enter the annual interest rate (in percentage): ");
    scanf("%lf", &rate);

    printf("Enter the monthly payment amount: ");
    scanf("%lf", &payment);

    printf("Enter the total number of payments: ");
    scanf("%d", &totalPayments);

    // Print initial loan details
    printf("\nInitial Loan Details:\n");
    printf("Principal: $%.2f\n", principal);
    printf("Annual Interest Rate: %.2f%%\n", rate);
    printf("Monthly Payment: $%.2f\n\n", payment);

    // Simulate loan amortization
    for (int month = 1; month <= totalPayments; month++) {
        double monthlyInterest = calculateMonthlyInterest(principal, rate);
        double principalPortion = calculatePrincipalPortion(payment, monthlyInterest);

        // Update principal
        principal -= principalPortion;

        // Print monthly breakdown
        printf("Payment %d:\n", month);
        printf("Monthly Interest: $%.2f\n", monthlyInterest);
        printf("Principal Portion: $%.2f\n", principalPortion);
        printf("Remaining Principal: $%.2f\n\n", principal);
    }

    return 0;
}

Compile and run the updated program:

gcc loan_amortization.c -o loan_amortization
./loan_amortization

Example output:

Enter the loan principal amount: 10000
Enter the annual interest rate (in percentage): 5.5
Enter the monthly payment amount: 200
Enter the total number of payments: 3

Initial Loan Details:
Principal: $10000.00
Annual Interest Rate: 5.50%
Monthly Payment: $200.00

Payment 1:
Monthly Interest: $45.83
Principal Portion: $154.17
Remaining Principal: $9845.83

Payment 2:
Monthly Interest: $45.04
Principal Portion: $154.96
Remaining Principal: $9690.87

Payment 3:
Monthly Interest: $44.25
Principal Portion: $155.75
Remaining Principal: $9535.12

Resumo

Neste laboratório, você aprenderá a ler os parâmetros fundamentais para o cálculo da amortização de empréstimos em C, incluindo o valor principal, a taxa de juros e o valor do pagamento. Você também aprenderá a calcular as partes de juros e principal de um pagamento de empréstimo usando fórmulas de matemática financeira. Ao final deste laboratório, você terá uma compreensão básica dos passos envolvidos no cálculo de uma única etapa de amortização de empréstimo em C.