One piece function to calculate water fee
#include <stdio.h>
int main()
{
/**
* 定义两个
* 定义浮点型变量
* y:水费
* x:用水的吨数
*
*/
double x, y;
printf("Enter x(x>=0):\n");
scanf_s("%lf",&x);
if (x <= 15) {
y = 4 * x / 3;
}
else
{
y = 2.5 * x - 10.5;
}
printf("y=f(%f)=%.2f\n", x, y);
return 0;
}
two summation
Input n, calculate the sum of 1 to n
#include <stdio.h>
int main() {
int i,n,num;
printf("Enter n:");
scanf("%d",&n);
num=0;
for (i=1;i<=n;i++){
num=num+i;
}
printf("The sum of number from 1 to %d is %d",n,num);
}
three guessing game
#include <stdio.h>
int main() {
int i,n,num;
printf("Enter n:");
scanf("%d",&n);
int password=38;
if(n==password){
printf("success");
}else{
printf("error");
}
return 0;
}