當前位置:首頁 » 編程語言 » c語言簡單編程if
擴展閱讀
webinf下怎麼引入js 2023-08-31 21:54:13
堡壘機怎麼打開web 2023-08-31 21:54:11

c語言簡單編程if

發布時間: 2022-07-11 04:28:28

c語言的編程,if else的語句。

scanf("%d",y);
這個改成scanf("%d",&y);
if(y%4=0)改成
if(y%4==0)
另外
判斷閏年演算法不對
還得考慮如果是100的倍數
但不是400的倍數
那麼也不是閏年
所以可以
if(y%400==0
||
(y%100!=0
&&
y
%4==0))
printf("This
year
is
leap
year.");
else
printf("This
year
is
not
a
leap
year.");

② C語言編程if語句

#include<stdio.h>
#include<math.h>
intmain()
{
floath,w,t;
printf("請輸入你的身高和體重h,w:");
scanf("%f%f",&h,&w);
t=w/(h*h);
printf("t=%f ",t);
if(t<18)
printf("你為低體重 ");
elseif(t>=18&&t<=25)
printf("你為中等身材 ");
elseif(t>25)
printf("你身體有點胖 ");
printf("%.2f,%f.2 ",h,w);
return0;
}

沒有給t值賦值,在printf("t=%f ",w/(h*h));只是列印出w/(h*h)表達式的返回值,並沒有賦值給t

③ c語言程序設計,只用if語句

#include<stdio.h>
voidmain(){
floatp,d;
scanf("%f",&p);
if(p<100)d=0;
elseif(p<200)d=5;
elseif(p<500)d=10;
elseif(p<1000)d=15;
elsed=20;
printf("折扣率:%.f%%,實付金額:%.2f",d,p-p*d/100);
}

運行示例:

④ C語言編程IF問題

改如下:

intmain(){
intx,/*a,b,c,d,e,*/f;
scanf("%d",&x);
if(x<1000||x>9999)
{
printf("Invaliddata ");
}
else
{
/*a=x/1000;//這一段演算法有誤
e=x-1000*a;
b=e/100;
e=e-100*b;
c=b/10;
e=e-10*c;
d=e;printf("%d%d%d%d ",a,b,c,d);
f=(a+b+c+d)/2;*/
f=(x/1000+x/100%10+x/10%10+x%10)%2;//將上面注銷的改成這一行
if(f==0)
{
printf("Yes ");
}
else
{
printf("No ");
}
}
return0;
}

⑤ C語言If語句的編程

#include<stdio.h>
int main(void)
{
int x,y;
printf("please input a figure:");
scanf("%d",&x);
if(x<0)
y = 0;
else if (x>=0 &&x<10)
y = x;
else if (x>=10 && x<20)
y = 10;
else if (x>=20 && x<40)
y = (-0.5)*x + 20;
else
printf("the figure which you inputed is wrong.\n");
printf("y=%d\n",y);
return 0;
}

2.
#include<stdio.h>
int main(void)
{
int x,y;
printf("please input a figure:");
scanf("%d",&x);
if(x>-5 && x<5)
y = x*x;
else
y = 3*x - 1;
printf("y = %d",y);
return 0;
}

⑥ C語言 用IF語句編程

#include<stdio.h>
main()
{
intp=0,w=0,s=0,f=0;
if(s<250)
{
f=p*w*Δs*(1-d);
printf("%d",f);
}
if(s>=250&&s<1500)
{
f=(p*w*Δs*(1-d))*0.02;
printf("%d",f);
}
if(s>=1500&&s<3000)
{
f=((p*w*Δs*(1-d))*0.02)*0.1;
printf("%d",f);
}
if(s>=3000)
{
f=(((p*w*Δs*(1-d))*0.02)*0.1)*0.15;
printf("%d",f);
}

⑦ 用C語言編程中if語句的格式是什麼

用C語言編程中if語句的格式是
if (邏輯表達式)
{
<語句>;
}
[
else
{
<語句>;
}
]
方括弧內為可選部分。邏輯表達式即為選擇的條件。
例如:將百分制整數成績轉換為及格與不及格:
if(x>=60) //滿足>=60為及格
{
printf("%d ==> 及格\n",x);
}
else //條件不滿足(<60)為不及格
{
printf("%d ==> 不及格\n",x);
}

其中的<語句>可以是C語言允許的任意可執行的語句。即它也可以是嵌套的if語句。例如:
if(x>=85) //優
{
printf("%d ==> A\n",x);
}
else //不夠優時
{
if(x>=75) //夠得上良
{
printf("%d ==> B\n",x);
}
else //夠不上良
{
if(x>=60) //夠得上中
{
printf("%d ==> C\n",x);
}
else //夠不上中
{
printf("%d ==> D\n",x);
}
}
}

⑧ c語言編程 用IF語句

c語言中的if分支語言一般有如下三種形式,當然還可以其它各種變形,無論如何變形,都以此三種形式為基礎,活學活用即可。
1、
if……形式
一般格式:if(表達式)
語句;
語義是:如果表達式的值為真,則執行其後的語句,否則不執行該語句。語句可以是單條語句,也可以是用花括弧{}包括起來的復合語句。示例如下:

#include

int main(){
int a,b,max;
printf("\n input two numbers: ");
scanf("%d%d",&a,&b);
max=a;
if (max

int main(){
int a, b;
printf("input two numbers: ");
scanf("%d%d",&a,&b);
if(a>b)
printf("max=%d\n",a);
else
printf("max=%d\n",b);
return 0;
}
3、if……else……if形式
一般形式為:
if(表達式1)
語句1;
else
if(表達式2)
語句2;
else
if(表達式3)
語句3;

else
if(表達式m)
語句m;
else
語句n;
語義是:依次判斷表達式的值,當出現某個值為真時,則執行其對應的語句。然後跳到整個if語句之外繼續執行程序。
如果所有的表達式均為假,則執行語句n。然後繼續執行後續程序。示例如下:

#include

int main(){
char c;
printf("input a character: ");
c=getchar();
if(c<32)
printf("This is a control character\n");
else if(c>='0'&&c<='9')
printf("This is a digit\n");
else if(c>='A'&&c<='Z')
printf("This is a capital letter\n");
else if(c>='a'&&c<='z')
printf("This is a small letter\n");
else
printf("This is an other character\n");
return 0;
}

⑨ C語言編程中if語句的格式是什麼

1.
if(條件語句)
{
程序執行體;

}
else
{
程序執行體;

}
2.
舉個例子:
int a;
scanf("%d",&a);
if(a > 0)
{
printf("a > 0"); //如果輸入的數大於0,就執行這個語句

}
else //就是除了a>0的情況
{
printf("a < 0"); //如果輸入的數小於0,就執行該語句

}

⑩ C語言程序設計if

解釋看注釋