我的C语言例题5

#include 
//while 实现1到100累加
int main()
{
	int i = 1, sum = 0;
	while (i <= 100)
	{
		sum = sum + i;
		i++;
	}
	printf("sum=%d\n", sum);
	return 0;
}
#include 
//do while 实现1到100累加
int main()
{
	int i = 1, sum = 0;
	do
	{
		sum = sum + i;
		i++;
	} while (i <= 100);
	printf("sum=%d\n", sum);
	return 0;
}

#include 
//while 和do while 循环比较
int main()
{
	int i, sum = 0;
	printf("piease enter i,i=?");
	scanf_s("%d", &i);
	while (i <= 10)
	{
		sum = sum + i;
		i++;
	}
	printf("sum=%d\n", sum);
	return 0;
}


#include 

int main()
{
	int i, sum = 0;
	printf("piease enter i,i=?");
	scanf_s("%d", &i);
	do 
	{
		sum = sum + i;
		i++;
	} while (i <= 10);
	printf("sum=%d\n", sum);
	return 0;
}
#include 
#define SUM 100000
//break终止循环
int main()
{
	float amount, aver, total;
	int i;
	for (i = 1,total=0; i <= 1000; i++)
	{
		printf("please enter amount:");
		scanf_s("%f", &amount);
		total = amount + total;
		if (total >= SUM) break;
	}
	aver = total / i;
	printf("num=%d\naver=%10.2f\n", i, aver);
	return 0;
}
#include 
//continue用法
int main()
{
	int n;
	for (n = 100; n <= 200; n++)
	{
		if (n % 3 == 0)
			continue;
		printf("%d ", n);
	}
	printf("\n");
	return 0;
}
#include 
//4*5的矩阵
int main()
{
	int i, j, n = 0;
	for(i=1;i<=4;i++)
		for (j = 1; j <= 5; j++,n++)
		{
			if (n % 5 == 0)
				printf("\n");
			printf("%d\t", i*j);
         }
	printf("\n");
	return 0;
}
#include 
//改动
int main()
{
	int i, j, n = 0;
	for (i = 1; i <= 4; i++)
		for (j = 1; j <= 5; j++, n++)
		{
			if (n % 5 == 0)
				printf("\n");
			if (i == 3 && j == 1)
				continue;
			printf("%d\t", i*j);
		}
	printf("\n");
	return 0;
}

#include 
#include
//求π的近似值
int main()
{
	int sign = 1;
	double pi = 0.0, n = 1.0, term = 1.0;
	while (fabs(term) >= 1e-6)
	{
		pi = pi + term;
		n = n + 2;
		sign = -sign;
		term = sign / n;
	}
	pi = pi * 4;
	printf("pi=%10.8f\n", pi);
	return 0;
}
#include 
//兔子问题
int main()
{
	int f1 = 1, f2 = 1, f3;
	int i;
	printf("%12d\n%12d\n", f1, f2);
	for (i = 1; i <= 38; i++)
	{
		f3 = f1 + f2;
		printf("%12d\n", f3);
		f1 = f2;
		f2 = f3;
	}
	return 0;
}

#include 
//兔子问题改
int main()
{
	int f1 = 1, f2 = 1;
	int i;
	for (i = 1; i <= 20; i++)
	{
		printf("%12d %12d ", f1, f2);
		if (i % 2 == 0)printf("\n");
		f1 = f1 + f2;
		f2 = f2 + f1;
	}
	return 0;
}
#include 
//判断素数
int main()
{
	int n, i;
	printf("please enter a integer number,n=?");
	scanf_s("%d", &n);
	for (i = 2; i < n; i++)
		if (n%i == 0)break;
	if (i < n)
		printf("%d is not a prime number.\n", n);
	else
		printf("%d is a prime number .\n", n);
	return 0;
}
#include 
#include

int main()
{
	int n, i, k;
	printf("please enter a integer number, n = ? ");
	scanf_s("%d", &n);
	k = sqrt(n);
	for (i = 2; i <= k; i++)
		if (n%i == 0)
			break;
	if(i<k)
		printf("%d is not a prime number .\n", n);
	else
		printf("%d is a prime number .\n", n);
	return 0;
}
#include 
#include

//100--200之间素数问题
int main()
{
	int i, n, k, m = 0;
	for (n = 101; n <= 200; n = n + 2)
	{
		k = sqrt(n);
		for (i = 2; i <= k; i++)
			if (n%i == 0)break;
		if (i >= k + 1)
		{
			printf("%d ", n);
			m = m + 1;
		}
		if (m % 10 == 0)printf("\n");

	}
	printf("\n");
	return 0;
}
#include 
//译码算法
int main()
{
	char c;
	c = getchar();
	while (c != '\n')
	{
		if ((c >= 'a'&&c <= 'z') || (c >= 'A'&&c <= 'Z'))
		{
			if (c >= 'W'&&c <= 'Z' || c >= 'w'&&c <= 'z')
				c = c - 22;
			else
				c = c + 4;
		}
		printf("%c", c);
		c = getchar();
	}
	printf("\n");
	return 0;
}

#include 
//译码改进
int main()
{
	char c;
	while ((c = getchar()) != '\n')
	{
		if ((c >= 'A'&&c <= 'Z') || (c >= 'a'&&c <= 'z'))
		{
			c = c + 4;
			if (c > 'Z'&&c <= 'Z' + 4 || c > 'z')
				c = c - 26;
		}
		printf("%c", c);
	}
	printf("\n");
	return 0;
}

你可能感兴趣的:(小白,C语言第五章例题,循环)