c語言程序代碼_第1頁
c語言程序代碼_第2頁
c語言程序代碼_第3頁
c語言程序代碼_第4頁
c語言程序代碼_第5頁
已閱讀5頁,還剩18頁未讀 繼續(xù)免費閱讀

下載本文檔

版權說明:本文檔由用戶提供并上傳,收益歸屬內(nèi)容提供方,若內(nèi)容存在侵權,請進行舉報或認領

文檔簡介

1、1. 要求在屏幕上輸出下一行信息。This is a c program.程序:#include<stdio.h>int main()printf(“this is a c program.n”);return 0;2. 求兩個整數(shù)之和。程序:#include<stdio.h>int main()int a,b,sum;a=122;b=234;sum=a+b;printf(“sum is %dn”,sum);return 0;3. 求兩個整數(shù)之間的較大者。程序:#include<stdio.h>int main()int max(int x,int y);i

2、nt a,b,c;scanf("%d,%d",&a,&b);c=max(a,b);printf("max=%dn",c);return 0;int max(int x,int y)int z;if(x>y)z=x;else z=y;return(z);4. 有人用溫度計測量出華氏發(fā)表示的溫度(如69°F),今要求把她轉換成以攝氏法表示的溫度(如20)。公式:c=5(f-32)/9.其中f代表華氏溫度,c代表攝氏溫度。程序:#include<stdio.h>int main()float f,c;f=64.0;c

3、=(5.0/9)*(f-32);printf("f=%fnc=%fn",f,c);return 0;5. 計算存款利息。有1000元,想存一年。有一下三種方法可選:(1)活期:年利率為r1;(2)一年定期:年利率為r2;(3)存兩次半年定期:年利率為r3。分別計算一年后按三種方法所得到的本息和。程序:#include<stdio.h>int main()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);pri

4、ntf("p1=%fnp2=%fnp3=%fn",p1,p2,p3);return 0;6. 給定一個大寫字母,要求以小寫字母輸出。程序:#include<stdio.h>int main()char c1,c2;c1=A;c2=c1+32;printf(“%cn”,c2);printf(“%dn”,c2);return 0;7. 給出三角形的三邊長,求三角形的面積。公式:若給定三角形的三邊長,且任意兩邊之長大于第三邊。則:area=ss-as-b(s-c)其中s=(a+b+C)/2.程序:#include<stdio.h>#include<m

5、ath.h>int main()double a,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=%ftb=%ftc=%fn”,a,b,c);printf(“area=%fn”,area);return 0;8. 求ax2+bx+c=0方程的根。a,b,c由鍵盤輸入,設b2-4ac>0.程序:#include<stdio.h>#include<math.h>int main()double a,b,c,disc,x1,x2,p,q;scanf(

6、“%lf%lf%lf”,&a,&b,&c);disc=b*b-4*a*c;if(disc<0)printf(“This question has no real rootsn”);elsep=-b/(2.0*a);q=sqrt(disc)/(2.0*a);x1=p+q;x2=p-q;printf(“x1=%7.2fnx2=%7.2fn”,x1,x2);return 0;9. 先后輸出BOY三個字符。程序:#include<stdio.h>int main()char a=B,b=O,c=Y;putchar(a);putchar(b);putchar(c

7、);putchar(n);return 0;10. 用三個getchar函數(shù)先后向計算機輸入BOY三個字符,然后用putchar函數(shù)輸出。程序:#include<stdio.h>int main()char a,b,c;a=getchar();b=getchar();c=getchar();putchar(a);putchar(b);putchar(c);putchar(n);return 0;或#include<stdio.h>int main()putchar(getchar();putchar(getchar();putchar(getchar();putchar

8、(n);return 0;11. 用getchar函數(shù)從鍵盤讀入一個大寫字母,把它轉換成小寫字母,然后用getchar函數(shù)輸出對應的小寫字母。程序:#include<stdio.h>int main()char c1,c2;c1=getchar();c2=c1+32;putchar(c2);putchar(n);return 0;12. 輸入兩個實數(shù),按代數(shù)值由小到大的順序輸出這兩個數(shù)。(參照將兩個杯子中的水互換,必須借助第三個杯子)。程序:#include<stdio.h>int main()float a,b,t;scanf(“%f,%f”,&a,&

9、b);if(a>b)t=a;a=b;b=t;printf(“%5.2f,%5.2fn”,a,b);return 0;13. 輸入a,b,c三個數(shù),要求由小到大的順序輸出。程序:#include<stdio.h>int main()float a,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.2fn",a,b,c);r

10、eturn 0;14輸入一個字符,判斷它是否為大寫字母,如果是,將它轉換成小寫字母,如果不是,不轉換。然后輸出最后得到的字符。程序:#include<stdio.h>int main()char ch;scanf(“%c”,&ch);ch=(ch>=A&&ch<=Z)?(ch+32):ch;printf(“%cn”,ch);return 0;或#include<stdio.h>int main()char ch;scanf("%c",&ch);if(ch>='A'&&c

11、h<='Z')printf("%cn",ch+32);elseprintf("%cn",ch);return 0;15.有一個函數(shù):y=-1 (x<0)0 (x=0) 1 x>0 編一程序。輸入一個x的值,要求輸出相應的y值。程序:#include<stdio.h>int main()int x,y;scanf("%d",&x);if(x<0)y=-1;elseif(x=0)y=0;elsey=1;printf("x=%dny=%dn",x,y);retu

