c for的問題

#include <stdio.h>

int main(void)

{

double month=1;

float c=0.02/12,monthly=100000;

printf(" month Deposit(monthly)\n");

for(c,month,monthly ; month<=121 ; month++,monthly=monthly*c+monthly)

printf("%15.0f %15.0f \n",month,monthly);

getchar();

return 0;

}

我想讓這個程式只printf出以下這幾行,其他的不要,請問要怎麼做

month=13,Deposit(monthly)=102018

month=25,Deposit(monthly)=104078

month=37,Deposit(monthly)=106178

month=49,Deposit(monthly)=108321

month=61,Deposit(monthly)=110508

month=73,Deposit(monthly)=112738

month=85,Deposit(monthly)=115014

month=97,Deposit(monthly)=117335

month=109,Deposit(monthly)=119704

month=121,Deposit(monthly)=122120

3 Answers

Rating
  • 2 decades ago
    Favorite Answer

    #include <stdio.h>

    int main(void)

    {

      int print_j = 13;

    double month=1;

      float c=0.02/12,monthly=100000;

      printf(" month Deposit(monthly)\n");

      for(c,month,monthly ; month<=121 ; month++,monthly=monthly*c+monthly)

    {  

       if (month==print_j)

    {

    printf("%15.0f %15.0f \n",month,monthly);

    print_j+=12;

    }

    }

      getchar();

      return 0;

    }

    Source(s): 自己
  • 2 decades ago

    to雯琪

    但是這樣Deposit(monthly)的值不對...

  • 2 decades ago

    可以看到printf的month值從13開始每格12就列印下一行。

    使用for迴圈=> for(起始值,終值,遞增值)

    因為一開始已經給予month初值,所以起始值位子便可不變,但遞增值位子必須改為+12,因為每列印一行下一行month的值就會增加12。

    所以程式為:

    #include <stdio.h>

    int main(void)

    {

      double month=13;

      float c=0.02/12,monthly=100000;

      printf(" month Deposit(monthly)\n");

      for(c,month,monthly:month<=121;month=month+12,monthly=monthly*c+monthly)

      

      printf("%15.0f %15.0f \n",month,monthly);

      getchar();

      return 0;

    }

    Source(s): me
Still have questions? Get your answers by asking now.