c语言程序代码.docx

上传人:b****7 文档编号:25008772 上传时间:2023-06-03 格式:DOCX 页数:18 大小:20.26KB
下载 相关 举报
c语言程序代码.docx_第1页
第1页 / 共18页
c语言程序代码.docx_第2页
第2页 / 共18页
c语言程序代码.docx_第3页
第3页 / 共18页
c语言程序代码.docx_第4页
第4页 / 共18页
c语言程序代码.docx_第5页
第5页 / 共18页
点击查看更多>>
下载资源
资源描述

c语言程序代码.docx

《c语言程序代码.docx》由会员分享,可在线阅读,更多相关《c语言程序代码.docx(18页珍藏版)》请在冰豆网上搜索。

c语言程序代码.docx

c语言程序代码

1.要求在屏幕上输出下一行信息。

Thisisacprogram.

程序:

#include

intmain()

{

printf(“thisisacprogram.\n”);

return0;

}

2.求两个整数之和。

程序:

#include

intmain()

{

inta,b,sum;

a=122;

b=234;

sum=a+b;

printf(“sumis%d\n”,sum);

return0;

}

3.求两个整数之间的较大者。

程序:

#include

intmain()

{

intmax(intx,inty);

inta,b,c;

scanf("%d,%d",&a,&b);

c=max(a,b);

printf("max=%d\n",c);

return0;

}

intmax(intx,inty)

{

intz;

if(x>y)z=x;

elsez=y;

return(z);

}

4.有人用温度计测量出华氏发表示的温度(如69°F),今要求把

她转换成以摄氏法表示的温度(如20℃)。

公式:

c=5(f-32)/9.

其中f代表华氏温度,c代表摄氏温度。

程序:

#include

intmain()

{

floatf,c;

f=64.0;

c=(5.0/9)*(f-32);

printf("f=%f\nc=%f\n",f,c);

return0;

}

5.计算存款利息。

有1000元,想存一年。

有一下三种方法可选:

(1)活期:

年利率为r1;

(2)一年定期:

年利率为r2;(3)存两次半年定期:

年利率为r3。

分别计算一年后按三种方法所得到的本息和。

程序:

#include

intmain()

{

float

p0=1000,r1=0.0036,r2=0.0225,r3=0.0198,p1,p2,p3;

p1=p0*(1+r1);

p2=p0*(1+r2);

p3=p0*(1+r3/2)*(1+r3/2);

printf("p1=%f\np2=%f\np3=%f\n",p1,p2,p3);

return0;

}

6.给定一个大写字母,要求以小写字母输出。

程序:

#include

intmain()

{

charc1,c2;

c1='A';

c2=c1+32;

printf(“%c\n”,c2);

printf(“%d\n”,c2);

return0;

}

7.给出三角形的三边长,求三角形的面积。

公式:

若给定三角形的三边长,且任意两边之长大于第三边。

则:

area=√s(s-a)(s-b)(s-c)

其中s=(a+b+C)/2.

程序:

#include

#include

intmain()

{

doublea,b,c,area;

a=3.67;

b=5.43;

c=6.21;

s=(a+b+c)/2;

area=sqrt(s*(s-a)*(s-b)*(s-c));

printf(“a=%f\tb=%f\tc=%f\n”,a,b,c);

printf(“area=%f\n”,area);

return0;

}

8.求ax2+bx+c=0方程的根。

a,b,c由键盘输入,设b2-4ac>0.

程序:

#include

#include

intmain()

{

doublea,b,c,disc,x1,x2,p,q;

scanf(“%lf%lf%lf”,&a,&b,&c);

disc=b*b-4*a*c;

if(disc<0)

printf(“Thisquestionhasnorealroots\n”);

else

{

p=-b/(2.0*a);

q=sqrt(disc)/(2.0*a);

x1=p+q;

x2=p-q;

printf(“x1=%7.2f\nx2=%7.2f\n”,x1,x2);

}

return0;

}

9.先后输出BOY三个字符。

程序:

#include

intmain()

