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

c語言循環編程練習題

發布時間: 2022-11-22 07:58:35

A. c語言循環編程題目

#include <stdio.h>
#include <stdlib.h>
int main()
{
int a,b,c,gao = 5; //定義一長串變數(gao變數的賦值是看你的圖定義的
//如果想改變高度就改變gao的值

for(a = 0;a <= gao ;++a)
{
for(b = 0;b < (gao-a);b++) //循環列印空格
{printf(" ");}

for(c = 0;c < 2*(a-1)+1;c++) //循環列印*
{printf("*");}
printf("\n"); //每行結尾帶個換行
}

system("pause");
return 0;
}

B. c語言編程題,用for循環!急急急

#include<stdio.h>
int main()
{
int hg,bhg;
for(hg=1;hg<2000;hg++)
for(bhg=0;bhg<2000;bhg++)
if(hg*5-bhg*18==9931){
printf("%d",hg);
}
return 0;
}

C. C語言循環結構程序設計題

(1)用while語句實現

#include<stdio.h>
void main()
{
int m,n;
printf("請輸入兩個正整數:");
scanf("%d%d",&m,&n);
while(m!=n)
{

if(m>n)m=m-n;
else n=n-m;
printf("m=%d n=%d\n",m,n); \\輸出m,n在運算構成中的變化

}
printf("最大公約數為:%d\n",m);
}

(2)用do-while語句實現

#include<stdio.h>
void main()
{
int m,n;
printf("請輸入兩個正整數:");
scanf("%d%d",&m,&n);
do
{
if(m>n)m=m-n;
else n=n-m;
printf("m=%d n=%d\n",m,n); \\輸出m,n在運算構成中的變化
}
while(m!=n)
printf("最大公約數為:%d\n",m);
}

(3)用for語句實現
#include<stdio.h>
void main()
{
int m,n,j;
printf("請輸入兩個正整數:");
scanf("%d%d",&m,&n);
for(i=0,i++,i<m)
{
if(m==n)
break;
else
{
if(m>n)m=m-n;
else n=n-m;
}
printf("m=%d n=%d\n",m,n); \\輸出m,n在運算構成中的變化
}
printf("最大公約數為:%d\n",m);
}

D. 我想要一套關於c語言循環語句的習題,不知哪位大俠有,希望幫幫忙,救急啊!!!先謝了

for語句格式為:
循環語句
for(表達式1;表達式2;表達式3)
循環語句語句:/*循環體*/
該語句執行過程如下:
①先對表達式1賦初值;
②判別表達式2是否滿足給定條件,若其值為真,滿足循環條件,則執行循環體內語句,然後執行表達式3,進入第二次循環,再判別表達式2……;否則判斷表達式2的值為假,不滿足條件,就終止for循環,執行循環體外語句。
while語句格式為:
while(表達式)
語句;/*循環體*/
這里的表達式是循環能否繼續下去的條件,而語句是循環體。只要表達式為真,則執行循環體內語句。否則終止循環,執行循環體外語句。
do while語句格式為:
do
語句;/*循環體*/
While(表達式);
程序進入do while循環後,先執行循環體內語句,然後判斷表達式的真假,若為真則進行
下一次循環,否則為假則終止循環。該循環語句的特點是,表達式為假時也執行一次循環體
內語句。

1.設有程序段 int k=10; while (k=0) k=k-1; 則下面描述中正確的是 (  )A while循環執行10次 B 循環是無限循環C 循環體語句一次也不執行D 循環體語句執行一次

2.設有以下程序段 int x=0,s=0; while(!x!=0)s+=++x;printf("%d",s) ;則(  )A 運行程序段後輸出0 B 運行程序段後輸出1 C 程序段中的控製表達式是非法的 D 程序段執行無限次

3、語句while(!E);中的表達式!E特等價於()A、E==0B、E!=1C、E!=0D、E==1

4、下面程序段的執行結果是( )a=1;b=2;c=2;while(a<b<c ) {t=a;a=b;b=t;c--;}printf("%d,%d,%d",a,b,c);A、1,2,0 B、2,1,0 C 、1,2,1 D、2,1,1

5、下面程序段的運行結果是(  )x=y=0;while(x<15) y++,x+=++y;printf("%d,%d",y,x);A、20,7 B、6,12 C、20,8 D 8,20

6、下面程序段的運行結果是(   )int n=0;while (n++<=2); printf("%d",n);A,2 B、3 C、4 D 有語法錯誤

