C语言小程序们~

我是灼灼,一只初学Java的大一金渐层。
向往余秀华和狄兰·托马斯的疯狂,时常沉溺于将情感以诗相寄;追逐过王尔德、王小波的文字,后陷于毛姆和斯蒂芬·金不可自拔;热爱文学的浪潮,白日梦到底却总在现实里清醒;艳羡平静又极度渴盼奔跑的力量。
欢迎与我交流鸭· QQ:1517526827;
个人博客:https://blog.csdn.net/weixin_52777510?spm=1001.2101.3001.5343

C语言小程序汇总

复习中…

  • 计算字符的总数

//这个程序不太对劲=_=,先不要看

#include <stdio.h>
//character numbers
#include <stdlib.h>
int main()
{
    
    
	char arr[10];
	char c;
	printf("please input your characters,不超过10个:\n");
	scanf("%s",&arr[10]);
	int count;
	while((c=getchar())!='\n')
	{
    
    
		count++;
	}
	printf("the characters numbers are %d:\n",count);
	return 0;
}
  • 计算各种字符的总数
//char numbers
#include <stdio.h>
int main()
{
    
    
	char s;
	int count1,count2,count3,count4;//count1为大写字母的总数,count2为小写字母的总数,count3为空格的总数,count4为数字的总数 
	printf("please input a string:\n");
	while((s=getchar())!='\n')
	{
    
    
		if(s>='A'&&s<='Z')
		{
    
    
			count1++;
		}
		else if(s>='a'&&s<='z')
		{
    
    
			count2++;
		}
		else if(s==' ')
		{
    
    
			count3++;
		}
		else if(s>='0'&&s<='9')
		{
    
    
			count4++;
		}
		else
		{
    
    
			break;
		}
	}
	printf("大写字母:%d个	小写字母:%d个	空格:%d个	数字:%d个\n",count1,count2,count3,count4);
	return 0;
}

输出示例:

please input a string:
283 STHJJYUFjheihfuewg hdjgwd89637vefj
大写字母:8个   小写字母:21个  空格:2个       数字:8
  • 计算两数之和
//the sum of two numbers
#include <stdio.h>
int main()
{
    
    
	int a,b;
	int sum;
	printf("please input two integer numbers with divided by blank space:\n");
	scanf("%d %d",&a,&b);
	sum=a+b;
	printf("the sum of two numbers is %d\n",sum);
	return 0;
}

输出示例:

please input two integer numbers with divided by blank space:
78 65
the sum of two numbers is 143
  • 交换两个数字的值
#include <stdio.h>
//numbers swap
int main()
{
    
    
	int num1,num2;
	printf("please input two numbers you want to swap(please use the ' ' to divide):\n");
	scanf("%d %d",&num1,&num2);
	int temp;
	temp=num1;
	num1=num2;
	num2=temp;
	printf("\nswap success!\n");
	printf("the swaped numbers are:\n");
	printf("%d %d",num1,num2);
	return 0;
}

输出示例:

please input two numbers you want to swap(please use the ' ' to divide):
98 70

swap success!
the swaped numbers are:
70 98
  • 数字拆分成另一个数字:
#include <stdio.h>
int main()
{
    
    
	int temp1,a,b,c,temp2;
	printf("请输入你想要拆分的三位数:\n");
	scanf("%d",&temp1);
	a=temp1/100;	//百位数a,除以100的除数即为a
	b=(temp1-100*a)/10;	//十位数b,忽略百位数,除以10的除数即为b
	c=temp1%10;	//个位数c,三位数对10取余即为c
	temp2=c*100+b*10+a;
	printf("拆分后组成的新数:%d\n",temp2);
	return 0;
}

输出示例:

请输入你想要拆分的三位数:
754
拆分后组成的新数:457
  • 求累加和:
#include <stdio.h>
//culumutive sum 
int main()
{
    
    
	int num;
	for(int a=0;a<=100;a++)
	{
    
    
		num=num+a;
	}
	printf("累加和为:%d\n",num);
	return 0;
}

输出结果:

累加和为:5050
  • 求累乘值:

//这里注意输入的值尽量小一些吧,不然输不出来!

