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

下載本文檔

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

文檔簡介

-.z.要求在屏幕上輸出下一行信息。Thisisacprogram.程序:#include<stdio.h>intmain(){printf("thisisacprogram.\n〞);return0;}求兩個整數(shù)之和。程序:#include<stdio.h>intmain(){inta,b,sum;a=122;b=234;sum=a+b;printf("sumis%d\n〞,sum);return0;}求兩個整數(shù)之間的較大者。程序:#include<stdio.h>intmain(){intma*(int*,inty);inta,b,c;scanf("%d,%d",&a,&b);c=ma*(a,b);printf("ma*=%d\n",c);return0;}intma*(int*,inty){intz;if(*>y)z=*;elsez=y;return(z);}有人用溫度計測量出華氏發(fā)表示的溫度〔如69°F〕,今要求把她轉(zhuǎn)換成以攝氏法表示的溫度〔如20℃〕。公式:c=5〔f-32〕/9.其中f代表華氏溫度,c代表攝氏溫度。程序:#include<stdio.h>intmain(){floatf,c;f=64.0;c=(5.0/9)*(f-32);printf("f=%f\nc=%f\n",f,c);return0;}計算存款利息。有1000元,想存一年。有一下三種方法可選:〔1〕活期:年利率為r1;〔2〕一年定期:年利率為r2;〔3〕存兩次半年定期:年利率為r3。分別計算一年后按三種方法所得到的本息和。程序:#include<stdio.h>intmain(){floatp0=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;}給定一個大寫字母,要求以小寫字母輸出。程序:#include<stdio.h>intmain(){charc1,c2;c1=’A’;c2=c1+32;printf("%c\n〞,c2);printf("%d\n〞,c2);return0;}給出三角形的三邊長,求三角形的面積。公式:假設(shè)給定三角形的三邊長,且任意兩邊之長大于第三邊。則:area=QUOTE其中s=(a+b+C)/2.程序:#include<stdio.h>#include<math.h>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;}求a*2+b*+c=0方程的根。a,b,c由鍵盤輸入,設(shè)b2-4ac>0.程序:#include<stdio.h>#include<math.h>intmain(){doublea,b,c,disc,*1,*2,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);*1=p+q;*2=p-q;printf("*1=%7.2f\n*2=%7.2f\n〞,*1,*2);}return0;}先后輸出BOY三個字符。程序:#include<stdio.h>intmain(){chara=’B’,b=’O’,c=’Y’;putchar(a);putchar(b);putchar(c);putchar(‘\n’);return0;}用三個getchar函數(shù)先后向計算機輸入BOY三個字符,然后用putchar函數(shù)輸出。程序:#include<stdio.h>intmain(){chara,b,c;a=getchar();b=getchar();c=getchar();putchar(a);putchar(b);putchar(c);putchar(‘\n’);return0;}或#include<stdio.h>intmain(){putchar(getchar());putchar(getchar());putchar(getchar());putchar(‘\n’);return0;}用getchar函數(shù)從鍵盤讀入一個大寫字母,把它轉(zhuǎn)換成小寫字母,然后用getchar函數(shù)輸出對應(yīng)的小寫字母。程序:#include<stdio.h>intmain(){charc1,c2;c1=getchar();c2=c1+32;putchar(c2);putchar(‘\n’);return0;}輸入兩個實數(shù),按代數(shù)值由小到大的順序輸出這兩個數(shù)?!矃⒄諏蓚€杯子中的水互換,必須借助第三個杯子〕。程序:#include<stdio.h>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;}輸入a,b,c三個數(shù),要求由小到大的順序輸出。程序:#include<stdio.h>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.輸入一個字符,判斷它是否為大寫字母,如果是,將它轉(zhuǎn)換成小寫字母,如果不是,不轉(zhuǎn)換。然后輸出最后得到的字符。程序:#include<stdio.h>intmain(){charch;scanf("%c〞,&ch);ch=(ch>=’A’&&ch<=’Z’)"(ch+32):ch;printf("%c\n〞,ch);return0;}或#include<stdio.h>intmain(){ charch; scanf("%c",&ch); if(ch>='A'&&ch<='Z') printf("%c\n",ch+32); else printf("%c\n",ch); return0;}15.有一個函數(shù):y=QUOTE編一程序。輸入一個*的值,要求輸出相應(yīng)的y值。程序:#include<stdio.h>intmain(){ int*,y; scanf("%d",&*); if(*<0) y=-1; elseif(*==0) y=0; else y=1; printf("*=%d\ny=%d\n",*,y); return0;}16.要求按照考試成績的等級輸出百分制分數(shù)段,A等為85分以上,B等為70-84分。C等為60-69分,D等為60分以下。成績的等級由鍵盤輸入。程序:#include<stdio.h>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<stdio.h>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<stdio.h>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<stdio.h>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.求a*2+b*+c=0方程的根。a,b,c由鍵盤輸入?!餐暾妗吵绦颍?include<stdio.h>#include<math.h>intmain(){ doublea,b,c,disc,*1,*2,*3,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("hastwoequalroots%8.4f\n",-b/2*a); else if(disc>1e-6) { printf("hastwodistinctrealroots\n%8.4f,%8.4f",*1,*2); *1=(-b+sqrt(disc))/2*a; *2=(-b-sqrt(disc))/2*a; } else { realpart=-b/2*a; imagepart=sqrt(-disc)/2*a; printf("hastwople*roots:\n"); printf("%8.4f+%8.4fi\n",realpart,imagepart); printf("%8.4f-%8.4fi\n",realpart,imagepart); } } return0;}//注釋:由于b*b-4*a*c〔disc〕是實數(shù),而實數(shù)在計算和存儲時會有一些微小的誤差,因此不能直接進展如下判斷://"if〔disc==0〕",因為這樣可能出現(xiàn)本來是零的量,由于上述誤差而判別為不等于零而導致結(jié)果錯誤,//所以采取的方法是判別disc的絕對值〔fabs〔disc〕〕是否小于一個很小的數(shù)〔例如:1e-6〕。如果小于此數(shù),則認為disc=0.19.給出一個不多出5位的正整數(shù),要求:〔1〕:求出它是幾位數(shù);〔2〕:分別輸出每一位數(shù)字;〔3〕:按逆序輸出各位數(shù)字,例如原數(shù)為321,輸出123。程序:#include<stdio.h>intmain(){ intnum,indiv,ten,hundred,thousand,ten_thousand,place; printf("請輸入一個正整數(shù)(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("位數(shù)為:%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("每一個數(shù)字分別為:"); printf("%d,%d,%d,%d,%d\n",ten_thousand,thousand,hundred,ten,indiv); switch(place) { case5:printf("反

溫馨提示

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

評論

0/150

提交評論