7、設有程序段t=0;while(printf("*")){t++; if(t<3) break;}下面描述正確的是(   )A、其中循環控製表達式與0等價B、其中循環控製表達式與』0『等價C 、其中循環表達式是不合法的D、以上說法都不對

8、下面程序的功能是將從鍵盤輸入的一對數,由小到大排序輸出。當輸入一對相等的數時結束循環,請選擇填空。main(){int a,b,t;scanf(「%d,%d」,&a,&b);while( ){if(a>b) {t=a;a=b;b=t;}printf(「%d,%d\n」,a,b);scanf(「%d,%d」,&a,&b);}}A、!a=b B、a!=b C、a==b D、a=b

9、下面程序的功能是從鍵盤輸入的一組字元中統計出大寫字母個數m和小寫字母個數n,並輸出m,n中較大者,請選擇填空。#include<stdio.h>main( ){int m=0,n=0;char c;while((__)!=』\n』){if(c>=』A』&&c<=』Z』) m++;if(c>』a』&&c<=』z』) n++;}printf(「%d\n」,m<n?___);}A. c==getchar() B.getchar() C.c=getchar() D.scanf(「%c」,c)A. n:m B.m:n C.m:m D.n:n

10、下面程序的功能是將小寫字母變成對應大寫字母後的第二個字母。其中y變成A,Z變成B。填空#include<stdio.h>main(){char c;while((c=getchar( ))!=』\n』){if(c>=』a』&&c<=』z』){_______;if(c>=』Z』&&c<=』Z』+2)___________;}printf(「%c」,c);}A.c+=2 B.c-=32 C.c=c+32+2 D.c-=30A.c=』B』 B.c=』A』 C.c-=26 D.c=c+26

11,下面程序的功能是在輸入的一批正整數中求出最大者,輸入0時結束,請選擇填空main( ){int a,max=0; scanf(「%d」,&a);while(____){if(max<a) max=a;scanf(「%d」,&a);}printf(「%d」,max);}A.a==0 B.a C!a==1 D.!a

12、下面程序的運行結果是(  )main( ){int num=0;while(num<=2){num++; printf(「%d\n」,num);}A.1 B.1<CR>2 C.1<CR>2<CR>3 D.1<CR>2<CR>3<CR>4

13.若運行以下程序時,從鍵盤輸入2473<CR>,則下面程序的運行結果是()#include<stdio.h>main( ){int c;while((c=getchar( ))!=』\n』)switch(c-『2』){case 0:case 1:putchar(c+4);case 2:putchar(c+4);break;case 3:putchar(c+3);default:putchar(c+2);break;}printf(「\n」);}A.668977 B.668966 C.66778777 D.6688766

14.C語言中while和do-while循環的主要區別是(  )A.do-while的循環體至少無條件執行一次B.while的循環控制條件比do–while的循環控制條件嚴格C.do-while允許從外部轉到循環體內D.do-while的循環體不能是復合語句

15 .以下程序段()x=-1 do{x=x*x;}while(!x);A.是死循環B.循環執行二次C.循環執行一次D.有語法錯誤

16、以下描述中正確的是( )A.由於do -while循環中循環語句中只能是一條可執行語句,所以循環體內不能使用復合語句。B.do-while循環由do開始,且while結束,在while(表達式)後面不能寫分號。C.在do-while循環體中,一定要有能使while後面表達式的值變為零(「假」)的操作。D.do-while循環中,根據情況可以省略while

17.若有以下語句int x=3;do{print("%d\n",x-=2);} while(!(--x));則上面程序段()A.輸入的是1 B.輸出的是1和-2 C.輸出的是3和0 D.死循環

18.下面程序是從鍵盤輸入學號,然後輸出學號中百位數字是3的學號,輸入0時結束循環,填空。main(){long int num;scanf("%ld",&num);do{if(_____) printf("%ld,num);scanf("%ld",&num);}while(______);}A.num%100/10==3 .Bnum/100%10==3 C.num%10/10==3 D.num/10%10==3A.!num B.num>0==0 C.!num==0 D.!num!=0

19.下面程序的運行結果是( )#include<stdio.h>
main( )
{int y=10;
do {y--;}while(--y);
printf(「%d\n」,--y);}
A .-1 B.1 C.8 D.-8

20.若運行以下程序時,從鍵盤輸入ADescriptot<CR>(<CR>表示回車),下面程序的運行結果是
#include<stdio.h>
main( )
{char c;
int v0=0,v1=0,v2=0;
do{
switch(c=getchar( ))
{case 』a』:case 』A』:
case 』e』:case』E』:
case 』i』:case 』I』:
case 』o』:case 』O』:
case 』u』:case 』U』:v1+=1;
default:v0+=1;v2+=1;}
}while(c!=』\n』);
printf(「v0=%d,v1=%d,v2=%d\n」,v0,v1,v2);}
A)v0=7,v1=4,v2=7 B)v0=8,v1=4,v2=8
C)v0=11,v1=4,v2=11 D)v0=12,v1=4,v2=12

