【C语言】基础篇

简单输出"helloword"

cpp 复制代码
#include<stdio.h>
int main(){
	printf("hello world!");
	return 0;
}

和与商

cpp 复制代码
#include<stdio.h>
int main(){
	int a,b,sum,quotient;
	printf("Enter two numbers:");
	scanf("%d %d",&a,&b);
	sum = a + b;
	quotient = a / b;
	printf("%d + %d = %d\n",a,b,sum);
	if(b != 0){
	printf("%d / %d = %d",a,b,quotient);
	}
	else{
		printf("请检查你输入的数字是否正确。");
	}
	return 0;
}

判断奇偶

cpp 复制代码
#include<stdio.h>
int main(){
	int number;
	printf("Enter a number:");
	scanf("%d",&number);
	if(number%2 == 0){
		printf("%d is even",number);
	}
	else{
		printf("%d is odd",number);
	}
	return 0;
}

判断闰年

cpp 复制代码
#include<stdio.h>
int main(){
	int year;
	printf("Enter a year:");
	scanf("%d",&year);
	if(year % 4 == 0 & year % 100 != 0 | year % 400 == 0){
		printf("%d is a leap year",year);
	}
	else{
		printf("%d is not a leap year",year);
	}
}

求阶乘

cpp 复制代码
#include<stdio.h>
int main(){
	int n,i;
	int t = 1;
	printf("Enter a number:");
	scanf("%d",&n);
	for(i = 1;i <= n;i++){
		t = t * i;
	}
	printf("factorial = %d",t);
	return 0;
}
相关推荐
kyle~10 小时前
算法---贪心算法(Greedy Algorithm)
算法·贪心算法
fashion 道格10 小时前
C 语言数组拼接:从基础实现到细节优化
算法
头发还没掉光光10 小时前
Linux多线程之自旋锁与读写锁
linux·运维·算法
fashion 道格10 小时前
C 语言希尔排序:原理、实现与性能深度解析
数据结构·算法·排序算法
无限进步_10 小时前
C语言atoi函数实现详解:从基础到优化
c语言·开发语言·c++·git·后端·github·visual studio
初夏睡觉10 小时前
P1048 [NOIP 2005 普及组] 采药
数据结构·c++·算法
小欣加油10 小时前
leetcode 1513 仅含1的子串数
c++·算法·leetcode·职场和发展
树在风中摇曳11 小时前
【C语言预处理器全解析】宏、条件编译、字符串化、拼接
c语言·算法
CodeWizard~11 小时前
P7149 [USACO20DEC] Rectangular Pasture S题解
算法
fashion 道格11 小时前
用 C 语言破解汉诺塔难题:递归思想的实战演练
c语言·算法