C language must memorize 18 classic programs, and C language must memorize 100 codes in 2022

1. C language must memorize 18 classic programs, and C language beginners must know

How to learn code, read code and write code for a beginner of C language Just add some keywords. The thousands or even tens of thousands of lines of code you see are written over and over with these statements and keywords. It's just that their logical functions are different. How to quickly get started with C language code? It is recommended to read and write more. The following are the 18 classic programs in C language compiled by Xiaobian.

A large number of C language must-be-recited codes have been organized into a compressed package. Follow the WeChat public account: "C and C plus" Reply: "YM" You can get it 

 

2. C language learning related C language must memorize 18 classic programs to show

1. The C language must memorize the first of 18 classic programs—the multiplication table.

Use C language to output 9*9 formulas. A total of 9 rows and 9 columns, i controls the row, j controls the column. 

 

2. C language must memorize 4×4 arrays of 18 classic programs

The function of the following program is to output a 4×4 array after rotating 90 degrees counterclockwise. The data of the original array is required to be input randomly, and the new array is output in the form of 4 rows and 4 columns. Please complete the program in the blank.

 

3. C language must memorize 18 classical problems related to classic programs

There is a pair of rabbits. From the third month after birth, a pair of rabbits are born every month. After the little rabbit grows to the third month, another pair of rabbits is born every month. If the rabbits are not dead, ask the rabbits of each month. What is the total?

The pattern of the rabbit is the sequence 1, 1, 2, 3, 5, 8, 13, 21… 

 

4. C language must memorize the prime numbers of 18 classic programs

Determine how many prime numbers there are between 101 and 200, and output all prime numbers and the number of prime numbers.

Program analysis: The method of judging the prime number: use a number to remove 2 to sqrt (this number), if it can be divisible, it means that the number is not a prime number, otherwise it is a prime number. 

 

5. C language must memorize the relevant codes of 18 classic programs

A number is called "complete" if it is exactly equal to the sum of its factors. For example, 6=1+2+3. Program to find all the numbers within 1000. 

 

6, C language must memorize 18 classic programs of triangle printing

Program to print right angled Yanghui triangle 

 

7. C language must memorize the average score of 18 classic programs

Enter the grades of 3 students and 4 courses through the keyboard, and find the average grade of each student and the average grade of each course. All grades are required to be put into an array with 4 rows and 5 columns. When entering, use spaces between the data of the same person and press Enter for different people. The last column and the last row respectively put the average grade of each student and the average grade of each course and the class average.

#include <stdio.h>
#include <stdlib.h>
main()
{ float a[4][5],sum1,sum2;
  int i,j;
  for(i=0;i<3;i++)
    for(j=0;j<4;j++)
      scanf("%f",&a[i][j]);
  for(i=0;i<3;i++)
  { sum1=0;
    for(j=0;j<4;j++)
      sum1+=a[i][j];
      a[i][4]=sum1/4;
  }
  for(j=0;j<5;j++)
  {  sum2=0;
     for(i=0;i<3;i++)
       sum2+=a[i][j];
       a[3][j]=sum2/3;
   }
   for(i=0;i<4;i++)
   {  for(j=0;j<5;j++)
      printf("%6.2f",a[i][j]);
      printf("\n");
   }
} 

8. C language must memorize the reverse output of 18 classic programs

Improve the program, realize the reverse order output of the input string, such as input windows output swodniw. 

 

9. The ninth C language must memorize 18 classic programs

The function of the following program is to delete the characters stored in c from the character array s. 

 

10. C language must memorize 18 classic programs----solve sorting problems

Write a void sort(int *x, int n) to sort the n data in the x array from large to small. n and array elements are entered in the main function. Display the result on the screen and output to the file p9_1.out 

 #include<stdio.h>