21.下面程序的運行結果是
#include<stdio.h>
main( )
{int a=1,b=10;
do
{b-=a;a++;}while(b--<0);
printf(「a=%d,b=%d\n」,a,b);}
a=3,b=11 B)a=2,b=8 C)a=1,b=-1 D)a=4,b=9

22.下面有關for循環的正確描述是( )A.for循環只能用於循環次數已經確定的情況B.for 循環是先執行循環體語句,後判斷表達式C.在for循環中,不能用break語句跳出循環體D.for循環的循環體語句中,可以包含多條語句,但必須花括弧括起來

23.對for(表達式1;; 表達式3)可理解為( )A.for(表達式1;0; 表達式3)B.for(表達式1;1;表達式3)C.for(表達式1;表達式1;表達式3)D.for(表達式1;表達式3;表達式3)

24.若i為整型變數,則以下循環執行的次數是( )for(i=2;i=0;) printf(「%d」,i--)A.無限次 B.0次 C.1次 D.2次

25.以下for循環的語句是( )for(x=0,y=0;(y=123)&&(x<4);x++);A.是無限循環 B.循環次數不定C.4次 D.3次

26.以下不是無限循環的語句是( )A.for(y=0,x=1;x>++y;x=I++) I=x;B.for(; ;x++=I);C.while(1) {x++;}D.for(I=10; ;I--) sum+=I;

27.以下程序段的運行結果是( )for (y=1;y<10;) y=((x=3*y,x+1),x-1);printf(「x=%d,y=%d」,x,y);A. x=27,y=27 B.x=12,y=13 C.x=15,y=14 D.x=y=27

28. 下面程序段的運行結果是____
for(x=3;x<6;x++)printf((x%2)?(「**%d」):(「##%d\n」),x);
a)**3 b)##3 c)##3 d)**3##4
##4 **4 **4##5 **5
**5 ##5

29.下列程序段不是死循環的是(   )A. int I=100 while(1){I=I%100+1;if(I>100)break;}B. for(;;)C. int k=0;do{++k;}while(k>=0);D.int s=36; while(s); --s;30.執行語句for(I=1;I++<4;);後變數I的值是()A.3; B.4 C.5 D.不定

E. 簡單C語言循環語句編程題目!

發一下本人的拙見!(半夜做的也不知道對不!)
int peach(int n)//該函數返回桃子總數,n為天數
{
if( 10==n )
return 1;
else
return ( peach(n+1)+ 1) * 2 ;
}

int main(void)
{
printf("第%d天的桃子數為: %d\n", 1, peach( 1 ) );

int total=1;//桃子總數
for(int i=10; i>1; i--)
{
total = (total+1)*2;
}
printf("桃子的總數為: %d\n", total);

return 0;
}

F. c語言循環編程題

#include<stdio.h>
#include<stdlib.h>
int main()
{
int lower,upper;
scanf("%d %d",&lower,&upper);
printf("fahr celsius\n");
for(int i=lower;i<=upper;i++)
{printf(" %d ",i);
if((1.0*(i-32)*5/9)>=0)
printf(" %.1f\n",1.0*(i-32)*5/9);
else
printf("%.1f\n",1.0*(i-32)*5/9);
}
//system("pause");
}
注意公式:(華氏溫度-32)*5/9=攝氏溫度
已經按格式給你處理了答案

G. C語言循環程序大題求解答!!!

題目不錯, 但是樓主首先要清楚一個概念, 就是C語言中基本數據類型保存的數據精度是相當有限的,其中整數是很容易溢出的(在你的題目中) , 所以只能用double求個大概值出來.
代碼如下(剛寫的,求樓主加分!!)

#include <stdio.h>
#include <string.h>
#include <windows.h>
//1、編程求分數序列2/1, 3/2, 5/3, 8/5, 13/8...的前50項的和。要求保留盡可能高的運算精度。
double solve1 () {
double a = 1.0 , b = 2.0 , res = 0.0 ;
for(int i = 0 ; i < 50 ; i++) {
res += b / a ;
double c = a + b ;
a = b ;
b = c ;
}
return res ;
}
int main() {
printf("%lf\n" , solve1()) ;//
system("pause") ;
return 0 ;
}

