当前位置:网站首页>Basic exercises of C language for beginners
Basic exercises of C language for beginners
2022-07-24 01:00:00 【lhb2998658795】
1. Character inversion
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
void swap(char *a,char *b)
{
if(a==NULL&&b==NULL){
printf("The ginseng error");
exit(EXIT_SUCCESS);// For beginners, use here return ; It's OK, too , Not affecting use ;
//exit To exit this process
}
int temp=0;
while(a<b){
temp=*a;
*a=*b;
*b=temp;
a++;
b--;
}
}
void change_word(char *a)
{
char *p,*q;
p=a;
q=a+strlen(a)-1;
swap(p,q);
q=p;
// This can still be used while Realization , however while It will increase the time complexity , Use as little as possible
while(*p!=0){
if(*p!=' '){
p++;
if(*p!=0){
continue;
}
}
swap(q,p-1);
if(*p==0){
break;
}
q=(++p);
}
}
int main(int argc, char const *argv[])
{
char buff[128]={0};
printf(" Please enter what you want to enter ");
fgets(buff,sizeof(buff),stdin);// For beginners, use here gets(buff) It's OK, too , Not affecting use ;
buff[strlen(buff)-1]='\0';
change_word(buff);
printf("%s\n",buff);
return 0;
}
2. Output all less than 100 The prime number of
#include <stdio.h>>
int main(int argc, char const *argv[])
{
int n=100;
int i=0;
int j=0;
int a=0;
for(i=1;i<n;i++){
a=0;
for(j=1;j<=i;j++){
if(i%j==0){
a++;
}
}
if(a==2){
printf("%d ",i);
}
}
puts("");
return 0;
}
3 There are three ways of exchange
3.1 The first three cups of water exchange
#include <stdio.h>
int main(int argc, char const *argv[])
{
int a=10;
int b=20;
int temp=0;
temp=a;
a=b;
b=temp;
printf("a=%d b=%d\n",a,b);
return 0;
}
3.2 The second XOR Exchange
#include <stdio.h>
int main(int argc, char const *argv[])
{
int a=10;
int b=20;
int temp=0;
a=a^b;
b=a^b;
a=a^b;
printf("a=%d b=%d\n",a,b);
return 0;
}
3.3 The third exchange // Note that the value beyond the limit cannot be too large
#include <stdio.h>
int main(int argc, char const *argv[])
{
int a=10;
int b=20;
int temp=0;
b=a+b;
a=b-a;
b=b-a;
printf("a=%d b=%d\n",a,b);
4. practice : 1. Design a program , Realization function : Enter a character , If it's capital , Just convert it to lowercase output If it's lowercase , It is converted to uppercase output If it's a number , Just * 10 Output If it's another character , All output # a --> A B --> b 2 --> 20 ( --> #
#include<stdio.h>
int main(int argc, const char *argv[])
{
char v = 0;
scanf("%c", &v);
getchar();
if (v >= 'A' && v <= 'Z'){
v += 32;
printf("%c\n", v);
}else if (v >= 'a' && v <= 'z'){
v -= 32;
printf("%c\n", v);
}else if (v >= '0' && v <= '9'){
v -= '0';
v *= 10;
printf("%d\n", v);
}else{
printf("%c\n", '#');
}
return 0;
}5ifelse Statements use
practice : Write a program to calculate height Every parent cares about the height of their children when they grow up , According to the knowledge of physiological health and mathematical statistics, it is shown that , There are genetic factors that affect the height of children as adults 、 Eating habits and physical exercise . The height of children as adults is closely related to their parents' height and their own gender . set up faHeight For his father's height ,moHeight For the height of his mother , The height prediction formula is : Male adult height = (faHeight + moHeight) * 0.54(cm) Female adult height = (faHeight * 0.923 + moHeight) / 2(cm) Besides , If you love physical exercise , Then you can increase your height on the original basis 2% If you have good healthy eating habits , Then you can increase your height on the original basis 1.5% Procedural requirements : Father's height and mother's height 、 The gender of the child 、 Do you like sports training Refining And whether there are good hygienic eating habits also input from the keyboard , The final output is predicted height . Tips : How to input the child's gender , Prompt can be given on the screen “ Please enter the gender of the child ( Boy input 1, Girl input 0):”, And then through if Statement to determine whether the character entered from the keyboard is 1 still 0. Whether you like physical exercise can also be achieved in a similar way .
#include <stdio.h>
int main(int argc, const char *argv[])
{
double faHeight = 0;
double moHeight = 0;
double childHeight = 0;
double pe_temp = 0,food_temp = 0;// It's used to record height
int sex = 0; // Gender 1 male 0 Woman
int PE = 0; // athletic sports 1 like 0 Don't like
int FOOD = 0;// eating habits 1 Good habit 0 I'm not used to it
printf(" Please enter your father's height :");
scanf("%lf", &faHeight);
printf(" Please enter your mother's height :");
scanf("%lf", &moHeight);
printf(" Please enter the child's gender (1 schoolboy 0 girl student ):");
scanf("%d", &sex);
if(1 == sex){
childHeight = (faHeight + moHeight) * 0.54;
}else{
childHeight = (faHeight * 0.923 + moHeight) / 2;
}
printf(" Please enter whether you like sports (1 like 0 Don't like ):");
scanf("%d", &PE);
printf(" Please enter whether you have good habits (1 Yes 0 No, ):");
scanf("%d", &FOOD);
if(1 == PE){
pe_temp = childHeight * 0.02;
}
if(1 == FOOD){
food_temp = childHeight * 0.015;
}
childHeight = childHeight + pe_temp + food_temp;
printf(" The predicted height of the child is :%.3lf\n",childHeight);
return 0;
}
6. Simple calculator implementation
. Write code , Realize the function of simple calculator ( Operands Is an integer + - * / %): scanf("%d %c %d",); Input :5 + 2 Output :7 Input :10 % 3 Output :1
#include <stdio.h>
int main(int argc, const char *argv[])
{
int num1 = 0, num2 = 0;
char operator = 0;
scanf("%d %c %d", &num1, &operator, &num2);
switch(operator){
case '+':
printf("%d + %d = %d\n", num1, num2, num1+num2);
break;
case '-':
printf("%d ‐ %d = %d\n", num1, num2, num1-num2);
break;
case '*':
printf("%d * %d = %d\n", num1, num2, num1*num2);
break;
case '%':
printf("%d %% %d = %d\n", num1, num2, num1%num2);
break;
case '/':
printf("%d + %d = %.3lf\n", num1, num2, (double)num1/(double)num2);
break;
default:
printf(" I only support + ‐ * / %%\n");
break;
}
return 0;
}
7 Student grade management
#include <stdio.h>
int main(int argc, const char *argv[])
{
int score = 0;
scanf("%d", &score);
getchar();
switch(score/10){
case 10:
case 9:
printf("A\n");
break;
case 8:
printf("B\n");
break;
case 7:
printf("C\n");
break;
default:
printf(" fail, \n");
break;
}
return 0;
}8. Use while loop , Realization 1~100 Sum up .
#include <stdio.h>
int main(){
int i = 1, sum = 0;
while(i<=100){
sum += i;
i++;
}
printf("sum = %d\n",sum);
return 0;
}9.. Monkeys eat peaches
#include <stdio.h>
int main(){
int sum = 1;
int i = 0;
while(i<9){
printf(" The first %d God , Yes %d Peach \n", 10-i, sum);
sum = (sum+1)*2;
i++;
}
printf(" The first %d God , Yes %d Peach \n", 10-i, sum);
return 0;
}10. daffodils
#include <stdio.h>
int main(int argc, const char *argv[])
{
int i = 0;
int ge = 0;
int shi = 0;
int bai = 0;
for(i = 100; i < 1000; i++){
ge = i % 10;
shi = i / 10 % 10;
bai = i / 100;
if(ge*ge*ge + shi*shi*shi + bai*bai*bai == i){
printf("%d\n", i);
}
}
return 0;
}
11 Typesetting shows the following results

#include <stdio.h>
int main(int argc, char const *argv[])
{
char ch='A';
int i=0;
int j=0;
int a=0;
for(i=0;i<8;i++){
for(j=0;j<i-1;j++){
printf("_");
}
for(a=0;a<i;a++){
printf("%c",ch+a);
}
puts("");
}
return 0;
}12. Enter a number , Output all the factors of this number .
#include <stdio.h>
int main(int argc, const char *argv[])
{
int num = 0;
scanf("%d", &num);
getchar();
for(int i= 1; i <= num; i++){
if(num%i == 0){
printf("%d\n", i);
}
}
return 0;
}
13 Perfect number
#include <stdio.h>
int main(int argc, const char *argv[])
{
int i = 0;
int j = 0;
int sum = 0;
for(i = 1; i < 1000; i++){
sum = 0;
for(j = 1; j < i; j++){
if(i%j == 0){
sum += j;
}
}
if(sum == i){
printf("%d\n", i);
}
}
return 0;
}
13 practice : 1. Define a 10 A length of int Array , And all initialized to 0, Loop to assign values to array members , The assigned value comes from keyboard input ; Output the largest element in the array and the subscript of the largest element .
#include <stdio.h>
int main(){
int arr[10] = {0};
int i = 0;
int max_index = 0;
for(i = 0; i < 10; i++){
scanf("%d", &arr[i]);
}
for(i = 1; i < 10; i++){
if(arr[i] > arr[max_index]){
max_index = i;
}
}
printf("max_value = %d , max_index = %d\n",arr[max_index],max_index);
return 0;
}14 Bubble sort
#include <stdio.h>
int main(int argc, const char *argv[])
{
int s[10] = {23,45,65,78,90,55,33,17,96,54};
int i = 0;
int j = 0;
int temp = 0;
int flags=0;
int len = sizeof(s)/sizeof(int);
for(i = 0; i < len; i++){
printf("%d ", s[i]);
}
printf("\n");
for(j = 0; j < len-1; j++){
flags=0;
for(i = 0; i < len-1-j; i++){
if(s[i] > s[i+1]){
temp = s[i];
s[i] = s[i+1];
s[i+1] = temp;
flags=1;
}
}
if(flags==0){
break;
}
}
for(i = 0; i < len; i++){
printf("%d ", s[i]);
}
printf("\n");
return 0;
}
15strlen function
#include <stdio.h>
int mystrlen(char *num)
{
int i=0;
while(*num){
i++;
num++;
}
return i;
}
int main(int argc, char const *argv[])
{
char num[20]="i love china";
int length=0;
length=mystrlen(num);
printf("%d\n",length);
return 0;
}16strcat
#include <stdio.h>
void mystrcat(char *dest, char *src)
{
//1. Determine whether the address is empty , If it is empty, exit
if(dest == NULL || src == NULL){
return ;
}
//2. find dest Of '\0'
while(*dest){
dest++;
}
//3. Copy the data
while(*src){
*dest++ = *src++;
}
//4. to dest Assign an end symbol
*dest = '\0';
}
int main(int argc, const char *argv[])
{
char arr[50] = {0};
char brr[100] = {0};
printf("input string (arr) > ");
gets(arr);
printf("input string (brr) > ");
gets(brr);
mystrcat(brr,arr);
printf("brr = %s\n",brr);
return 0;
}
16strcpy
#include <stdio.h>
void mystrcpy(char *dest, char *src)
{
//1. Determine whether the address is empty , If it is empty, exit
if(dest == NULL || src == NULL){
return ;
}
//2. Copy the data
while(*src){
*dest++ = *src++;
}
//3. to dest Assign an end symbol
*dest = '\0';
}
int main(int argc, const char *argv[])
{
char arr[100] = {0};
char brr[100] = {0};
printf("input string (arr) > ");
gets(arr);
mystrcpy(brr,arr);
printf("brr = %s\n",brr);
return 0;
}17strcmp
#include <stdio.h>
int mystrcmp(char *a,char *b)
{
while(*a&&*b&&*a==*b){
a++;
b++;
}
if((*a-*b)>0){
return 1;
}else if((*a-*b)<0){
return -1;
}else{
return 0;
}
}
int main(int argc, char const *argv[])
{
char a[20]={0};
char b[20]={0};
int flag=0;
printf("Please enter the value of a >");
gets(a);
printf("Please enter the value of b >");
gets(b);
flag=mystrcmp(a,b);
if(flag>0){
printf("a>b\n");
}else if(flag<0){
printf("a<b\n");
}else{
printf("a=b\n");
}
return 0;
}
18 To write atoi function
#include <stdio.h>
void my_gets(char *a,int n)
{
int i=0;
while(i<n){
if((a[i]=getchar())=='\n'){
i++;
break;
}
i++;
}
a[i]=0;
}
int my_atoi(char *s)
{
int num=0;
int sig=1;
if(*s=='-'){
sig=-1;
}
if(*s=='-'||*s=='+'){
s++;
}
while((*s)>='0'&&(*s)<='9'){
num=num*10+*s-'0';
s++;
}
return sig*num;
}
int main(int argc, const char *argv[]){
char array[50]={0};
my_gets(array,100);
printf("%d\n",my_atoi(array));
return 0;
}
19 Fibonacci sequence
#include <stdio.h>
int main(int argc, const char *argv[]){
int s[20]={1,1};
int i=0;
for(i=2;i<20;i++){
s[i]=s[i-1]+s[i-2];
}
for(i=0;i<20;i++){
printf("%d ",s[i]);
}
puts("");
return 0;
}
20 Find the maximum value in the array
#include <stdio.h>
int main(int argc, const char *argv[]){
int arr[10]={0};
int i=0;
for(i=0;i<10;i++){
scanf("%d",&arr[i]);
}
int max=0;
for(i=0;i<10;i++){
if(arr[i]>arr[max]){
max=i;
}
}
printf("%d %d\n",max,arr[max]);
return 0;
}
21 multiplication table
#include <stdio.h>
int main(int argc, char const *argv[])
{
int i=0;
int j=0;
for(i=1;i<10;i++){
for(j=1;j<=i;j++){
printf("%d*%d=%d ",j,i,i*j);
}
puts("");
}
return 0;
}
22 perpetual calendar
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
int main (){
int year = 0, month = 0, day = 0;
int hour = 0, min = 0, sec = 0;
scanf("%d%d%d%d%d%d", &year, &month, &day
, &hour, &min, &sec);// When the input Space off
while(1){
system("clear");// call clear Command clear screen , Realize single line display
printf("%4d-%02d-%02d %02d:%02d:%02d\n",
year, month, day, hour, min, sec);//%02d Is to format the output Later on
sec++;
if(60 == sec){
sec = 0;
min++;
if(60 == min){
min = 0;
hour++;
if(24 == hour){
hour = 0;
day++;
//31 Day month
if(1 == month || 3 == month || 5 == month || 7 == month || 8 == month
|| 10 == month || 12 == month){
if(32 == day){
day = 1;
month++;
}
//30 Day month
}else if(4 == month || 6 == month || 9 == month || 11 == month || 8 == month){
if(31 == day){
day = 1;
month++;
}
// Leap year's 2 month
}else if((year%4==0 && year%100!=0) || year%400==0){
if(30 == day){
day = 1;
month++;
}
// In ordinary years 2 month
}else{
if(29 == day){
day = 1;
month++;
}
}
if(13 == month){
month = 1;
year++;
}
}
}
}
sleep(1);// Sleep 1 second
}
return 0;
}边栏推荐
- 创建自签名证书, 对exe文件进行数字签名
- Bean validation custom container validation chapter ----06
- Leetcode set the intersection size to at least 2
- How to speed up matrix multiplication -- optimizing GEMM (CPU single thread)
- Expérience du système réseau: résoudre les problèmes de ping
- [QNX hypervisor 2.2 user manual]9.1 configuration variables
- What the hell is ThreadLocal doing?
- JS determines whether the element scrolls to the top
- MySQL exercise: all employees reporting to the CEO
- 落枕如何快速缓解
猜你喜欢

Introduction to several scenarios involving programming operation of Excel in SAP implementation project

深入理解协程

NOTICE: PHP message: PHP Warning: PHP Startup: Unable to load dynamic library ‘*****‘

Image processing: Generation 3 × Window of 3

Image processing 1:rgb888_ YCbCr444

Installation and use of appscan

Starfish OS: create a new paradigm of the meta universe with reality as the link

Bert article translation

MySQL's heart index

freemarker
随机推荐
vim常用命令
Analysis of the advantages of the LAAS scheme of elephant swap led to strong performance of ETOKEN
What is promise? What are the benefits of promise
Hot 100 depth first
For data security reasons, the Dutch Ministry of Education asked schools to suspend the use of Chrome browser
Image processing 1:rgb888_ YCbCr444
爬虫请求库的使用2
Bean validation usage article ----05
Tutorial on principles and applications of database system (043) -- MySQL query (V): Sorting Query Results
Accelerating matrix vector multiplication of special matrices with FFT
Modify node temporarily_ Modules package source code and compile reference to modify dependent packages
Seektiger's okaleido has a big move. Will the STI of ecological pass break out?
黑马程序员-接口测试-四天学习接口测试-第四天-Postman读取外部数据文件,读取数据文件数据,iHRM项目实战,员工管理模块,添加员工,批量运行测试用例,生成测试报告,
C language: deep analysis of const keyword
docker mysql
The salary of a tester who has worked for 3 years after job hopping is twice that of the original. The secret is
1000个Okaleido Tiger首发上线Binance NFT,引发抢购热潮
Off screen rendering & FBO
Hot 100 dynamic programming
First knowledge of C language functions