實驗3


1.編寫C語言程序,計算s=22+42+62+82+102+...+1002。

  for循環

#include <stdio.h>
int main()
{
    int a;
    long int s;
    s=0;
    for(a=22;a<=1002;a+=20)
    s=s+a;
    printf("%ld\n",s);
    return 0;
}

  while循環

#include <stdio.h>
int main()
{
    int a=22;
    long s=0;
    while(a<=1002)
    {    s+=a;
        a+=20;
    }
    printf("%ld\n",s);
    return 0;
}

  do-while循環

#include <stdio.h>
int main()
{
    int a=22;
    long s=0;
    do
    {    s+=a;
        a+=20;
    }
    while(a<=1002);
    printf("%ld\n",s);
    return 0;
}

2.編寫C語言程序,輸出所有的“水仙花數”。

#include <stdio.h>
int main()
{
    int a,b,c,d,e=0;
    for(a=100;a<1000;a++)
    {
        b=a/100;
        c=(a-100*b)/10;
        d=a%10;
        if(a==b*b*b+c*c*c+d*d*d)
        {
            printf("%d\n",a);
            e++;
        }
    }
    printf("一共有%d個水仙花數\n",e);
    return 0;
}

3.有1、2、3、4、5五個數字,編寫程序,計算該五個數字能組成多少個互不相同且無重復數字的三位數。

 

#include <stdio.h>
int main()
{
    int a,b,c,d,sum=0;
    for(a=1;a<=5;a++)
    for(b=1;b<=5;b++)
    for(c=1;c<=5;c++)
    if(a!=b&&b!=c&&a!=c)
    {
        d=a*100+b*10+c;
        printf("%d\t",d);
        sum++;
    }
    printf("一共有%d個符合要求的三位數\n",sum);
    return 0;
}


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM