其他分享
首页 > 其他分享> > 第六次随笔

第六次随笔

作者:互联网

1编写程序,使用while语句求和sum=1+3+5+…+21

#include<stdio.h>

main()

{

         int i=1,sum=0;

         while(i<=21)

         {

                  sum+=i;

                  i+=2;

         }

         printf("%d\n",sum);

}

 

 

2.编写程序,使用while语句求和sum=1+1/3+1/5+..+1/n

#include<stdio.h>

main()

{

         int i=1,n;

         double sum=0;

         scanf("%d",&n);

         while(i<=n)

         {

                  sum+=1.0/i;

                  i+=2;

         }

         printf("sum=%f\n",sum);

}

 

 

3编写程序,使用do-while语句求阶乘f=1*2*3*..*n

#include<stdio.h>

main()

{

         int i=1,n,sum=1;

         scanf("%d",&n);

         while(i<=n)

         {

                  sum*=i;

                  i++;

         }

         printf("%d\n",sum);

}

 

 

4.打印出所有的水仙花数

#include<stdio.h>

main()

{

         int i=100,ge,shi,bai;

         while(i<=999){

                  ge=i%10;

                  shi=i/10%10;

                  bai=i/100;

                  if(i==ge*ge*ge+shi*shi*shi+bai*bai*bai)

                          printf("%4d",i);

                  i++;

         }

}

 

 

5.输入两个数,输出他们中间的数

#include<stdio.h>

main()

{

         int a,b,i;

         scanf("%d%d",&a,&b);

         i=a+1;

         while(i<=b-1){

                  printf("%d",i); 

         i++;

         }

}

 

 

6.输出1-100既能被3又能被5整除的数的和

#include<stdio.h>

main()

{

         int n,i=0,sum=0;

         scanf("i%3==o&&i%5==o",i);

         while(i<=100){

                  sum+=i;

                  printf("%d",i); 

         i++;

         }

}

 

标签:main,int,sum,include,while,printf,第六次,随笔
来源: https://www.cnblogs.com/qwertyuiop1/p/15527190.html