循环累积计算问题。C程序设计

循环累积计算问题。C程序设计,c,C,在这里,我创建了一个复利计算器。用户输入本金、利息百分比和期限(以季度为单位)。我在初始计算中使用了for循环。但是,我不知道如何将总额转为下一季度的本金 假设用户输入1000、5%和2个季度。产出应该是这样的,第一季度本金=1000美元,利息=0.05,总额=1012.50美元,第二季度=1012.50美元=0.05美元=1025.16美元 我最后一次做的是给我一些问题。在让用户重新开始之前,ouput会多输出几行 如有任何建议,将不胜感激 多谢各位 #include <stdio.h&

在这里,我创建了一个复利计算器。用户输入本金、利息百分比和期限(以季度为单位)。我在初始计算中使用了for循环。但是,我不知道如何将总额转为下一季度的本金

假设用户输入1000、5%和2个季度。产出应该是这样的,第一季度本金=1000美元,利息=0.05,总额=1012.50美元,第二季度=1012.50美元=0.05美元=1025.16美元

我最后一次做的是给我一些问题。在让用户重新开始之前,ouput会多输出几行

如有任何建议,将不胜感激

多谢各位

#include <stdio.h>

int main (void)
{
    int a = 0, b=0;
    double interest, prin, total=0;
    char check = ' ';

    do{

        do{

            do{
                printf (" Please enter principal:\n");
                scanf ("%lf", &prin);
            }while(prin <=0);

            do{
                printf ("Please enter desired interest greater 
                        than 0 less than 20 :\n");
                scanf ("%lf", &interest);
            }while(interest <=0 || interest >20);
            interest = interest/100;

            do{
                printf ("For how many quarters would you like 
                        to deposit: (more than 0, less than 40) \n");
                scanf ("%d", &b);
            }while(b <=0 || b >40);

            printf ("Is this information correct? Press X 
                    to continue" );
            scanf ("\n%c", &check);
        }while(check != 'x' && check != 'X');

        total = prin * (1+(interest *.25));

        printf ("Quarter     Principal      Interest      
                Total\n");

        for(a=1;   ;++a){
            printf ("%2d          $%.2f        %.2lf        
                    $%.2lf\n", a, prin, interest, total);
            if(a == b)
                break;
        }

        printf ("Do you want to start over (Y/N)?");
        scanf ("%c\n", &check);
    }while(check != 'y' || check != 'Y');





    return 0;
}
#包括
内部主(空)
{
int a=0,b=0;
双倍利息,本金,合计=0;
字符检查=“”;
做{
做{
做{
printf(“请输入主体:\n”);
扫描频率(“%lf”、&prin);

}而(prin代码中的缩进和逻辑有一些问题。您需要在for循环语句中更新原则,然后打印出来。这是我的解决方案

#include <stdio.h>

int main(void)
{
    int a = 0, b = 0;
    double interest, prin, total = 0;
    char check = ' ';

    do {

        do {

            do {
                printf(" Please enter principal:\n");
                scanf("%lf", &prin);
            } while (prin <= 0);

            do {
                printf("Please enter desired interest greater  than 0 less than 20 :\n");
                scanf("%lf", &interest);
            } while (interest <= 0 || interest > 20);
            interest = interest / 100;

            do {
                printf("For how many quarters would you like to deposit : (more than 0, less than 40) \n");
                scanf("%d", &b);
            } while (b <= 0 || b > 40);

            printf("Is this information correct? Press X to continue" );
            scanf("\n%c", &check);
        } while (check != 'x' && check != 'X');



        printf("Quarter     Principal      Interest      Total\n");

        for (a = 1; a<=b; ++a) {
            total = prin * (1 + (interest *.25));
            printf("%2d          $%.2f        %.2lf        $%.2lf\n", a, prin, interest, total);
            prin = total;
        }

        printf("Do you want to start over (Y/N)?");
        scanf("%c\n", &check);
    } while (check != 'y' || check != 'Y');
return 0;
}
#包括
内部主(空)
{
int a=0,b=0;
双倍利息,本金,合计=0;
字符检查=“”;
做{
做{
做{
printf(“请输入主体:\n”);
扫描频率(“%lf”、&prin);

}而(prin代码中的缩进和逻辑有一些问题。您需要在for循环语句中更新原则,然后打印出来。这是我的解决方案

#include <stdio.h>

int main(void)
{
    int a = 0, b = 0;
    double interest, prin, total = 0;
    char check = ' ';

    do {

        do {

            do {
                printf(" Please enter principal:\n");
                scanf("%lf", &prin);
            } while (prin <= 0);

            do {
                printf("Please enter desired interest greater  than 0 less than 20 :\n");
                scanf("%lf", &interest);
            } while (interest <= 0 || interest > 20);
            interest = interest / 100;

            do {
                printf("For how many quarters would you like to deposit : (more than 0, less than 40) \n");
                scanf("%d", &b);
            } while (b <= 0 || b > 40);

            printf("Is this information correct? Press X to continue" );
            scanf("\n%c", &check);
        } while (check != 'x' && check != 'X');



        printf("Quarter     Principal      Interest      Total\n");

        for (a = 1; a<=b; ++a) {
            total = prin * (1 + (interest *.25));
            printf("%2d          $%.2f        %.2lf        $%.2lf\n", a, prin, interest, total);
            prin = total;
        }

        printf("Do you want to start over (Y/N)?");
        scanf("%c\n", &check);
    } while (check != 'y' || check != 'Y');
return 0;
}
#包括
内部主(空)
{
int a=0,b=0;
双倍利息,本金,合计=0;
字符检查=“”;
做{
做{
做{
printf(“请输入主体:\n”);
扫描频率(“%lf”、&prin);

}当(prin你有几个层次的循环嵌套。阅读这样一个没有适当缩进的代码是罪人在地狱里会做的事情。这可能是一个愚蠢的问题,但是我能找到一个资源来详细说明适当缩进吗?如果你使用任何稍微体面的IDE,它可以自动为你做。虽然有不同的风格,但是您可能想要采用的方法。只需四处寻找“缩进C代码”…现在为您做了(使用gVim)。除此之外:
scanf(\n%C“,&check);
会更好,因为
scanf(%C“,&check)
这是一个空格,而不是换行符,再往下一点,前面的空格
%c
也不见了。风向标。谢谢,这似乎解决了我上次遇到的do-while循环问题。你有几个层次的循环嵌套。阅读这样的代码而没有适当的缩进是罪人在地狱里会做的事情。这是米格这可能是一个愚蠢的问题,但我能找到一个资源来详细说明正确的缩进吗?如果你使用任何一个稍微合适的IDE,它可以自动为你做。虽然你可能会采用不同的样式。只要看看“缩进C代码”…现在为你做了(使用gVim)。旁白:
scanf(\n%C“,&check)
最好是
scanf(“%c”和&check);
这是一个空格,而不是换行符,再往下一点,前面的空格
%c
丢失了。风向标。谢谢,这似乎解决了我上次遇到的循环问题。Loc Trim,好的,所以我给“Prin”指定了“total”的值在for循环中,并将我的兴趣分配也移动到for循环中。我已识别出逻辑错误。感谢您的帮助。OT:这不好:
scanf(“\n%c”,&check);
这不好:
scanf(“\n%c”,&check);
建议:
scanf(“%c”,&check);
scanf(“%c”,&check)
另外,在调用任何
scanf()函数族时,始终检查返回值(而不是参数值)以确保操作成功。类似于:`if(scanf(“%c”),&check)!=1){//handle error and exit}Loc Trim,Ok,因此我已为“Prin”指定了“total”的值在for循环中,并将我的兴趣分配也移动到for循环中。我已识别出逻辑错误。感谢您的帮助。OT:这不好:
scanf(“\n%c”,&check);
这不好:
scanf(“\n%c”,&check);
建议:
scanf(“%c”,&check);
scanf(“%c”,&check)
此外,在调用任何
scanf()函数族时,始终检查返回值(而不是参数值)以确保操作成功..类似于:`if(scanf(“%c”,&check)!=1){//handle error and exit}