当前位置:网站首页>C language related programming exercises
C language related programming exercises
2022-07-28 14:48:00 【Youth is short!】
- Program to achieve per 5 All data can be output in a row 3 and 5 to be divisible by , But ten people are not 0 Of 3 An integer .
#include<stdio.h> void main( ) { int i,j=0; for (i=100;i<=1000;i++) if((i%3==0&&i%5==0)&&((i%100)/10!=0&&(i%100)/10!=3)) { printf("%5d",i); j++; if(j%5==0) printf("\n"); } } |
2. Programming from the keyboard to enter a binary form of string , Convert it to decimal integer output .
#include <stdio.h> #include <math.h> #include <string.h> int main() { char s[21]; long a, i, len, k; scanf("%s",s); len = strlen(s); a = 0; k = 1; for(i=len-1; i>=0; i--) { if(s[i] == '1') a += pow(2.0, len-1-i); } printf("%ld\n",a); return 0; } |
3. Give a 100 point mark , Output grade is required “A”、“B” 、“C” 、“D”、“E”.90 The above points are “A”, 80~89 It is divided into “B”,70~79 It is divided into “C”,60~69 It is divided into “D”,60 It is divided into the following “E”.
#include<stdio.h> void main() { float score; printf("please input a number:\n"); scanf("%f",&score); if(score>=90) printf("A\n"); else if(score>=80) printf("B\n"); else if(score>=70) printf("C\n"); else if(score>=60) printf("D\n"); else if(score<60) printf("E\n"); } |
4. seek 1! +2! +3! ....+50!( This programming result is wrong because bytes cannot hold that large number , But there is nothing wrong with the program )
#include<stdio.h> void main() { int i,j; long int t=1,sum=0; for (i=1;i<=50;i++) { t=1; for(j=1;j<=i;j++) t=t*j; sum=sum+t; } printf("1!+2!+3!...+50!=%d",sum); } |
5 Using bubbling method 10 An integer sort .
#include<stdio.h> void main( ) { int i,j,t; int a[10]={8,9,0,7,6,5,4,3,2,1}; for(i=0;i<9;i++) for(j=0;j<9-i;j++) { if(a[j]>a[j+1]) { t=a[j];a[j]=a[j+1];a[j+1]=t; } } for(i=0;i<10;i++) printf("%d",a[i]); } |
6. seek 500 Prime number within .
#include <stdio.h> int main() { int a,i; for (a=2;a<=500;a++) { for (i=2;i<=a-1;i++) { if (a%i==0) { break; } } if(i>=a-1) printf("%4d",a); } } |
7. Programming technology 5 A row of data is output by 1、3、5、7 A three digit number consisting of different and non repeating numbers .
#include <stdio.h> void main() { int a[4]={1,3,5,7}; int i,j,k,n=0; for(i=0;i<4;i++) { for(j=0;j<4;j++) { for(k=0;k<4;k++) { if(i!=j&&j!=k&&k!=i) // The three numbers are not equal to each other { printf("%3d%d%d",a[i],a[j],a[k]); n++; if(n%5==0) printf("\n"); } } } } } |
Add :1+3+5+……+99=?
#include<stdio.h> void main() { int i=1,sum=0; while(i<100) { sum=sum+i; i=i+2; } printf(“%d”,sum); } |
seek 1×2×3×……×10=?
#include<stdio.h> void main() { int i,k=1; for(i=1;i<=10;i++) k=k*i; printf("k=%d",k); } |
8. Count all daffodils ( The so-called daffodils number , It's such a three digit number : This number is equal to the sum of the cubes of its bits . Such as 153=13+53+33 )
#include<stdio.h> void main() { int i,gw,sw,bw; for(i=100;i<1000;i++) { gw=i%10; sw=(i%100)/10; bw=i/100; if(gw*gw*gw+sw*sw*sw+bw*bw*bw==i) printf("%4d",i); } } |
9.c Language seeking 500 Prime number within , And 10 One line output
#include <stdio.h> int main() { int a,i,m=0; for (a=2;a<=500;a++) { for (i=2;i<=a-1;i++) { if (a%i==0) { break; } } if(i>=a-1) { printf("%4d",a); m++; if(m%10==0) printf("\n"); } } } |
10. multiplication table , Align triangle left
#include<stdio.h> void main() {int i,j; for(i=1;i<10;i++) { for(j=1; j<=i ;j++) printf(“%d*%d=%2d ”,i,j,i*j); printf(“\n”); } } |
11. hold 100~200 Can be 7 Divisible number , Output ten numbers as a line , How many such numbers are finally output .
#Include<stdio.h> main( ) { int n,j=0; for(n=100;n<=200;n++) { if (n%7!=0) continue; printf("%6d",n); j++; if (j%10==0) printf("\n"); } printf(" \n j=%d\n",j); } |
12. Judge a certain number entered m Prime or not . If it's a prime , Output “YES”, If it is not , Output “NO”.
#include<stdio.h> void main( ) { int j,m; printf("Enter an integer number: "); scanf("%d",&m); for (j=2; j<=m-1; j++) if (m%j==0) break; if (j>=m) printf("YES\n"); else printf("NO\n"); } |
13. Programming sequence 2~10000 Perfect numbers within .( A factor of a number ( Except for the number itself ) The sum is equal to the number itself .)
for example :6 What is the best factor 1、2、3, Factor and 1+2+3=6
therefore 6 It's a complete number
#include<stdio.h> void main( ) { int i,j,s; for(i=2;i<=10000;i++) { s=0; for(j=1;j<=i-1;j++) if(i%j==0) s+=j; if(i==s) printf("%6d\n",s); } } |
14. Output 8*8 Black and white chessboard

#include<stdio.h> void main( ) { int i,j; for(i=1;i<=8;i++) { for(j=1;j<=8;j++) if((i+j)%2==0) printf("■"); else printf(" "); printf("\n"); } } |
15. Programming , Output the following graphics .
******* ***** *** * |
#include<stdio.h> void main( ) { int i,j; for (i=1; i<=4; i++) { for (j=1; j<=i; j++) printf(" "); for (j=1;j<=8-(2*i-1);j++) printf("*"); printf("\n"); } } |
16. Output ad 1 - 5000 year ( contain ) All leap years , And count the total number of leap years ;
#include<stdio.h> void main( ) { int i,j=0; for (i=1;i<=5000;i++) if((i%4==0&&i%100!=0)||(i%400==0)) { printf("%5d",i); j++; } printf(" Leap year total :%5d",j); } |
17. Enter a positive integer n (1<n<=10), Input again n It's an integer , In reverse order ( Storage ) Output these numbers .
Such as : Input n=5; Input again 2,5,8,1,4 Save to array ,
Then exchange their positions 4,1,8,5,2. Output
#include <stdio.h> void main( ) { int i, n, temp; int a[10]; scanf("%d", &n); for(i=0; i<n; i++) scanf("%d", &a[i]); for(i=0; i<n; i++) { temp=a[i]; a[i]=a[n-1-i]; a[n-1-i]=temp; } for(i=0; i<n; i++) printf("%d ", a[i]); printf("\n"); } |
18. Program to find a 3x4 The maximum element value of the matrix and its row and column numbers
#include <stdio.h> void main( ) { int i,j,row,col,max,a[3][4]; max=a[0][0]; row=0 ; col=0 ; for(i=0;i<3;i++) for(j=0;j<4;j++) if(a[i][j]>max) { max=a[i][j]; row=i;col=j; } printf("row=%d,col=%d",row,col); } |
19. The initial value in a character array “I am happy!” Lowercase characters in become uppercase output .
#include <stdio.h> void main( ) { char c[10]={'i', ' ',’a’…}; int i; for(i=0;i<10;i++) if(c[i]>='a'&&c[i]<='z')c[i]-=32; for(i=0;i<10;i++) printf(“%c”,a[i]); printf(“\n”); } |
20. Receive a string from the keyboard , Find the number of characters in the string .
#include <stdio.h> void main( ) { char str[30]; int i=0; scanf(“%s”,str); while (str[i] != ‘\0’) i++; printf(“the string lenth is %d”,i); } |
边栏推荐
- Iterator iterator interface
- How does core data save data in SQLite
- Raspberry pie foundation | summarize and record some operations in the learning process of raspberry pie
- 我正在使用中的博客创作工具
- Getting started with scottplot tutorial: getting and displaying values at the mouse
- Chi square distribution and gamma function
- 2022 low voltage electrician examination questions and answers
- unittest执行runTestCase提示<_io.TextIOWrapper name=‘<stderr>‘ mode=‘w‘ encoding=‘utf-8‘>解决方案
- Analysis of thrift serialization protocol
- Create a table under swiftui with table
猜你喜欢

文件批量重命名工具Bulk Rename Utility

When Xcode writes swiftui code, it is a small trap that compiles successfully but causes the preview to crash

On July 29, apachecon | apachepulsar's exploration and practice in vivo will be broadcast soon

Redis persistence

Installing MySQL on Linux

Tdengine helps Siemens' lightweight digital solutions

2022 safety officer-a certificate operation certificate examination question bank simulated examination platform operation

ssh服务

linux安装redis

How to reduce the resolution of only 3D camera but not UI camera
随机推荐
Swiftui layout - alignment
Hcip day 11
Simple data analysis using Weka and excel
【七夕】七夕孤寡小青蛙究极版?七夕节最终章!
看了就会的 Rainbond 入门教程
Brief introduction and use of mqtt entry level
Afnetworking crash course
SwiftUI 布局 —— 对齐
How to make the characters in the photos laugh? HMS core video editing service one click smile function makes people smile more naturally
Log management platform of infrastructure and nail & email alarm notification
How to reduce the resolution of only 3D camera but not UI camera
Focus on differentiated product design, intelligent technology efficiency improvement and literacy education around new citizen Finance
如何只降3D相机不降UI相机的分辨率
MQTT入门级简单介绍与使用
9、 Uni popup usage popup effect at the bottom of the drop-down box
Digital transformation security issues occur frequently, and Shanshi Netcom helps build a digital government
SwiftUI 4.0 的全新导航系统
FormData对象的使用, var formdata=new FormData()
Added the ability of class @published for @cloudstorage
String转为long 类型报错原因:要转为long必须是int、double、float型[通俗易懂]