-
简单输出“helloword”
#include<stdio.h>
int main(){
printf("hello world!");
return 0;
}
-
和与商
#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;
}
-
判断奇偶
#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;
}
-
判断闰年
#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);
}
}
-
求阶乘
#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;
}
本站资源均来自互联网,仅供研究学习,禁止违法使用和商用,产生法律纠纷本站概不负责!如果侵犯了您的权益请与我们联系!
转载请注明出处: 免费源码网-免费的源码资源网站 » 【C语言】基础篇
发表评论 取消回复