天天看點

水仙花數c語言程式_C語言練習題

水仙花數c語言程式_C語言練習題

你點選原文,就分你小魚幹

計算機語言的學習基礎是C語言。掌握好C語言就尤為重要。

1

 題目:

      計算所有三位水仙花數之和。所謂水仙花數是指所有位上數字的立方和等于該數,如:153= 1+5+3

輸出結果的形式為:

s=1234

參考程式:

#include

void PRINT(long s)

{

      FILE *out;

      printf("s=%ld\n,s);

      if( (out=fopen("result. dat”,' w+”)) !=NULL)

      {

       fprintf (out, "S=s%ld",s):

       fclose (out) ;

       }

}

      void main()

  long s=0:

  int  i,a,b,c:

  for(i1oo: iN1000i+)

 {

    a=i%10;

    b=i/10%10;

    c=i/100;

    if (a*a*atb*b*b+cжc*c=i) s+=i:

  }

    PRINT(s);

}

2

題目:

根據下式計算s的值(要求使用循環實作):

水仙花數c語言程式_C語言練習題

說明:結果保留4位小數。

輸出結果的形式為:

s=12.4567

參考程式:

#include

void PRINT (double s)

{

  FILE *out;

  printf(" s=%.4fn",s):

  if ((out-fopen(" result. dat","w+")) !=NULL)

     fprintf(out," s=%. 4fg",s);

  fclose (out) ;

}

  void main()

{

     double s;

     int i;

     s=l;

     for(i=2;i<=100;i=i+2)

          s=s*1.0*i/(i-1)*i/(i+1) ;

     PRINT(s) :

}

3

題目:

      計算1~ 2018之間所有3或5的倍數的和(要求使用循環實作)。

輸出結果的形式為:

s=123456

#include

void PRINT (1ong s)

{

    FILE *out;

    printf(" s=%ld\n",s);

    if((out=fopen(" result. dat", "w+")) !=NULL)

    {

      fprintf(out,'”s=%ldg' ,s) ;

      fclose (out) ;

     }

}

      void main()

{

   long s=0;

   int i;

   for(i=1;i<2018;i++)

        if(i%3==0 || 1%5==0)

            s=s+i;

  PRINT(s);

}

水仙花數c語言程式_C語言練習題

希望阿程發的這些題目能夠幫助到大家!

水仙花數c語言程式_C語言練習題

投稿郵箱:[email protected]. com

編輯|排版|佘義偉

審校|楊有浩

責任編輯|張元瑩  梁星雨

宿州學院資訊工程學院學生會出品

如需轉載請詢問背景

水仙花數c語言程式_C語言練習題