My Carelessness

Back to your page!


Or leave your comments here.

RegendLa

导航

关于for,while与do while

Q:输入一个整数i,输出i+(i+1)+...+19+20的结果

S:法1:for

#include<stdio.h>
#include<math.h>
#include<stdlib.h>
int main()
{
	int i;
	int j;
	int s=0;//sum
	scanf("%d",&i);
	for(j=i;j<=20;j++)
	{
		s=s+j;	
	}
	printf("%d\n",s);
	return 0;
}

 法2:while

#include<stdio.h>
#include<math.h>
#include<stdlib.h>
int main()
{
	int i;
	int j;
	int s=0;//sum
	scanf("%d",&i);
	j=i;
	while(j<=20)
	{
		s=s+j;
		j++;
	}
	printf("%d\n",s);
	return 0;
}

 法3:do while

#include<stdio.h>
#include<math.h>
#include<stdlib.h>
int main()
{
	int i;
	int j;
	int s=0;//sum
	scanf("%d",&i);
	j=i;
	if(i<=20)
	do{
		s=s+j;
		j++;
	}while(j<=20);
	printf("%d\n",s);
	return 0;
}

 That's all.

posted on 2014-12-01 17:03  最爱七  阅读(155)  评论(0编辑  收藏  举报




Thanks for your coming!
If what you read helps,I would appreciate!