#include <stdio.h>
//factorials
int main()
{
    
    
	int n,i,fact=1;
	printf("please input a number you want to fact(正整数):\n");  
	scanf("%ld",&n);    
	for(i=2;i<=n;i++)   
	{
    
    
    	fact*=i;
	}
	printf("%ld!=%ld",n,fact);  //n!=fact(表现形式,!=不是不等于的意思哦~)
	return 0;
}

输出示例:

please input a number you want to fact(正整数):
29
29!=-1241513984
  • 二维数组的翻转:
//two-dimensional array inversion
#include <stdio.h>
int main()
{
    
    
	int arr1[2][3],arr2[3][2];
	printf("please input 6 numbers you want to store with divided by blank space,also can not:\n");
	for(int i=0;i<2;i++)
	{
    
    
		for(int j=0;j<3;j++)
		{
    
    
			scanf("%5d",&arr1[i][j]);
		}
	}
	printf("\nthe 6 numbers in the arr1 are as follows:\n");
	for(int i=0;i<2;i++)
	{
    
    
		for(int j=0;j<3;j++)
		{
    
    
			printf("%5d",arr1[i][j]);
		}
		printf("\n");
	}
	printf("\nthe arr1 is reversing.....\n");
	for(int i=0;i<2;i++)					//这里注意被翻转的是原数组,所以嵌套循环仍按照原数组来。 
	{
    
    
		for(int j=0;j<3;j++)
		{
    
    
			arr2[j][i]=arr1[i][j];
		}
		printf("\n");
	}
	printf("\nreverse success!\n");
	printf("the reversed array is arr2! look! \n");
	for(int i=0;i<3;i++)
	{
    
    
		for(int j=0;j<2;j++)
		{
    
    
			printf("%5d",arr2[i][j]);
		}
		printf("\n");
	}
	return 0;
}

输出示例:

please input 6 numbers you want to store with divided by blank space,also can not:
7 6 5 4 2 1

the 6 numbers in the arr1 are as follows:
    7    6    5
    4    2    1

the arr1 is reversing.....



reverse success!
the reversed array is arr2! look!
    7    4
    6    2
    5    1
  • 杨辉三角的输出
#include <stdio.h>
//yanghui
int main()
{
    
    
    int n,i,j,a[10][10];
	printf("please input the line you want to print:\n");
    scanf("%d",&n);
    printf("\n%d行杨辉三角如下:\n",n);	//三角形的行数等于列数 
    for(i=1;i<=n;i++)	//打印n行 
    {
    
    
        a[i][1] = a[i][i] = 1;	//每行第一个数和最后一个数都是1 
    }
    for(i=3;i<=n;i++)	//从第三行到第n行打印n-2行 
    {
    
    
        for(j=2;j<=i-1;j++)	//从第二列到第n-1列打印列 
        {
    
    
            a[i][j]=a[i-1][j-1]+a[i-1][j]; 	//某个数等于它左边这列上边这行的那个数加上这一列的上面这一行的那个数的和,就是它头上的数以及头上的数左边的数之和 
		} 
	}
    for(i=1;i<=n;i++)	//打印n行 
	{
    
    
        for(j=1;j<=i;j++)  //从第一列到第n列,其中第一行的1、第二行的12,第三行的123...以此类推,位置上的数字就是本数字------限制列数不得超过行数 
        {
    
    
		    printf("%6d",a[i][j]);
		}
        printf("\n");
    }
    printf("\n");
}

输出示例:

please input the line you want to print:
7

7行杨辉三角如下:
     1
     1     1
     1     2     1
     1     3     3     1
     1     4     6     4     1
     1     5    10    10     5     1
     1     6    15    20    15     6     1
  • 最大公因数和最小公倍数:
#include <stdio.h>
//最大公因数和最小公倍数
int main()
{
    
    
	int m,n,t,a,b;
	printf("请输入你想要判断的两个整数,中间用空格分隔:\n") ;
	scanf("%d %d",&m,&n);
	a=m;
	b=n;
	t=a%b;
	while(t!=0)
	{
    
    
		a=b;
		b=t;
		t=a%b;
	}
	printf("最大公约数为:%d\n",b);
	printf("最小公倍数为:%d\n",m*n/b);
	return 0;
}