void sort(int *x,int n)
{
int i,j,k,t;
for(i=0;i<n-1;i++)
{
 k=i;
 for(j=i+1;j<n;j++)
   if(x[j]>x[k]) k=j;
   if(k!=i)
   {
    t=x[i];
    x[i]=x[k];
    x[k]=t;
   }
}
}
void main()
{FILE *fp;
     int *p,i,a[10];
     fp=fopen("p9_1.out","w");
    p=a;
printf("Input 10 numbers:");
for(i=0;i<10;i++)
  scanf("%d",p++);
p=a;
sort(p,10);
for(;p<a+10;p++)
 {  printf("%d ",*p);
        fprintf(fp,"%d ",*p);  }
        system("pause");
    fclose(fp);
} 

11. C language must memorize 18 classic programs to solve the sorting from small to large

It is known that the elements in the array a have been arranged in ascending order. The function of the following program is to insert an input number into the array a. After the insertion, the elements in the array a are still arranged in ascending order. 

 

12. C language must memorize the replacement output of 18 classic programs

Write the function replace(char *s, char c1, char c2) to replace all the characters c1 in the string pointed to by s with c2. The strings, characters c1 and c2 are all input in the main function, and the original string and the replacement The resulting string is displayed on the screen and output to the file p10_2.out

#include<stdio.h>
replace(char *s,char c1,char c2)
{ while(*s!='\0')
   {  if (*s==c1)
         *s=c2;
         s++;
   }
}
main()
{ FILE *fp;
  char str[100],a,b;
   if((fp=fopen("p10_2.out","w"))==NULL)
      { printf("cannot open the file\n");
       exit(0);                     }
   printf("Enter a string:\n");
    gets(str);
    printf("Enter a&&b:\n");
    scanf("%c,%c",&a,&b);
printf("%s\n",str);
fprintf(fp,"%s\n",str);
replace(str,a,b);
printf("The new string is----%s\n",str);
fprintf(fp,"The new string is----%s\n",str);
fclose(fp);
} 

13, C language must memorize 18 classic programs to find

Find a substring s2 in a string s1, and return the starting position of the substring in the main string if it exists, or -1 if it does not exist. 

 

14. C language must memorize 18 classic programs, and use pointer variables to output structure array elements.

struct student
{
 int num;
 char *name;
char sex;
int age;
}stu[5]={
   
   {1001,"lihua",'F',18},{1002,"liuxing",'M',19},{1003,"huangke",'F',19},{1004,"fengshou",'F',19},{1005,"Wangming",'M',18}};
main()
{int i;
struct student *ps;
printf("Num \tName\t\t\tSex\tAge\t\n");
/*用指针变量输出结构体数组元素。*/
for(ps=stu;ps<stu+5;ps++)
printf("%d\t%-10s\t\t%c\t%d\t\n",ps->num,ps->name,ps->sex,ps->age);
/*用数组下标法输出结构体数组元素学号和年龄。*/
for(i=0;i<5;i++)
printf("%d\t%d\t\n",stu[i].num,stu[i].age);
} 

15. C language must memorize fifteen of 18 classic programs

Create a simple linked list with three nodes

 

16, C language must memorize the bubble sort of 18 classic programs

Bubble sort, from small to large, the sorted results are output to the screen and the file myf2.out 

 

17. The C language that outputs a string must memorize the classic program

Enter a string to check if it is a palindrome. A palindrome is a string that is read left-to-right and right-to-left exactly the same string.

 

18. C language must memorize the writing functions of 18 classic programs

Write the function countpi, and use the formula to calculate the approximate value of π. When the value of a certain item is less than 10-5, it is considered that the accuracy requirement is met, please improve the function. Display the results on the screen and output to the file p7_3.out.

 

3. So far, a simple list of 18 classic programs that must be recited in C language End of this article

The 18 classic programs that must be recited in C language have been organized into a compressed package. Follow the WeChat public account: "C and C plus" Reply: "YM" to get it 

 

Guess you like

Origin blog.csdn.net/weixin_55305220/article/details/122025197
Recommended