简单输出"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;
}