第6次作业

随机打个数判断大小

#include<stdio.h>

#include <stdlib.h>

#include <time.h>

int main() {

while(1){

int a=0,b;

srand((unsigned)time(NULL));

a = rand()%100+1;

printf("请输入一个数");

scanf("%d",&b);

if(a>b){

printf("小了");

}

if(a<b){

printf("大了");

}

printf("\n");

}

}

 

 

(1)编写一个程序,求之值

#include <stdio.h>

main()

{

int a=0,b;

double sum=0;

for(b=1;b<=100;b++)

{

if(b%2==0)

{

a=b*(-1);

}

else

{

a=b;

}

sum=sum+1.0/a;

}

printf("%f",sum);

}

 

 

 

(2)编写一个程序,求s= s=1+(1+2)+(1+2+3)++(1+2+3++n)的值

#include <stdio.h>

main()

{

int s=0,n,c,b=0;

printf("请输入一个整数");

scanf("%d",&n);

for(c=1;c<=n;c++)

{

b+=c;

s=s+b;

}

printf("%d",s);

}

 

 

3)编写一个程序,用户输入一个正整数,把它的各位数字前后颠倒一下,并输出颠倒后的结果。

#include <stdio.h>

main()

{

int n,m,s=0;

printf("请输入一个正整数");

scanf("%d",&n);

while(n>0)

{

m=n%10;

s=s*10+m;

n/=10;

}

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

}

 

posted @ 2021-12-02 09:23  Berial127  阅读(34)  评论(0)    收藏  举报