{

chara='B',b='O',c='Y';

putchar(a);

putchar(b);

putchar(c);

putchar(‘\n');

return0;

}

10.用三个getchar函数先后向计算机输入BOY三个字符,然后用putchar函数输出。

程序:

#include

intmain()

{

chara,b,c;

a=getchar();

b=getchar();

c=getchar();

putchar(a);

putchar(b);

putchar(c);

putchar(‘\n');

return0;

}

#include

intmain()

{

putchar(getchar());

putchar(getchar());

putchar(getchar());

putchar(‘\n');

return0;

}

11.用getchar函数从键盘读入一个大写字母,把它转换成小写字母,然后用getchar函数输出对应的小写字母。

程序:

#include

intmain()

{

charc1,c2;

c1=getchar();

c2=c1+32;

putchar(c2);

putchar(‘\n');

return0;

}

12.输入两个实数,按代数值由小到大的顺序输出这两个数(参

照将两个杯子中的水互换,必须借助第三个杯子)。

程序:

#include

intmain()

{

floata,b,t;

scanf(“%f,%f”,&a,&b);

if(a>b)

{

t=a;

a=b;

b=t;

}

printf(“%5.2f,%5.2f\n”,a,b);

return0;

}

13.输入a,b,c三个数,要求由小到大的顺序输出。

程序:

#include

intmain()

{

floata,b,c,t;

scanf("%f,%f,%f",&a,&b,&c);

if(a>b);

{

t=a;

a=b;

b=t;

}if(a>c)

{

t=a;

a=c;

c=t;

}

if(b>c)

{

t=b;

b=c;

c=t;

}printf("%5.2f,%5.2f,%5.2f\n",a,b,c);

return0;

}

14.输入一个字符,判断它是否为大写字母,如果是,将它转换成小写字母,如果不是,不转换。

然后输出最后得到的字符。

程序:

#include

intmain()

{

charch;

scanf(“%c”,&ch);

ch=(ch>='A'&&ch<='Z')?

(ch+32):

ch;

printf(“%c\n”,ch);

return0;

}

#include

intmain()

{

charch;

scanf("%c",&ch);

if(ch>='A'&&ch<='Z')

printf("%c\n",ch+32);

else

printf("%c\n",ch);

return0;

-1(x<0)

15.有一个函数:

y={0(x=0)

1(x>0)

编一程序。

输入一个x的值,要求输出相应的y值。

程序:

#include

intmain()

{

intx,y;

scanf("%d",&x);

if(x<0)

y=-1;

else

if(x==0)

y=0;

else

y=1;

printf("x=%d\ny=%d\n",x,y);return0;

}

16.要求按照考试成绩的等级输出百分制分数段,A等为85分以上,B等为70-84分。

C等为60-69分,D等为60分以下。

成绩的等级由键盘输入。

程序:

#include

intmain()

{

chargrade;

scanf("%c",grade);

printf("Youscore:

\n");

switch(grade)

{

case'A':

printf("85~100\n");break;

case'B':

printf("70~84\n");break;

case'C':

printf("60~69\n");break;

case'D':

printf("<60\n");break;default:

printf("enterdateerror\n");

}

return0;

}

17.写一程序,判断某一年是否为闰年。

程序:

#include

intmain()

{

intleap,year;

printf("pleaseenteryear:

");

scanf("%d",&year);

if(year%4==0)

{

if

(year%100==0)

{

if(year%400==0)

leap=1;

else

leap=0;

}

else

leap=1;

}

elseleap=0;

if(leap)

printf("%disaleapyear\n",year);

else

printf("%disnotaleapyear\n",year);return0;

}

#include

intmain()

{

intleap,year;printf("pleaseenteryear:

");scanf("%d",&year);

if(year%4!

=0)

leap=0;

elseif(year%100!

=0)

leap=1;

elseif(year%400!

=0)

leap=0;

else

leap=1;

if(leap==1)

printf("%disaleapyear\n",year);else

printf("%disnotaleapyear\n",year);return0;

}

#include

intmain()

{

intleap,year;

printf("pleaseenteryear:

");scanf("%d",&year);

if((year%4==0&&year%100!

=0)||(year%400==0))leap=1;

else

leap=0;

if(leap==1)

printf("%disaleapyear\n",year);else

printf("%disnotaleapyear\n",year);return0;

}

18.求ax2+bx+c=0方程的根。

a,b,c由键盘输入(完整版)程序:

#include

#include

intmain()

{

doublea,b,c,disc,x1,x2,x3,realpart,imagepart;

scanf("%lf,%lf,%lf",&a,&b,&c);

printf("Theequation");

if(fabs(a)<=1e-6)

printf("isnotaquadratic");

else

{

disc=b*b-4*a*c;

if(fabs(disc)<=1e-6)

printf("hastwoequal

roots%8.4f\n",-b/2*a);

else

if(disc>1e-6)

{

printf("hastwodistinctreal

roots\n%8.4f,%8.4f",x1,x2);

x1=(-b+sqrt(disc))/2*a;

x2=(-b-sqrt(disc))/2*a;

else

{

realpart=-b/2*a;

imagepart=sqrt(-disc)/2*a;

printf("hastwocomplexroots:

\n");

printf("%8.4f+%8.4fi\n",realpart,imagepart);

printf("%8.4f-%8.4fi\n",realpart,imagepart);

}

}

return0;

}

//注释:

由于b*b-4*a*c(disc)是实数,而实数在计算和

存储时会有一些微小的误差,因此不能直接进行如下判断:

//“if(disc==0)",因为这样可能出现本来是零的量,由

于上述误差而判别为不等于零而导致结果错误,

//所以采取的办法是判别disc的绝对值(fabs(disc))

是否小于一个很小的数(例如:

1e-6)。

如果小于此数,则认为

disc=0.

19.给出一个不多出5位的正整数,要求:

(1):

求出它是几位数;

(2):

分别输出每一位数字;

(3):

按逆序输出各位数字,例如原数为321,输出123。

程序:

#include

intmain()

{

int

num,indiv,ten,hundred,thousand,ten_thousand,place;printf("请输入一个正整数(0~99999):

");scanf("%d",&num);

if(num>9999)

place=5;

elseif(num>999)

place=4;

elseif(num>99)

place=3;

elseif(num>9)

place=2;

else

place=1;

printf("位数为:

%d\n",place);

ten_thousand=num/10000;thousand=(num-ten_thousand*10000)/1000;

hundred=(num-ten_thousand*10000-thousand*1000)/100;

ten=(num-ten_thousand*10000-thousand*1000-hundred*100)/10;

indiv=(num-ten_thousand*10000-thousand*1000-hundred*100-ten*10);

printf("每一个数字分别为:

");printf("%d,%d,%d,%d,%d\n",ten_thousand,thousand,hundred,ten,indiv);

switch(place)

{

case5:

printf("反序数字为:

%d%d%d%d%d\n",indiv,ten,hundred,thousand,ten_thousand);break;

case4:

printf("反序数字为:

%d%d%d%d\n",indiv,ten,hundred,thousand);break;

为:

%d%d%d\n",indiv,ten,hundred);break;

case2:

printf("反序数字为:

%d%d\n",indiv,ten);break;

case1:

printf("反序数字为:

%d\n",indiv);break;

}

return0;

}

20.求1+2+3+4+5+……+100。

程序:

#include

intmain()

{

inti=1,sum=0;

while(i<=100)

{

sum=sum+i;

i++;

}

printf("sum=%d\n",sum);

return0;

#include

intmain()

{

inti=1,sum=0;

do

{

sum=sum+i;

i++;

}

while(i<=100);

printf("sum=%d\n",sum);

return0;

}

21.输出1-100,100个数。

程序:

#include

intmain()

{

inti=1;

do

{

printf("%d\n",i++);}while(i<=100);return0;

}

22.

展开阅读全文
相关资源
猜你喜欢
相关搜索

当前位置:首页 > 幼儿教育 > 幼儿读物

copyright@ 2008-2022 冰豆网网站版权所有

经营许可证编号:鄂ICP备2022015515号-1