输出示例:

请输入你想要判断的两个整数,中间用空格分隔:
65 90
最大公约数为:5
最小公倍数为:1170
  • 求三角形的面积:
#include <stdio.h>
//the area of triangle
#include <math.h>	//求面积调用函数sqrt开平方
int main()
{
    
    
	float a,b,c,p,area;
	printf("请输入你想要求面积的三角形的三条边长:\n三个浮点数请用空格分隔\n");
	scanf("%f %f %f",&a,&b,&c);
	if(a>0&&b>0&&c>0&&(a+b)>c&&(b+c)>a&&(a+c)>b)	//满足三角形的条件
	{
    
    
		printf("input right!\n");
		p=(a+b+c)/2;	//求三角形的半周长
		area=sqrt(p*(p-a)*(p-b)*(p-c));	//海伦公式求三角形的面积
		printf("该三角形的面积为:%f\n",area);
	}
	else
	{
    
    
		printf("input error!please reinput!\n");
	}
	return 0;
}

输出示例:

请输入你想要求面积的三角形的三条边长:
三个浮点数请用空格分隔
3.0 4.0 5.0
input right!
该三角形的面积为:6.000000
  • 选择成绩等级
#include <stdio.h>
//switch grade judgement
int main()
{
    
    
	int score;
	printf("*********************************************\n");
	printf("        欢迎使用评分等级系统:\n");
	printf("请输入一一小朋友的整数制成绩来判断他的等级哦:\n");
	printf("*********************************************\n");
	scanf("%d",&score);
		switch(score/10)
		{
    
    
			case 10:
			case 9:
				printf("一一同学的评分等级为:'A'\n");
				break;
			case 8:
			case 7:
				printf("一一同学的评分等级为:'B'\n");
				break;
			case 6:
				printf("一一同学的评分等级为:'C'\n");
				break;
			default:
				printf("一一同学的评分等级为:'D'\n");
				break;
		}
	return 0;
}

输出示例:

*********************************************
        欢迎使用评分等级系统:
请输入一一小朋友的整数制成绩来判断他的等级哦:
*********************************************
98
一一同学的评分等级为:'A'
  • 素数判断
#include <stdio.h>
//Prime Judgement
int main()
{
    
    
	int num;
	printf("请输入你想要判断的数字:\n");
	scanf("%d",&num);
	for(int i=2;i<num/2;i++)	//从2开始循环判断;循环条件可更改为num或者根号2
	{
    
    
		if(num%i==0)
		{
    
    
			printf("该数不是素数!\n");
		}
		else
		{
    
    
			printf("该数是素数!\n");
			break;
		}
		break;
	} 
	return 0;
}

输出示例:

请输入你想要判断的数字:
79
该数是素数!
  • 闰年判断:
#include <stdio.h>
//leap year judgement
int main()
{
    
    
	int year;
	printf("请输入你想要查找的年份:\n");
	scanf("%d",&year);
	if(year%4==0&&year%100!=0)	//条件为可以被4或400整除但不能被100整除的年份
	{
    
    
		printf("该年是闰年!\n"); 
	}
	else if(year%400==0)
	{
    
    
		printf("该年是闰年!\n"); 
	}
	return 0;
}

输出示例:

请输入你想要查找的年份:
2024
该年是闰年!
  • 华氏温度和摄氏温度转换
#include <stdio.h>
//C change into F
int main()
{
    
    
	int C,F; 
	printf("请输入你想要转换的摄氏温度:\n");
	scanf("%d",&C);
	F=C*9/5+32;
	printf("转换后的华氏温度为:%d",F);
	return 0;
}
#include <stdio.h>
//F转换为C
int main()
{
    
    
	int C,F;
	scanf("%d",&F);
	C=(F-32)*5/9;
	printf("C=%d\n",C);
}

待更新…

在这里插入图片描述
如果对你有帮助的话不要忘记一键三连噢~
谢谢鸭~

初次编写于2021/3/5日;

猜你喜欢

转载自blog.csdn.net/weixin_52777510/article/details/114413458
今日推荐