#include <stdio.h>
#include <string.h>
#include <windows.h>
//2、編程按下列的級數求COS(X)的值並輸出:
//COS(X)=1-X2/2!+X4/4!-X6/6!+......
//要求:x的值由鍵盤輸入(代表弧度),運算精度保持到最後一項的絕對值小於10的負6次為止。
double solve2 (double x) {
static const double eps = 1e-6 ;
double res = 0.0 ;
double s = 1.0 , a = 1.0 , b = 1.0 , i = 1.0 , y = s * a / b ;
while(y >= eps || y <= -eps) {
res += y ;
s = -s ;
a = a * x * x ;
b = b * i * (i + 1) ;
i = i + 2 ;
y = s * a / b ;
}
return res ;
}

int main() {
double x ;
while(scanf("%lf" , &x) == 1) {
printf("%lf\n" , solve2(x)) ;
}
system("pause") ;
return 0 ;
}

其它兩個已經寫完了,樓主加分就給哦 :)

H. C語言循環程序大題

第一題,寫了一點代碼,但是結果很離譜,誤差挺大的,僅供參考
#include <stdio.h>
#include <math.h>

double _cos(double x);

int main(int argc, char *argv[])
{
float x;
printf("plesae input x: \n");
scanf("%g", &x);
printf("cos(%g) = %g\n", x, _cos(x));
}

int factorial(int n)
{
if(n <= 1)
return 1;
n *= factorial(n - 1);
return n;
}

double _pow(double x, int n)
{
int i;
double tmp, result;
tmp = x;
result = 1.0;
for(i = 0; i < n; i++)
{
result *= x;
if(fabs(result- tmp) < 1e-10)
break;
tmp = result;
}
return result;
}

double _cos(double x)
{
int i;
double result, tmp, forhead;
i = 1;
result = 1.0;
forhead = 0.0;
while(fabs(forhead - result) > 1e-6)
{
forhead = result;
tmp = _pow(x, 2*i) / (double) factorial(2*i);
if(i % 2 == 0)
result += tmp;
else
result -= tmp;
if(tmp < 1e-10)
break;
i++;
}
return result;
}

I. C語言編程題(有關二維數組的循環的)

#include <stdio.h>
main()
{
int a[10][5]; /*a表示10個學生的5門成績*/
int i,j;
int sum,avg;
char b[5][10]={"課程1","課程2","課程3","課程4","課程5"};
for(i=0;i<10;i++){
printf("\n請輸入第%d個學生的成績 (%s)\n",i+1,b);
for(j=0;j<5;j++){
scanf("%d",&a[i][j]);
}
}
for(i=0;i<10;i++){
printf("第%d個學生的總分是:%d\n",i+1,zong(i,a));
}
for(i=0;i<5;i++){
printf("%s的平均分是%d\n",b[i],pingjun(a,i));
}
for(i=0;i<5;i++){
printf("%s課的最高分是%d,學生是第%d個\n",b[i],a[check(a,i)][i],check(a,i));
}
getch();
}
int zong(int index,int x[10][5]){ /*計算index個學生的總分*/
int sum=0,j;
for(j=0;j<5;j++){
sum+=x[index][j];
}
return sum;
}
int pingjun(int x[10][5],int index){ /*計算第index門課程的平均分*/
int sum=0,i;
for(i=0;i<10;i++){
sum+=x[i][index];
}
return sum/10;
}
int check(int x[10][5],int index){ /*找其第index門課的最高分*/
int max,i,count;
max=x[0][index];
count=0;
for(i=0;i<10;i++){
if(max<=x[i][index]){
max=x[i][index];
count=i;
}
}
return count;
}
我有1年沒有學C了,現在學的是java和jsp,所以還有一些印象,不知道對不對,你的第3問我是找的每門課程的最高分,不知道你問的是不是這樣,要是問的所有分中的最高分,你比較5門就可以了
恩,我用win-tc寫的,如果用vc,直接刪除getch()就可以了

J. c語言編程題,用循環語句

#include<stdio.h>
int main(void)
{
int i,n;
double s=1;
printf("Please input n:");
scanf("%d",&n);
for(i=1;i<=n;i++)
s=s+1.0/(i*(i+1));
printf("s=%.2f ",s);
return 0;
}