将显示编号的C程序。月份,期末余额和每月到期

时间:2018-08-25 03:34:11

标签: c

我应该写一个程序,输入以下内容:
-贷款额
-贷款期限
-年利率
那么它将以列类型显示month numberending balancemonthly due

我的程序是:

#include<stdio.h>

int main() 
{
    int mn;
    float la, y, m1, md, p, eb, i, x, z;
    printf("Enter the loan amount:");
    scanf("%f",&la);
    printf("Enter the desired load term(years):");
    scanf("%f", &y);
    m1=y*12;
    md=la/m1;
    printf("Enter the annual interest rate:");
    scanf("%f",&z);
    i=z/100;
    x=(la*i*y)/m1;
    p=md+x;
    printf("Month no.\tMonthly due\tEnding balance\n");
    for(eb=la-md; eb>=-1; eb-=md){
        for(mn=1; mn<=m1; mn++)
            printf("%d\t\t%.2f%\t\t%.2f\n",mn,p,eb);     
    }
    getch();
    return 0;
} 

请帮助!

4 个答案:

答案 0 :(得分:0)

enter code here
 int main() {
           int mn,a;
           float la, y, m1, md, p, eb, i, x, z,b,c;
           printf("Enter the loan amount:");
           scanf("%f",&la);
           printf("Enter the desired load term(years):");
           scanf("%f", &y);
           m1=y*12;
           md=la/m1;
           printf("Enter the annual interest rate:");
           scanf("%f",&z);
           i=z/100;
           x=(la*i*y)/m1;
           p=md+x;
           b=la+(i*la);
           printf("Month no.\tMonthly due\tEnding balance\n");
           for(a=1;a<=48;a++)
            {
              c=b-(p*a);

          printf("%d\t\t%f%\t\t%f\n",a,p,c);

   }
   getch();
return 0;
}

答案 1 :(得分:0)

请检查以下代码:

mScrollView.getViewTreeObserver().addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {
        @Override
        public void onGlobalLayout() {
            mScrollView.post(new Runnable() {
                public void run() {
                    mScrollView.fullScroll(View.FOCUS_DOWN);
                }
            });
        }
    });

这是输出:

#include<stdio.h>

#define TOTAL_MONTHS_IN_A_YEAR 12
#define DEBUG

int main(){
    int monthNumber, loopVariable;
    float loanAmount, totalInterestPerMonth, years, monthlyDue, totalMonths, interestRate, interest, totalAmountPerMonth, amountToPay;

    printf("\nEnter Load Amount: ");
    scanf("%f",&loanAmount);

    printf("\nEnter desired load term (in years): ");
    scanf("%f",&years);

    totalMonths = years * TOTAL_MONTHS_IN_A_YEAR;

    monthlyDue = loanAmount / totalMonths;

    printf("\nEnter the annual interest rate: ");
    scanf("%f",&interestRate);

    interest = interestRate / 100;

    totalInterestPerMonth = ( loanAmount * interest * years ) / totalMonths;

    totalAmountPerMonth = monthlyDue + totalInterestPerMonth;

    amountToPay = totalAmountPerMonth * totalMonths;

    #ifdef DEBUG
    printf("\nTotal Months: %0.2f", totalMonths);
    printf("\nYour Monthly Due: %0.2f", monthlyDue);
    printf("\nYour Interest Rate: %0.2f", interestRate);
    printf("\nYour Total Interest Per Month: %0.2f", totalInterestPerMonth);
    printf("\nTotal Amount to Pay Per Month With Interest: %0.2f", totalAmountPerMonth);
    printf("\nTotal Amount To Pay %0.2f in Years: %0.2f", amountToPay, years);
    #endif

    printf("\n\nMonth no.\tMonthly due\tEnding balance\n");
    for (loopVariable = 0 ; loopVariable < totalMonths; loopVariable++){
         printf("%d\t\t%.2f%\t\t%.2f\n",loopVariable, totalAmountPerMonth, amountToPay);
         amountToPay = amountToPay - totalAmountPerMonth;
    }

    printf("Remaining Amount to Pay in last Month: %0.2f", amountToPay + totalAmountPerMonth);
}

此处调试MACRO可用于启用/禁用调试打印。

让我知道是否可以。

答案 2 :(得分:0)

任何人看到您的代码都可以说您对编码不感兴趣。您的代码挤满了文件并被不良记录。下次使用可以理解的变量名称。我写了另一个程序。我希望这会起作用。

#include<stdio.h>

int main() 
{
    int mn;
    float loan_amount, years, time_in_months, monthly_due, p, ending_balance, interest, x, interest_rate;

    printf("Enter the loan amount:");
    scanf("%f",&loan_amount);
    printf("Enter the desired loan term(years):");
    scanf("%f", &years);

    time_in_months=years*12;

    printf("Enter the annual interest rate:");
    scanf("%f",&interest_rate);

    interest = (loan_amount*years*interest_rate)/100;

    monthly_due = (loan_amount+interest) / time_in_months;

    printf("\n\nAmount to be paid back : %d",loan_amount+interest);

    printf("Month no.\tMonthly Due\tEnding balance\n");
    int month_no;
    for(month_no = 1,ending_balance = loan_amount + interest; month_no <= time_in_months; month_no++)
    {
        printf("%d\t\t%.2f\t\t%.2f\n",month_no,monthly_due,ending_balance);
        ending_balance -= monthly_due;
    }

    getch();
    return 0;
}

