猿问

该如何用for,while,do-while 三种语句进行编程?

求k=x+(x+1)+(x+2)+(x+3)+……+(x+y)的值,其中x,y的值由键盘输入
谢谢阿!!

大话西游666
浏览 156回答 2
2回答

RISEBY

So easy!- -@C语言->[for]main(){int a,x,y,s=0;scanf("%d%d",&x,&y);for(a=0;a<=y;a++)s+=x+a;printf("sum=%d",s);}->[while]main(){int a=0,x,y,s=0;scanf("%d%d",&x,&y);while(a<=y)s+=x+a++;printf("sum=%d",s);}->[do-while]main(){int a=0,x,y,s=0;scanf("%d%d",&x,&y);do{s+=x+a;}while(++a<=y);printf("sum=%d",s);}可以在scanf后面加个if(y<0)算式非法的判断.初学不写没关系.上面3个我在TC2.0上调过了.没有问题.如果是C++的话,随便一改就可以啦.

红糖糍粑

#include "stdio.h"main(){int x,y,i,j;long k;pirntf("input tow numbers: \n");scanf("%d,%d",&x,&y);for(i=1;i<=y;i++)j+=x+i;k=x+j;printf("%d\n",k);getchar();return 0;}
随时随地看视频慕课网APP
我要回答