【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;
}
相关推荐
leiming62 分钟前
CAN 通信协议学习讲义(带图文 + C 语言代码)
c语言·开发语言·学习
xht083236 分钟前
PHP vs C语言:核心差异全解析
c语言·开发语言·php
你真是饿了1 小时前
算法专题二:滑动窗口
算法
ccLianLian1 小时前
数论·约数
数据结构·算法
会编程的土豆1 小时前
【数据结构与算法】最短路径---Dijkstra 算法
数据结构·c++·算法
2401_879693871 小时前
C++中的观察者模式实战
开发语言·c++·算法
炽烈小老头1 小时前
【 每天学习一点算法 2026/03/24】寻找峰值
学习·算法
fff9811182 小时前
C++与Qt图形开发
开发语言·c++·算法
计算机安禾2 小时前
【数据结构与算法】第3篇:C语言核心机制回顾(二):动态内存管理与typedef
c语言·开发语言·数据结构·c++·算法·链表·visual studio
njidf3 小时前
C++中的访问者模式
开发语言·c++·算法