C语言编程练习(一)

C语言编程练习(一)

1、计算n的阶乘;

void getFactorial(int n)
{	//计算n阶乘 
	int index = 0;
	int res = 1;
	if(n != 0)
	{
		for(index=1; index<=n; index++)
		{
			res = res * index;
		}
	}
	printf("%d的阶乘为:%d\n", n, res);
}

2、求1-100之间的奇数之和、偶数之和;

void  getSumif()
{	//计算100以内奇数之和偶数之和 
	int num = 100;
	int odd = 0;
	int even = 0;
	int index = 1;
	for(index=1; index<=100; index++)
	{
		if(index%2 == 0)
		{
			even = even + index;
		}
		else
		{
			odd = odd + index;
		}
	 } 
	
	printf("奇数之和为:%d, 偶数之和为:%d\n", odd, even);
}

3、输入3个整数,然后从小到大排序;

void sorting()
{	//三个数排序 
	int arr[3] = {};
	int i = 0;
	int j = 0;
	int tmp = 0;
	printf("请输入三个整数:"); 
	for(i=0; i<3; i++)
	{
		scanf("%d", &arr[i]);
	}
	for(i=0; i<3; i++)
	{
		for(j=i+1; j<3; j++)
		{
			if(arr[i]>arr[j])
			{
				tmp = arr[j];
				arr[j] = arr[i];
				arr[i] = tmp;
			}
		}
	}
	for(i=0; i<3; i++)
	{
		printf("%d ", arr[i]);	
	} 
	printf("\n");
 } 

4、输入数字,输出星期几;

void getDay()
{	//输入1-7,输出格式为星期几 
	printf("请输入1-7中的一个数:");
	int day;
	scanf("%d", &day);
	switch(day){
		case 1:
			printf("星期一\n");
			break;
		case 2:
			printf("星期二\n");
			break;
		case 3:
			printf("星期三\n");
			break;
		case 4:
			printf("星期四\n");
			break;
		case 5:
			printf("星期五\n");
			break;
		case 6:
			printf("星期六\n");
			break;
		case 7:
			printf("星期日\n");
			break;
		default:
		 	printf("输入错误\n"); 
	} 
}

5、输出100-200之间不能被3整除的数;

void getNumif()
{	//100-200中不能被3整除的数 
	int i = 0;
	for(i=100; i<=200; i++)
	{
		if(i%3 !=0)
			printf("%d ", i);
		if(i%10==0 && i!=100)
			printf("\n");
	}	
}

6、输入某年某月某日,判断这一天是这一年的第几天。

void getWhichDay()
{	//计算天数  
	int year;
	int month;
	int day;
	int whichDay = 0;
	int i = 0;
	int leapYear[13] = {0, 31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
	int notLeapYear[13] = {0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
	printf("请以“2020 2 20”这样的格式输入年月日:");
	scanf("%d %d %d", &year, &month, &day);
	if((year%4==0 && year%100!=0) || (year%400==0))
	{
		for(i=0; i<month; i++)
		{
			whichDay = whichDay + leapYear[i];
		}
		printf("这是当年的第%d天\n", whichDay+day);
	}
	else
	{
		for(i=0; i<month; i++)
		{
			whichDay = whichDay + notLeapYear[i];
		}
		printf("这是当年的第%d天\n", whichDay+day);
	}
	
 } 

你可能感兴趣的:(编程练习栈,C/C++)