12、rn 0;16.要求按照考試成績的等級輸出百分制分數(shù)段,A等為85分以上,B等為70-84分。C等為60-69分,D等為60分以下。成績的等級由鍵盤輸入。程序:#include<stdio.h>int main()char grade;scanf("%c",grade);printf("You score:n");switch(grade)case'A':printf("85100n");break;case'B':printf("7084n");break;case&#

13、39;C':printf("6069n");break;case'D':printf("<60n");break;default:printf("enter date errorn");return 0;17.寫一程序,判斷某一年是否為閏年。程序:#include<stdio.h>int main()int leap,year;printf("please enter year:");scanf("%d",&year);if(year%4=0)i

14、f(year%100=0)if(year%400=0)leap=1;elseleap=0;else leap=1;else leap=0;if(leap)printf("%d is a leap yearn",year);elseprintf("%d is not a leap yearn",year);return 0;或#include<stdio.h>int main()int leap,year;printf("please enter year:");scanf("%d",&year)

15、;if(year%4!=0)leap=0;else if(year%100!=0)leap=1;else if(year%400!=0)leap=0;elseleap=1;if(leap=1)printf("%d is a leap yearn",year);elseprintf("%d is not a leap yearn",year);return 0;或#include<stdio.h>int main()int leap,year;printf("please enter year:");scanf("

16、%d",&year);if(year%4=0&&year%100!=0)|(year%400=0)leap=1;elseleap=0;if(leap=1)printf("%d is a leap yearn",year);elseprintf("%d is not a leap yearn",year);return 0;18. 求ax2+bx+c=0方程的根。a,b,c由鍵盤輸入。(完整版)程序:#include<stdio.h>#include<math.h>int main()double

17、a,b,c,disc,x1,x2,x3,realpart,imagepart;scanf("%lf,%lf,%lf",&a,&b,&c);printf("The equation");if(fabs(a)<=1e-6)printf("is not a quadratic");elsedisc=b*b-4*a*c;if(fabs(disc)<=1e-6)printf("has two equal roots %8.4fn",-b/2*a);elseif(disc>1e-6)p

18、rintf("has two distinct real rootsn%8.4f,%8.4f",x1,x2);x1=(-b+sqrt(disc)/2*a;x2=(-b-sqrt(disc)/2*a;elserealpart=-b/2*a;imagepart=sqrt(-disc)/2*a;printf("has two complex roots:n");printf("%8.4f+%8.4fin",realpart,imagepart);printf("%8.4f-%8.4fin",realpart,imagep

19、art);return 0;/注釋:由于b*b-4*a*c(disc)是實數(shù),而實數(shù)在計算和存儲時會有一些微小的誤差,因此不能直接進行如下判斷:/“if(disc=0)",因為這樣可能出現(xiàn)本來是零的量,由于上述誤差而判別為不等于零而導致結果錯誤,/所以采取的辦法是判別disc的絕對值(fabs(disc)是否小于一個很小的數(shù)(例如:1e-6)。如果小于此數(shù),則認為disc=0.19.給出一個不多出5位的正整數(shù),要求:(1):求出它是幾位數(shù);(2):分別輸出每一位數(shù)字;(3):按逆序輸出各位數(shù)字,例如原數(shù)為321,輸出123。程序:#include<stdio.h>int

20、main()int num,indiv,ten,hundred,thousand,ten_thousand,place;printf("請輸入一個正整數(shù)(099999):");scanf("%d",&num);if(num>9999)place=5;else if(num>999)place=4;else if(num>99)place=3;else if(num>9)place=2;elseplace=1;printf("位數(shù)為:%dn",place);ten_thousand=num/10000;t

21、housand=(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("每一個數(shù)字分別為:");printf("%d,%d,%d,%d,%dn",ten_thousand,thousand,hundred,ten,indiv);switch(place)case 5:printf(&quo

溫馨提示

  • 1. 本站所有資源如無特殊說明,都需要本地電腦安裝OFFICE2007和PDF閱讀器。圖紙軟件為CAD,CAXA,PROE,UG,SolidWorks等.壓縮文件請下載最新的WinRAR軟件解壓。
  • 2. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請聯(lián)系上傳者。文件的所有權益歸上傳用戶所有。
  • 3. 本站RAR壓縮包中若帶圖紙,網(wǎng)頁內(nèi)容里面會有圖紙預覽,若沒有圖紙預覽就沒有圖紙。
  • 4. 未經(jīng)權益所有人同意不得將文件中的內(nèi)容挪作商業(yè)或盈利用途。
  • 5. 人人文庫網(wǎng)僅提供信息存儲空間,僅對用戶上傳內(nèi)容的表現(xiàn)方式做保護處理,對用戶上傳分享的文檔內(nèi)容本身不做任何修改或編輯,并不能對任何下載內(nèi)容負責。
  • 6. 下載文件中如有侵權或不適當內(nèi)容,請與我們聯(lián)系,我們立即糾正。
  • 7. 本站不保證下載資源的準確性、安全性和完整性, 同時也不承擔用戶因使用這些下載資源對自己和他人造成任何形式的傷害或損失。

最新文檔

評論

0/150

提交評論