答案 3 :(得分:0)

看看我能不能帮忙。

区分浮点数(floatdouble)和整数类型(此程序中的int)很重要。

付款时间表始终以离散的增量进行,因此应始终以整数表示(此处为int类型)。我认为您不希望允许分期付款。

您有float的期限,y以年为单位,m1的期限是月。 这些名称需要改进。另外,永远不要在变量顶部定义变量。根据需要定义它们。

所以,让我们从头开始。您要贷款额。

更改自:

int mn;
float la, y, m1, md, p, eb, i, x, z;
printf("Enter the loan amount:");
scanf("%f",&la);

收件人:

printf("Enter the loan amount: ");
float loan_amount = 0.0;
scanf("%f", &loan_amount);

现在我们只知道一件事。贷款额。所有其他值都可以等待。让我们一次做一个想法/一个概念。现在获取贷款期限值。

您所拥有的:

printf("Enter the desired load term(years):");
scanf("%f", &y);
m1=y*12;
md=la/m1;

很难记住

  • y是“以年计的贷款期限”
  • m1是“以月为单位的贷款期限”

更改这些名称并使其为整数:

int loan_term_in_years = 0;
printf("Enter the desired loan term (years): ");
scanf("%d", &loan_term_in_years);
int const loan_term_in_months = loan_term_in_years * 12;
float const monthly_principal = loan_amount / loan_term_in_months;
printf("Monthly Principal: %.2f\n", monthly_principal);

如果事实是您想允许小数年,请不要使用float。取而代之的是,在几个月内接受用户输入,甚至不用理会变量loan_term_in_years

我使用const表示,一旦设置,该值就不会更改。

好的,现在让我们获取利率:

printf("Enter the annual interest rate:");
scanf("%f",&z);
i=z/100;
x=(la*i*y)/m1;
p=md+x;

同样,很难记住这些含义。我建议:

printf("Enter the annual interest rate: ");
float annual_interest_rate_percent = 0.0;
scanf("%f", &annual_interest_rate_percent);
float const annual_interest_rate = annual_interest_rate_percent / 100.0;
printf("Anual interest rate: %.2f\n", annual_interest_rate);

float const monthly_interest = (loan_amount * annual_interest_rate * loan_term_in_years) / loan_term_in_months;
float const payment = monthly_principal + monthly_interest;

好的,现在循环播放。内部循环不是必需的。因此,请忽略它。正如您可能发现的那样,外循环永远存在。这是因为期末余额eb总是大于-1。您正在执行此操作(我在猜测),因为如果将您与0.0进行比较,则在某些情况下,循环会提前一个月终止。 Google提供“浮点比较”功能-但目前,您无法比较浮点是否相等并期望它们完全相等。

在循环中将浮点数用作迭代参数是不明智的做法(出于比较的原因)。我们知道有loan_term_in_months个付款要支付。让我们将其用作for循环的比较:

printf("Month no.\tMonthly due\tEnding balance\n");
float ending_balance = loan_amount;
for (int payment_month = 1; payment_month <= loan_term_in_months; payment_month++) {
    ending_balance -= monthly_principal;
    printf("%d\t\t%.2f\t\t%.2f\n", payment_month, payment, ending_balance);
}

最后,这是整个程序:

#include <stdio.h>

int main()
{
    printf("Enter the loan amount: ");
    float loan_amount = 0.0;
    scanf("%f", &loan_amount);

    int loan_term_in_years = 0;
    printf("Enter the desired loan term (years): ");
    scanf("%d", &loan_term_in_years);
    int const loan_term_in_months = loan_term_in_years * 12;
    float const monthly_principal = loan_amount / loan_term_in_months;
    printf("Monthly Principal: %.2f\n", monthly_principal);

    printf("Enter the annual interest rate: ");
    float annual_interest_rate_percent = 0.0;
    scanf("%f", &annual_interest_rate_percent);
    float const annual_interest_rate = annual_interest_rate_percent / 100.0;
    printf("Anual interest rate: %.2f\n", annual_interest_rate);

    float const monthly_interest = (loan_amount * annual_interest_rate * loan_term_in_years) / loan_term_in_months;
    float const payment = monthly_principal + monthly_interest;

    printf("Month no.\tMonthly due\tEnding balance\n");
    float ending_balance = loan_amount;
    for (int payment_month = 1; payment_month <= loan_term_in_months; payment_month++) {
        ending_balance -= monthly_principal;
        printf("%d\t\t%.2f\t\t%.2f\n", payment_month, payment, ending_balance);
    }

    return 0;
}