web123456

C language - 55 basic examples

Table of contents

1. Simple programming

2. Simple calculation

3. Calculate the area of ​​the trapezoid

4. Output the ASCII code corresponding to the character

5. Data type length test

6. Grade judgment

7. Character conversion

8. Use Helen's formula to find the area of ​​a triangle

9. Determine whether a triangle can be formed

10. Sort three numbers from large to small

11. Data statistics

12. Einstein’s Ladder Problem

13. The problem of monkey eating peaches

14. Find the greatest common divisor and the least common multiple of two numbers

15. Find the approximate value of sin(x) Use the formula to find the approximate value of sin(x) (precision is 10e-6).

16. Graphic output

17. String concatenation

18. Simple encryption program

19. Summarize diagonal elements of matrix

20. Data order adjustment

21. Character count

22. Calculation of student grades

23. Name sort

24. Output Yang Hui triangle

25. Use pointers to realize sorting

26. Use pointers to implement string sorting

27. Data inversion

28. Use pointers to adjust data position

29. Use pointers to find the maximum number and its position in a two-dimensional array.

30. Use pointers to implement substring extraction

31. Integer exchange function design

32. Design of statistical function for number characters

33. Sorting function design

34. Matrix transpose function design

35. Find prime number function design

36. Design of the binary conversion function

37. Find the greatest common divisor function design

38. Symmetric array judgment

39. Definition and reference of structure

40. Definition and reference of structure arrays

41. Telephone Book Management Program Design

42. Positive and negative numbers judgment

43. Even digit clear

44. Statistics of characters in text files

45. Binary file operation

46. ​​File content comparison

47. Unexclusive natural numbers

48. 4-bit inverse number

49. Find the speed of the car

50. Armstrong Number

51. Complete number

52. Intimacy Number

53. Keep your own numbers

54. Number of palindromes

55. Four digits with the property abcd=(ab+cd)^2


1. Simple programming

Programming and writing programs, outputting information on the screen: Happy New Year!

/* Output graphics*/
 #include<>
 void main()
 {
     printf("Happy New Year!\n");
 }

2. Simple calculation

Enter 2 numbers at any time from the keyboard to calculate their sum, difference, product and quotient respectively.

/*Simple calculation*/
 #include<>
 void main()
 {
     float a,b;
     float h,c,j,s;
     scanf("%f%f", &a,&b);
     h=a+b;
     c=a-b;
     j=a*b;
     s=a/b;
     printf("sum = %f, difference = %f, product = %f, quotient = %f\n",h,c,j,s);
 }

3. Calculate the area of ​​the trapezoid

The upper bottom, lower bottom and height of the trapezoid are known, and the area of ​​the trapezoid is calculated.

/*Calculate the area of ​​the trapezoid*/
 #include<>
 void main()
 {
     float supline,dowline,high;
     double area;
     scanf("%f",&supline);
     scanf("%f",&dowline);
     scanf("%f",&high);
     area = ((supline+dowline)*high)/2.0;
     printf("%f \n",area);
 }

4. Output the ASCII code corresponding to the character

Enter any character and output the ASCII code corresponding to this character.

/*The ASCII code corresponding to the output characters*/
 #include<>
 void main()
 {
     char c;
     scanf("%c",&c);
     printf("%d\n",c);
 }

5. Data type length test  

Write a program to test the number of bytes occupied by character, short, shaping, long, single-precision real, and double-precision real-precision real-precision computer systems used.

/*Data type length test*/
 #include<>
 void main()
 {
     printf("size of char=%d\n",sizeof(char));
     printf("size of short=%d\n",sizeof(short));
     printf("size of int=%d\n",sizeof(int));
     printf("size of long int=%d\n",sizeof(long int));
     printf("size of float=%d\n",sizeof(float));
     printf("size of double=%d\n",sizeof(double));
 }

6. Grade judgment  

Enter a student's math score, if it is below 60, output "Fail", otherwise, output "Pass". Do not change statements related to input and output.

/*Score judgment*/
 #include<>
 void main()
 {
     float mark;
	 scanf("%f",&mark);
	 if(mark<60)
     printf("Fail\n");
	 else
     printf("Pass\n");
 }

7. Character conversion 

Enter a character, if it is a capital letter, output the corresponding lowercase letter; if it is a lowercase letter, output the corresponding uppercase letter; otherwise, output as is. Do not change statements related to input and output.

/*character conversion*/
 #include<>
 void main()
 {
     char ch;
	 ch=getchar();
	 if(ch>='A'&&ch<='Z')
     {
         ch=ch+32;
	     printf("%c\n",ch);}
	 else if(ch>='a'&&ch<='z')
	 {
         ch=ch-32;
	     printf("%c\n",ch);}
	 else
         printf("%c\n",ch);
 }

8. Use Helen's formula to find the area of ​​a triangle

Three side lengths of triangle

/* Calculate the area of ​​the triangle*/
 #include "" // Introduce math library header files, mainly to calculate square roots using sqrt function
 #include "" // Introduce standard input and output library header files for input and output functions

 void main() // main function
 {
     double a,b,c,s,area; // Define side length a, b, c; half perimeter s; area area; use double type to provide higher numerical accuracy

     printf("Please enter 3 side lengths:"); // prompt the user to enter the three side lengths of the triangle
     scanf("%lf%lf%lf",&a,&b,&c); // Read the three side length values ​​entered by the user

     if(a+b>c&&b+c>a&&a+c>b) // Determine whether the input three sides can form a triangle
     {
         s=1.0/2*(a+b+c); // Calculate the half-perimeter
         area=sqrt(s*(s-a)*(s-b)*(s-c)); // Use the Helen formula to calculate area
         printf("The area of ​​the triangle is: %f\n",area); // Output the area of ​​the triangle
     }
     else // If three sides cannot form a triangle
     {
         printf("These 3 numbers cannot form a triangle!\n"); // The three sides entered by the user cannot form a triangle
     }
 } // End of main function

9. Determine whether a triangle can be formed  

Enter three numbers from the keyboard to determine whether they can be used as three sides of a triangle to form a triangle. If possible, output "Yes", otherwise, output "No".

/*Judge whether it can form a triangle*/
 #include<>
 void main()
 {
     float a,b,c;
     printf("Please enter 3 side lengths:");
     scanf("%f%f%f",&a,&b,&c);
     if(a>0&&b>0&&c>0&&a+b>c&&a+c>b&&b+c>a)
         printf("Yes\n");
     else
         printf("No\n");
 }

10. Sort three numbers from large to small

Enter three numbers from the keyboard, sort them from large to small, and output.

/*Sort three numbers from large to small*/
 #include<>
 main()
 {
     int a,b,c,d,e,f;

     printf("Please enter three integers n1, n2, n3:");
     scanf("%d%d%d",&a, &b, &c);
     printf("Before sorting: %d, %d, %d\n", a, b, c);
     if(a>b)
     {
         if(a<c)
         {
             d=c;
             e=a;
             f=b;
         }
         else
         {
             if(c<b)
             {
                 d=a;
                 e=b;
                 f=c;
             }
             else
             {
                 d=a;
                 e=c;
                 f=b;
             }
         }
     }
     else
     {
         if(b<c)
         {
             d=c;
             e=b;
             f=a;
         }
         else
         {
             if(a>c)
             {
                 d=b;
                 e=a;
                 f=c;
             }
             else
             {
                 d=b;
                 e=c;
                 f=a;
             }
         }
     }
     printf("Sorted: %d, %d, %d\n", d, e, f);
 }

11. Data statistics

Enter n integers arbitrarily to count the sum of odd numbers, odd numbers, even numbers, and even numbers respectively.

/*Data statistics*/
 #include <>
 void main()
 {
     int i,n,m,jishuhe=0,oushuhe=0,jishuge=0,oushuge=0;
     scanf("%d",&n);
     for(i=0; i<n; i++)
     {
         scanf("%d",&m);
         if(m%2!=0)
         {
             jishuhe=jishuhe+m;
             jishuge++;
         }
         else
         {
             oushuhe=oushuhe+m;
             oushuge++;
         }
     }
     printf("%d\n%d\n%d\n%d\n",jishuhe,jishuge,oushuhe,oushuge);
 }

12. Einstein Ladder Problem 

There is a step, each step spans 2 steps, and the last step is left; each step spans 3 steps, and the last step is left; each step is 5 steps, and the last step is 4 steps; each step is 6 steps, and the last step is 5 steps; each step is 7 steps, just to the top of the step. Ask what is the minimum number of steps that meet the conditions.

/*Einstein Ladder Problem*/
 #include <>
 void main()
 {
	 int x;
	 x=7;
	 While(!((x%3==2)&&(x%5==4)&&(x%6==5)))
		 x+=7;
	 printf("%d\n",x);
 }

13. The problem of monkey eating peaches

A monkey picked a pile of peaches a day, and after eating half of it, I felt that it was not satisfied, so I ate one more. On the second day, I ate half of the previous day and ate one more. From now on, I will do this every day until the nth day, and only 1 peach left. I asked how many peaches the monkey picked in total?

/*Monkeys eat peaches*/
 #include<>
 void main()
 {
     int n;
     int x=1,i;
     scanf("%d",&n);
     for(i=1; i<n; i++)
     {
         x=2*(x+1);
     }
     printf("%d\n",x);
 }

14. Find the greatest common divisor and the least common multiple of two numbers

Use the transition phase division (i.e., Euclidean algorithm) to find the greatest common divisor and the least common multiple of two positive integers.

/* Find the greatest common divisor and the least common multiple of two numbers*/
 #include<>
 void main()
 {
     int a,b,rem,temp;
     int Div,Multi;
     int a1,b1;
     scanf("%d%d",&a,&b);
     a1=a;
     b1=b;
     if(a<b)
     {
         temp=a;
         a=b;
         b=temp;
     }
     While(rem)
     {
         rem = a%b;
         a=b;
         b=rem;
     }
     Div=a;
     Multi = a1*b1/Div;
     printf("%d\n%d\n",Div,Multi);
 }

15. Find the approximate value of sin(x) Use the formula to find the approximate value of sin(x) (precision is 10e-6)

sin(x)=x-x3/3!+x5/5!-x7/7!+....(-1)nx(2n+1)/(2n+1)!+...

/* Find the approximate value of sin(x)*/
 #include<>
 #include<>
 void main()
 {
     float a,b=1;
     float i,sinx,x;
     scanf("%f",&x);
     a=x;
     sinx=0;
     for(i=1; fabs(a/b)>=1e-6; i++)
     {
         sinx+=a/b;
         a=-a*x*x;
         b=b*2*i*(2*i+1);
     }
     printf("%f\n",sinx);
 }

16. Graphic output

Enter a character and line number n, and use this character to output the following figure on the screen:
    *
   ***
  *****
 *******
*********
Input: a character and the number of lines to be output n.
Output: The required graphics formed on the screen using this character.

/*Graphic output*/
 #include<>
 void main()
 {
     int n,i,j;
     char ch;
     ch=getchar();
     scanf("%d",&n);
     for(i=0; i<n; i++)
     {
         for(j=0; j<(n-1-i); j++)
             printf(" ");
         for(j=0; j<(2*i+1); j++)
             printf("%c",ch);
         printf("\n");
     }
 }

17. String concatenation   

Enter two strings of characters arbitrarily from the keyboard, without using the library function strcat, and connect two strings of characters.
Input: Two strings of characters
Output: Connect two strings of characters
Input sample: abc
                   def
Output sample: abcdef

/*String concatenation 1*/
 #include<>
 #include<>
 void main()
 {
     int i,j,k;
     char str[20]= {"\0"},str1[10],str2[10];
     gets(str1);
     gets(str2);
     j=strlen(str1),k=strlen(str2);
     for(i=0; i<j+k; i++)
     {
         if(i<j)
             str[i]=str1[i];
         else
             str[i]=str2[i-j];
     }
     puts(str);
 }

 /*String concatenation 2*/
 #include<>
 #include<>
 void main()
 {
	 char str1[100],str2[50];
	 int i,j;
	 gets(str1);
	 gets(str2);
	 for(i=0;str1[i]!='\0';i++);
	 for(j=0;str2[j]!='\0';j++)
	 {
		 str1[i]=str2[j];
		 i++;
	 }
	 str1[i] = '\0';
	 puts(str1);
  }

18. Simple encryption program

Enter a string of characters from the keyboard and encrypt them. The encryption principle is: if it is a letter, move it right by 2 letters, and the other characters remain unchanged.
Input: a string of characters
Output: Encrypted string
Input sample: abl2CDxyz
Output sample: cdl2EFzab
Tip: 1. Gets() and puts() can be used to input and output strings.
2. Encryption using ASCII code operation.
3. The meaning of cyclic right shift is: treat a-z and A-Z as a ring and move it, that is, y is moved by 2 letters right and is a, z is moved by 2 letters right and is b; Y is moved by 2 letters right and is A, and Z is moved by 2 letters right and is B.

/*Simple encryption program*/
 #include<>
 void main()
 {
     char a[80];
     int i;
     gets(a);
     for(i=0; a[i]!=0; i++)
     {
         if((a[i]>='A'&& a[i]<='X')||(a[i]>='a'&&a[i]<='x'))
             a[i]=a[i]+2;
         else if ((a[i]>='y'&&a[i]<='z')||(a[i]>='Y'&&a[i]<='Z'))
             a[i]=a[i]-24;
     }
     puts(a);
 }

19. Summarize diagonal elements of matrix

Enter a 5×5 array and find the sum of the elements on its main diagonal and auxiliary diagonal respectively.
Input: 5×5 array
Output: Sum of elements on the main diagonal and the auxiliary diagonal
Input sample: 1 2 3 4 5
6 7 8 9 10 
11 12 13 14 15 
16 17 18 19 20 
21 22 23 24 25
Output sample: 65 65
Tip: The main diagonal is a line from the upper left corner of the matrix to the lower right corner. In the array, it refers to the elements with the same row and column subscripts, and the auxiliary diagonal is a line from the upper right corner of the matrix to the lower left corner of the matrix. The subscripts of these elements can also be expressed by corresponding expressions.

/*Summarize diagonal elements of matrix*/
 #include<>
 #include<>
 void main()
 {
     int i,j;
     int a[6][6];
     int sum1=0,sum2=0;
     for(i=0; i<5; i++)
         for(j=0; j<5; j++)
             scanf("%d",&a[i][j]);
     for(i=0; i<5; i++)
     {
         sum1=sum1+a[i][i];
         sum2=sum2+a[i][4-i];
     }
     printf("%d %d\n",sum1,sum2);
 }

20. Data order adjustment

Enter 10 numbers at any time from the keyboard, adjust the maximum number to the front and the minimum number to the last.
Input: Any 10 numbers
Output: 10 numbers after data adjustment.
Input sample: 0 1 2 3 4 5 6 7 8 9
Output sample: 9 1 2 3 4 5 6 7 8 0
Tip: ① Define four variables d1, d2, x1, and x2 to store the maximum number, the minimum number and their positions respectively;
② Before the loop starts, use the first element as a reference, store its values ​​into d1 and x1, and store its subscripts into d2 and x2 respectively;
③ Compare other elements with d1 one by one in the loop. If it is larger than d1, store it in d1 and store its subscript into d2; otherwise, compare it with x1. If it is smaller than x1, store it in x1 and store its subscript into x2;
④ After the loop is finished, exchange the number at the position shown in d2 with the first number, exchange the number at the position shown in x2 with the last number, and then output all elements of the array.

/* Data order adjustment*/
 #include<>
 #include<>

 int main ()
 {
     int i,a[10],temp1,temp2;
     for(i=0; i<10; i++)
         scanf("%d",&a[i]);
     int d1=a[0],x1=a[0],x2=0,d2=0;
     for(i=0; i<10; i++)
     {
         if(a[i]>d1){
             d1=a[i];
             d2=i;
         }else if(a[i]<x1)
         {
             x1=a[i];
             x2=i;
         }
     }
     temp1=a[0];
     a[0]=a[d2];
     a[d2]=temp1;

     temp2=a[9];
     a[9]=a[x2];
     a[x2]=temp2;
     for(i=0; i<10; i++)
         printf("%d ",a[i]);
     return 0;
 }

21. Character count

Enter any string from the keyboard, store it in a character array, and count the number of uppercase letters, lowercase letters, numbers and other characters.
Input: Any string
Output: The number of uppercase letters, lowercase letters, numbers and other characters.
Input sample: abcdefg123456ABCDEFG
Output sample: 7 7 6

/*charact statistics*/
 #include<>
 #include<>
 void main()
 {
     char str[100];
     int iA=0,ia=0,io=0,iqt=0;
     int i;
     gets(str);
     for(i=0; str[i]; i++)
     {
         if(str[i]>='A'&&str[i]<='Z')
             iA++;
         else if(str[i]>='a'&&str[i]<='z')
             ia++;
         else if(str[i]>='0'&&str[i]<='9')
             io++;
         else
             iqt++;
     }
     printf("%d %d %d %d %d\n",iA,ia,io,iqt);
 }

22. Calculation of student grades

The results of 5 courses of 10 students are known, and they are deposited into a two-dimensional array to find the total score of each student and the average score of each student.
Enter: 5 courses for 10 students
Output: The total scores of each student and the average scores of each student
Input sample: 90.5 80 70 89 84.6
91.5 81 71 88 84.7
92.5 82 72 87 84.8
93.5 83 73 86 84.9
94.5 84 74 85 84.0
95.5 85 75 84 84.1
96.5 86 76 83 84.2
97.5 87 77 82 84.3
98.5 88 78 81 84.4
99.5 89 79 80 84.5 
Output sample: 414.100006 82.820000
416.200012 83.240005
418.299988 83.659996
420.399994 84.080002
421.500000 84.300003
423.600006 84.720001
425.700012 85.139999
427.799988 85.559998
429.899994 85.979996
432.000000 86.400002

/*Student score calculation*/
 #include""
 void main()
 {
     int i,j;
     float score[10][5];
     float sum[10], aver[10];
     for(i=0; i<10; i++)
         for(j=0; j<5; j++)
             scanf("%f",&score[i][j]);
     for(i=0; i<10; i++)
     {
         sum[i]=0;

         for(j=0; j<5; j++)
         {
             sum[i]=sum[i]+score[i][j];
         }

         aver[i]=sum[i]/5;
     }

     for(i=0; i<10; i++)
         printf("%f %f\n",sum[i],aver[i]);
 }

23. Name sort

Enter the names of 10 students (in pinyin) from the keyboard and sort them from small to large in order of ASCII.
Enter: 10 students' names (in pinyin form).
Output: Sort from small to large in order of ASCⅡ.
Input sample: zhang
ziang
zaang
zbang
zcang
zdang
zeang
zfang
zgang
zhang 
Output sample: zaang
zbang
zcang
zdang
zeang
zfang
zgang
zhang
zhang
ziang

/*Name sort*/
 #include<>
 #include<>
 void main()
 {
     char name[10][10];
     int i,j;
     char temp[20];
     for(i=0; i<10; i++)
         gets(name[i]);
     for(j=0; j<10; j++)
     {
         for(i=0; i<9-j; i++)
             if(strcmp(name[i],name[i+1])>0)
             {
                 strcpy(temp,name[i]);
                 strcpy(name[i],name[i+1]);
                 strcpy(name[i+1],temp);
             }
     }
     for(i=0; i<10; i++)
         puts(name[i]);
 }

24. Output Yang Hui triangle

Programming to implement the output of the n-order (n<10) Yang Hui triangle. When n=6, the Yang Hui triangle is as follows:
1
1  1 
1  2   1
1  3   3   1
1  4   6   4   1
1  5   10  10  5   1 
Input: The order n of Yang Hui triangle.
Output: n-order Yang Hui triangle.
Input sample: 6
Output sample: 1
1  1 
1  2   1
1  3   3   1
1  4   6   4   1
1  5   10  10  5   1 
Tip:   ① Characteristics of Yang Hui’s triangle: the element values ​​on the first column and the diagonal are 1, that is, a[i][0]=a[i][i]=1; the values ​​of other positions = the element value of this column in the previous row + the element value of the previous row and the element value of the previous row. For example, the second column 2 of the third row is the first column of the second row + the second row and the second column. The calculation formula is a[i][j]=a[i-1][j-1]+ a[i-1][j].
② The first column and diagonal elements should be assigned first, and then other elements should be assigned.

/*Output Yang Hui triangle*/
 #include<>
 #include<>
 void main()
 {
     int i,j,n;
     int a[100][100];
     scanf("%d",&n);
     for(i=0; i<n; i++)
     {
         a[i][i]=1;
         a[i][0]=1;
     }
     for(i=0; i<n; i++)
         for(j=1; j<i; j++)
             a[i][j]=a[i-1][j-1]+a[i-1][j];
     for(i=0; i<n; i++)
     {
         for(j=0; j<=i; j++)
             printf("%d",a[i][j]);
         printf("\n");
     }
 }

25. Use pointers to implement sorting  

Implementation with pointers: Enter 10 integers from the keyboard and arrange them from small to large.
Input: 10 integers entered by the keyboard.
Output: Order from small to large.
Input sample: 1 5 4 3 2 9 23 11 5 7
Output sample: 1 2 3 4 5 5 7 9 11 23

/*Use pointers to implement sorting*/
 #include<>
 void main(void)
 {
     int a[10],*p;

     int i,j=0,temp;
     p=a;
     for(i=0; i<10; i++ )
     {
         scanf("%d",p+i);
     }
     p=&a[j];
     for(i=1; i<=9; i++)
         for(j=0; j<10-i; j++)

             if(*(p+j)>*(p+j+1))

             {
                 temp=*(p+j);
                 *(p+j)=*(p+j+1);
                 *(p+j+1) =temp;
             }
     for(i=0; i<9; i++)
         printf("%d ",a[i]);
     printf("%d\n",a[9]);
 }

26. Use pointers to implement string sorting

Implemented with pointers: sort 10 strings (assuming their length is less than 20) from small to large.
Enter: 10 strings.
Output: 10 strings sorted.
Input sample: ijk
jkl
def
ghi
def
cde
hij
def
efg
fgh 
Output sample: cde
def
def
def
efg
fgh
ghi
hij
ijk
jkl 
Tip:     ① Define a two-dimensional character array and an array of pointers to the array;
② Use a loop to make the elements of the pointer array point to the beginning of each row of the two-dimensional character array;
③ Use a loop to assign a string as a value to the pointer array element;
④ Use the strcmp function to compare the values ​​of two pointer array elements, and use the strcpy function to exchange the values ​​of two pointer array elements.

/*Use pointers to implement string sorting*/
 #include<>
 #include<>
 void main()
 {
     char a[10][20],*p[10],b[20];
     int i,j;
     for(i=0; i<10; i++)
     {
         p[i]=a[i];
     }
     for(i=0; i<10; i++)
     {
         gets(p[i]);

     }
     for(i=0; i<9; i++)
     {
         for(j=i+1; j<10; j++)
         {
             if(strcmp(p[i],p[j])>0)
             {
                 strcpy(b,p[i]);
                 strcpy(p[i],p[j]);
                 strcpy(p[j],b);
             }
         }
     }
     for(i=0; i<10; i++)
     {
         puts(a[i]);
     }
 }

27. Data inversion

Implemented with pointers: Invert data in a one-dimensional array with 10 elements.
Input: 10 elements.
Output: Invert the data.
Input sample: 0 1 2 3 4 5 6 7 8 9
Output sample: 9 8 7 6 5 4 3 2 1 0
Tip: Data inversion refers to exchanging the values ​​of elements before and after the array and then outputting them.

/*Data inversion*/
 #include <>
 void main()
 {
     int a[10],b,*p,*q;
     int i;
     for(i=0; i<10; i++ )
     {
         scanf("%d",a+i);
     }
     p=a;
     q=a+9;
     for(p=a,q=a+9; p<q; p++,q--)
     {
         b=*p;
         *p=*q;
         *q=b;
     }
     for(i=0; i<10; i++ )
     {
         printf("%d ",*(a+i));
     }
 }

28. Use pointers to adjust data position

Implementation with pointers: enter 10 integers from the keyboard, adjust the largest one to the last, and adjust the smallest one to the front.
Enter: 10 integers.
Output: The largest one is adjusted to the last, and the smallest one is adjusted to the first.
Input sample: 5 8 7 6 9 4 3 2 1 0
Output sample: 0 8 7 6 5 4 3 2 1 9

/*Use pointers to adjust data position*/
 #include <>
 void main()
 {
     int a[10],*p1,*p2,temp;
     int *b1,*b2;
     int i;
     //p1=a;
     //p2=a;
     for(i=0; i<10; i++ )
     {
         scanf("%d",&a[i]);
     }
     b1=a;
     p1=a+1;
     for(i=1; i<10; i++)
     {
         if(*p1>*b1)
             b1=p1;
         p1++;
     }
     temp=*b1;
     *b1=a[9];
     a[9]=temp;
     b2=a;
     p2=a+1;
     for(i=0; i<10; i++)
     {
         if(*p2<*b2)
             b2=p2;
         p2++;
     }
     temp=*b2;
     *b2=a[0];
     a[0]=temp;
     for(i=0; i<10; i++)
     {
         printf("%d ",*(a+i));
     }
 }

29. Use pointers to find the maximum number and its position in a two-dimensional array 

Implementation with pointers: Find out the maximum number and position in a two-dimensional array (suppose 3 rows and 4 columns).
Input: 2D array (suppose 3 rows and 4 columns).
Output: Maximum number and its position.
Input sample: 1 2 5 4
6 8 7 2
0 2 4 5 
Output sample: 1 1 8

/*Use pointers to find the maximum number and its position in a two-dimensional array*/
 #include<>
 void main()
 {
     int a[3][4],i,j;
     int iMaxRow,iMaxCol;
     for(i=0; i<3; i++)
     {
         for(j=0; j<4; j++)
         {
             scanf("%d",a[i]+j);
         }
     }
     iMaxRow=0;
     iMaxCol=0;
     for(i=0; i<3; i++)
     {
         for(j=0; j<4; j++)
         {
             if(*(*(a+i)+j)>a[iMaxRow][iMaxCol])
             {
                 iMaxRow=i;
                 iMaxCol=j;
             }
         }
     }
     printf("%d %d %d\n",iMaxRow,iMaxCol,a[iMaxRow][iMaxCol] );
 }

30. Use pointers to implement substring extraction

Implementation with pointers: input a string of characters from the keyboard, start with characters with subscript m, take out n characters (m and n are inputted by the keyboard), and form a new string.
Input: Enter a string of characters on the keyboard.
Output: Starting from the characters with subscript m, take out n characters (m and n are input from the keyboard) to form a new string.
Input sample: 0123456
5 2 
Output sample: 56
Tip:   ① Define two character arrays zfsour[100] and zfdest[100], zfsour stores the original string, and zfdest stores the new string;
② Define two pointer variables *pzfsour and *pzfdest; the first address of the zfsour character array;
③ Enter the starting position m (that is, the subscript value) of the characters to be taken from the keyboard and the number of characters to be taken n;
④ Let the pointer variable pzfsour point to zfsour+m, and let pzfdest point to the first address of the zfdest character array;
⑤ Through n loops, the value is assigned and the pointer is moved successively, that is, the n characters in the original string are stored in the new string character array.

/*Use pointers to implement substring extraction*/
 #include<>
 #include<>
 void main(void)
 {
     char szstrsour[80],szstrdest[80],*pszsour,*pszdest;
     int i,m,n;
     gets(szstrsour);
     scanf("%d %d",&m,&n);
     pszsour=szstrsour+m;
     pszdest=szstrdest;
     for(i=0; i<n; i++)
     {
         *pszdest=*pszsour;
         pszdest++;
         pszsour++;
     }
     *pszdest='\0';
     puts(szstrdest);
 }

31. Integer exchange function design

Design a function, exchange any 2 integers, and call this function in the main function.
Input: Any 2 integers
Output: 2 integers after exchange
Input sample: 1 2
Output sample: 2 1

/* Integer exchange function design*/
 #include <>
 void swap(int*,int*);
 void main()
 {
     int a,b;
     scanf("%d%d",&a,&b);
     swap(&a,&b);
     printf("%d %d\n",a,b);
 }
 void swap(int*p1,int*p2)
 {
     int temp;
     temp=*p1;
     *p1=*p2;
     *p2=temp;
 }

32. Design of statistical function for number characters  

Design a function to count the number of numeric characters in any string of characters, and call this function in the main function.
Input: Any string of characters
Output: Number of numeric characters in the string
Input sample: abs1234ajbkc
Output sample: 4

/* Design of statistics function for number characters*/
 #include <>
 int count(char*);
 void main()
 {
     char a[100];
     gets(a);
     printf("%d\n",count(a));
 }
 int count(char *p)
 {
     int b=0;
     While(*p!='\0')
     {
         if(*p>='0'&&*p<='9')
             b++;
         p++;
     }
     return b;
 }

33. Sorting function design

Design a function to sort any n integers (from large to small), and enter the number of data n and n integers in the main function, and call this function to achieve sorting.
Input: n integers
Output: n integers after sorting
Input sample: 10<----Number of data
0 1 2 3 4 5 6 7 8 9<----Data
Output sample: 9 8 7 6 5 4 3 2 1 0

/*Sorting function design*/
 #include<>
 void paixu(int a[],int num);
 void main(void)
 {
     int i,n,m[100];
     scanf("%d",&n);
     for(i=0; i<n; i++)
         scanf("%d",&m[i]);
     paixu(m,n);
     for(i=0; i<n; i++)
         printf("%d ",m[i]);
 }
 void paixu(int a[],int num)
 {
     int i,j,k,temp;
     for(i=0; i<num-1; i++)
     {
         k=i;
         for(j=i+1; j<num; j++)
             if(a[k]<a[j])
                 k=j;
         if(k!=j)
         {
             temp=a[k];
             a[k]=a[i];
             a[i]=temp;
         }
     }
 }

34. Matrix transpose function design

Design a function to transpose any n×n matrix, and call this function in the main function to transpose a 4*4 matrix.
Input: n×n matrix
Output: Transposed n×n matrix
Input sample: 3
1 2 3
4 5 6
7 8 9 
Output sample: 1 4 7
2 5 8
3 6 9

/* Matrix transpose function design*/
 #include<>
 void zhuan(int a[][100],int );
 int main(void)
 {
 int i, j;
 int m[100][100];
     int n;
     scanf("%d", &n);
     for(i=0; i<n; i++)
     {
         for(j=0; j<n; j++)
             scanf("%d",&m[i][j]);
     }
     zhuan(m,n);
     for(i=0; i<n; i++)
     {
         for(j=0; j<n - 1; j++)
             printf("%d",m[i][j]);
         printf("%d\n", m[i][j]);
     }
    
     return 0;
 }
 void zhuan(int a[][100],int n)
 {
     int i,j,temp;
     for(i=0; i<n; i++)
     {
         for(j=0; j<i; j++)
         {
             temp = a[i][j];
             a[i][j] = a[j][i];
             a[j][i] = temp;
         }
     }
 }

35. Find prime number function design

Design a function to determine whether an integer is a prime number. If it is, it returns 1; if it is not, it returns 0; and use this function to find all prime numbers between m-n and count their numbers. m and n are inputted by the keyboard.
Input: Data range m-n.
Output: All prime numbers and numbers between m-n.
Input sample: 100 200
Output sample: 101 103 107 109 113 127 131 137 139 149 151 157 163 167 173 179 181 191 193 197 199
21

/*Search prime number function design*/
 #include<>
 #include<>
 int P(int );
 void main(void)
 {
     int m,num=0;
     int i,j;
     scanf("%d %d",&i,&j);
     for (m=i; m<=j; m=m+1)
     {
         if(P(m))
         {
             printf("%d ",m);
             num++;
         }
     }
     printf("\n%d\n",num);
 }
 int P(int n)
 {
     int found;
     int i;
     double k;
     k=sqrt((double)n);
     found = 1;
     i = 2;
     while(i<=k && found)
     {
         if( n%i ==0)
             found = 0;
         i++;
     }
     return found;
 }

36. Design of the Category Conversion Function 

Design a function to convert any octal data string into decimal data, and call this function in the main function.
Input: an octal number
Output: Decimal number
Input sample: 77
Output sample: 63

/*Binary conversion function design*/
 #include<>
 int conver(char *);
 void main(void)
 {
     char str[10];
     gets(str);
     printf("%d\n",conver(str));
 }
 int conver(char *p)
 {
     int num=0;
     for(; *p!='\0'; p++)
         num=num*8+*p-'0';
     return num;
 }

37. Find the greatest common divisor function design

Design a function, find out the greatest common divisor of any 2 integers, and call this function in the main function.
Input: 2 integers
Output: The greatest common divisor
Input sample: 8 4
Output sample: 4
Tip: You can use the method of dividing the greatest common divisor to find the phase: rem=m%n; m=n;n=rem; If rem=0, m is the greatest common divisor, the program will end; otherwise, the above statement will be executed again.

/*Search for the maximum common divisor function design*/
 #include<>
 int comdivi(int,int);
 void main(void)
 {
     int a,b,com;

     scanf("%d%d",&a,&b);
     com=comdivi(a,b);
     printf("%d\n",com);
 }
 int comdivi(int m,int n)
 {
     int q;
     if(n>m)
     {
         int z;
         z=m;
         m=n;
         n=z;
     }
     do
     {
         q=m%n;
         m=n;
         n=q;
     }
     while(q!=0);
     return m;
 }

38. Symmetric array judgment

Design a function to determine whether a two-dimensional array is a symmetric array (symmetric matrix). If yes, it returns 1; if not, it returns 0, and call this function in the main function to determine whether an array of 4*4 is a symmetric array.
Input: 2D array
Output: Is it a symmetric array?
Input sample: 1 2 3 4
2 5 6 7
3 6 8 9
4 7 9 0 
Output sample: Yes

/*Symmetric array judgment*/
 #include<>
 #include<>
 int array(int*,int);
 void main(void)
 {
	 int i,j;
	 int a[4][4];
	 for(i=0;i<4;i++)
	 {
		 for(j=0;j<4;j++)
			 scanf("%d",&a[i][j]);
	 }
	 if(array((int*)a,4))
		 printf("No\n");
	 else
		 printf("Yes\n");
 }
 int array(int*p,int n)
 {
	 int found;
	 int i,j;
	 found=1;
	 for(i=0;i<n;i++)
	 {
		 for(j=0;j<i;j++)
		 {
			 if(*(p+i*n+j)!=*(p+j*n+j))
			 {
				 found=0;break;
			 }
		 }
	 }
	 return found;
 }

39. Definition and reference of structure 

Define a data type of an employee structure and define employee structure variables.
Enter: Enter an employee's information from the keyboard. (4 data, each occupying one row, and the salary has two decimal places)
Output: Output employee information. (4 data, each occupying one row)
Input sample: zhangping
610103196802262001
21
2183.55 
Output sample: zhangping
610103196802262001
21
2183.55

/*Definition and reference of structure*/
 #include<>
 struct employee
 {
     char name[20];
     char id[20];
     int gl;
     float salary;
 };
 int main()
 {
     struct employee em;
     scanf("%s",);
     scanf("%s",);
     scanf("%d",&);
     scanf("%g",&);
     printf("%s\n%s\n%d\n%g\n",,,,);
     return 0;
 }

40. Definition and reference of structure array  

Define an array of employee structures, enter the information of 5 employees from the keyboard, and print out the highest salary.
Enter: Enter the information of 5 employees from the keyboard (4 data, each taking one row, and the salary has two decimal places).
Output: Print out the highest salary.
Input sample: liuxin
91736527976271265
11
1234.23
liyixin
91736527976271265
11
5234.24
liuxin
91736527943271265
11
1244.25
liuxin
91736527976271265
11
1284.26
liuxin
91736527976271265
11
1232.27 
Output sample: 5234.24

/* Definition and reference of structure array*/
 #include<>
 #define N 5
 struct a
 {
     char name[8];
     int id;
     int NO;
     float salary;
 } a[N];
 void main()
 {
     int i;
     int j=0;
     float max=0;
     for(i=0; i<N; i++)
     {
         scanf("%s%d%d%g",a[i].name,&a[i].id,&a[i].NO,&a[i].salary);
     }
     for(i=0; i<N; i++)
     {
         if(max<a[i].salary)
         {
             max=a[i].salary;
             j=i;
         }
     }
     printf("%g\n",a[j].salary);
 }

41. Telephone Book Management Program Design 

Write a mobile phone book management program and use a structure to implement the following functions:
(1) The mobile phone book contains three contents: name, home appliance, and mobile phone, and create a telephone book containing the above information.
(2) Enter your name and find the person's number.
(3) Insert someone's number.
(4) Enter your name and delete someone's number.
(5) Implement the above functions with subfunctions separately. Write the main function. You can call the corresponding subfunctions according to the needs of the user.
It is recommended to use a structure to complete it.
1. The main function of the program is shown below. Please understand carefully and do not change the code.
int main(void)                               /* Main function */
   {
      int k;
      do
     {
printf("  0:  exit\n");      /* Print menu item */
          printf("  1:  creat\n");
          printf("  2:  search\n");
Tip:           printf("  3:  insert\n");
          printf("  4:  delete\n");
          printf("  5:  output\n");
          printf("please select:");
scanf("%d",&k);              /* Enter selection */
          switch(k)
         {
          case 0:
exit(0);             /* Exit function */
        case 1:
            creat( );
            break;
        case 2:
            search( );
            break;
        case 3:
            insert( );
            break;
        case 4:
            delete( );
            break;
        case 5:
            output( );
break;         /* Calling subfunction */
        default:
            exit(0);
        }
    }
    while(1);
    return 0;
}
2. According to the main function, design the corresponding function to complete the specified function.
3. When creat, you can create while (1) continuously, and when '*' is entered, it ends.
/*3. Telephone Book Management Programming*/
/*1) Mobile phone book contains three contents: name, home appliance, and mobile phone. Create a phone book containing the above information*/
/*(2) Enter your name and find the number of this person. */
/*(3) Insert someone's number. */
/*(4) Enter your name and delete someone's number. */
/*(5) Implement the above functions with subfunctions separately, write the main function, */
/*The corresponding subfunction can be called according to the user's needs. */

#include<>
 #include<>
 #include<>
 int N;
 struct phone
 {
     char name[20];
     char num1[20];
     char num2[20];
 } book[100];
 void creat();
 void search();
 void insert();
 void delete();
 void output();
 int main(void) /* main function */
 {
     int k;
     do
     {
         printf(" 0: exit\n"); /* Print menu item */
         printf(" 1: creat\n");
         printf(" 2: search\n");
         printf(" 3: insert\n");
         printf(" 4: delete\n");
         printf(" 5: output\n");
         printf("please select:");
         scanf("%d",&k); /* Enter selection */
         switch(k)
         {
         case 0:
             exit(0); /* Exit function */
         case 1:
             creat( );
             break;
         case 2:
             search( );
             break;
         case 3:
             insert( );
             break;
         case 4:
             delete( );
             break;
         case 5:
             output( );
             break; /* Call subfunction */
         default:
             exit(0);
         }
     }
     while(1);
     return 0;
 }
 void creat(void)
 {
     char appella[20];
     getchar();
     while(1) /* Input structure array */
     {

         gets(appella);
         if(*appella=='*')
             break; /* Enter *, and end */
         strcpy(book[N].name, appella);
         gets(book[N].num1);
         gets(book[N].num2);
         N++;
     }
 }
 void search(void) /*Search subfunction */
 {
     char appella[20];
     int i=0;
     getchar();
     gets(appella);
     while(i<N && strcmp(book[i].name,appella)!=0)
     {
         i++;
     }
     if(i<N)
         printf("%s %s %s\n",
                book[i].name,book[i].num1,book[i].num2);
     else
         printf("not found");
 }
 void insert(void) /* Insert subfunction */
 {
     getchar();
     gets(book[N].name);
     gets(book[N].num1);
     gets(book[N].num2);
     N++; /* length plus 1 */
 }
 void delete(void) /*Delete subfunction */
 {
     char appella[20];
     int i=0,j;
     getchar();
     gets(appella);
     while(i<N && strcmp(book[i].name,appella)!=0)
     {
         i++;
     }
     if(i<N)
     {
         for(j=i+1; j<N; j++)
         {
             book[j-1] = book[j];
         }
         N--; /* Length minus 1 */
     }
     else
         printf("not found");
 }
 void output(void) /* output subfunction */
 {
     int i;
     for(i=0; i<N; i++)
         printf("%s %s %s\n",
                book[i].name,book[i].num1,book[i].num2);
 }

42. Positive and negative numbers judgment

Enter any hexadecimal integer a from the keyboard and judge that it is at most 0 or 1. If it is 0, output information that this function is a positive number; if it is 1, output information that this function is a negative number.
Enter: integer a.
Output: Output information that this function is a negative number.
Input sample: 0XFFA7 or 0X59
Output sample: is negative number or is positive number

/*Personal and negative numbers*/
 #include<>
 void main(void)
 {
     short num,a;
     int m;
     m= 0x8000;

     scanf("%x",&num);
     a = (m& num)?1:0;
     if(a==0)
         printf("is positive number\n");
     else
         printf("is negative number\n");
 }

43. Even-digit clear 

Enter an integer a arbitrarily from the keyboard, clear its even bits, and retain the other bits.
Enter: integer a.
Output: The even bits are cleared, and the other bits are retained.
Input sample: 0XFFFF
Output sample: 5555<--hexadecimal

/*Even digit clear*/
  #include<>
  void main(void)
  {
	   unsigned short a,b,mask;
	   mask = 0x5555;
	   scanf("%x",&a);
	   b = a&mask;
	   printf("%x\n",b);
  }

44. Statistics of characters in text files

Count the number of characters in a text file.
Enter: Text file name (including full path)
Output: The number of characters in the text file.
Input sample: E:\\DATA\\70\\
Output sample: 16

/* Character statistics in text files*/
 #include<>
 #include<>
 void main(void)
 {
     char ch, filename[200];
     int count = 0;
     FILE *fp; /* Define file pointer */
     scanf("%s", filename);//Enter text file name
     if((fp=fopen(filename,"r"))==NULL)
     {
         printf("File open error!\n");
         exit(1);
     }
     while((ch=fgetc(fp))!=EOF)
     {
         count++;
     }
     printf("%d\n",count);
     if(fclose(fp))
     {
         printf("File close error!\n");
         exit(1);
     }
 }

45. Binary file operation 

It is known that 10 integer data are stored in a text file, and they are stored in another file as binary data.
Enter: The text file name that stores 10 integer data.
Output: Binary data file.
Input sample: E:\\DATA\\72\\
E:\\DATA\\72\\ 
Output sample: 23 45 67 78 45 32 12 56 99 56

/*Binary file operation*/
 #include<>
 #include<>
 int main(vaoid)
 {
     int a[10], i = 0;
     char filename1[80], filename2[80];
     FILE *fp1,*fp2;
     /* Define file pointer */
     scanf("%s", filename1);//Enter the text file name to open
     scanf("%s", filename2);//Enter the binary file name to create
     if((fp1 = fopen(filename1, "rb")) == NULL)
     {
         printf("Input file open error!\n");
         exit(1);
     }
     if((fp2 = fopen(filename2, "wb")) == NULL)
     {
         printf("Result file open error!\n");
         exit(1);
     }
     for(i=0; i<10; i++)
     {
         fscanf(fp1,"%d",&a[i]);
         fwrite(&a[i],sizeof(int),1,fp2);
     }
     if( fclose(fp1))
     {
         printf("Result file close error!\n");
         exit(1);
     }
     if( fclose(fp2))
     {
         printf("Result file close error!\n");
         exit(1);
     }
     if((fp1 = fopen(filename2, "rb")) == NULL)
     {
         printf("Result file open error!\n");
         exit(1);
     }
     for(i = 0; i < 10; i++)
     {
         fread(&a[i], sizeof(int), 1, fp1); /* Read data */
     }
     if( fclose(fp1))
     {
         printf("Result file close error!\n");
         exit(1);
     }
     for(i = 0; i < 9; i++)
         printf("%d ", a[i]);
     printf("%d\n", a[i]);
     return 0;
 }

46. ​​File content comparison

Suppose the number of characters in the two text files is equal, and compare whether the contents in the two text files are consistent. If different outputs the positions of different characters for the first time.
Enter: 2 text file full path names.
Output: Compare whether the contents in the 2 text files are consistent, if different outputs are the positions of different characters for the first time.
Input sample: E:\\DATA\\73\\
E:\\DATA\\73\\ 
Output sample:

#include <>
#include <>
int main(void)
{
    int i =1, flag = 0;
    char filename1[80], filename2[80];
    char a,b;
    FILE *p1;
    FILE *p2;
    scanf("%s", filename1);
    scanf("%s", filename2);
    if((p1=fopen(filename1,"r"))==NULL)
    {
       printf("Input file1 open error!\n");
       exit(1);
    }
    if((p2=fopen(filename2,"r"))==NULL)
    {
       printf("Input file2 open error!\n");
       exit(1);
    }
    for(;!flag&&feof(p1)==0;i++)
    {
        fscanf(p1,"%c",&a);
        fscanf(p2,"%c",&b);
        if(a!=b)
        {
            flag=1;
            printf("%d\n",i);
        }
    }
    if(flag==0)
    printf("is equal\n");
    fclose(p1);
    fclose(p2);
     return 0;
}

47. Unexclusive natural numbers

A natural number is divided by 8 and the remaining 1 is obtained by 8. The second quotient is divided by 8 and the remaining 7 is then divided by 8. The final quotient is a. It is also known that this natural number is divided by 17 and the remaining 4 is obtained, and the quotient is divided by 17 and 15 is obtained, and finally a quotient is twice that of a. Find this natural number.
Input: None
Output: Natural number that meets the conditions.
Input sample: None
Output sample: 1993
Tip: According to the question, you can set the last quotient as i (i starting from 0), and use the reverse method to list the relationship: (((i*8+7)*8)+1)*8+1=((2*i*17)+15)*17+4 and then use the test method to find the value of quotient i.

#include<>
 int main()
 {
     int i;
     for(i=0;; i++) /*Test the quotient value*/
         if(((i*8+7)*8+1)*8+1==(34*i+15)*17+4)
         {
             /*Reverse estimation determines whether the current i value obtained satisfies the relationship*/
             /*If it is satisfied, the output result*/
             printf("%d\n",(34*i+15)*17+4);
             break; /*Exit loop*/
         }
     return 0;
 }

48. 4-bit inverse number

Suppose N is a four-digit number, and its 9 times is exactly its inverse number, find N. An inverse number is an integer formed by inverting the numbers of integers. For example: the inverse number of 1234 is 4321.
Input: None
Output: 4-bit inverse number
Input sample: None
Output sample: 1089
Tip: You can assuming that the thousand, hundred, ten, and single digits of the integer N are i, j, k, l, and their values ​​are 0~9, then the i, j, k, l that satisfies the relationship: (i*10^3+j*10^2+10*k+l)*9=(l*10^3+k*10^2+10*j+i) constitutes N.

#include<>
 int main()
 {
     int i;
     for(i=1002; i<1111; i++) /* Exhaust the possible values ​​of four digits*/
         if(i%10*1000+i/10%10*100+i/100%10*10+i/1000==i*9)
             /*Judge whether the inverse number is 9 times the original integer*/
             printf("%d\n",i);
     /*If so, output*/
     return 0;
 }

49. Find the speed of the car

A car driving at a fixed speed, the driver saw at 10 a.m. that the reading on the odometer was a symmetrical number (that is, the number is exactly the same as reading from left to right and from right to left), which is 95859. Two hours later, a new symmetry appeared on the odometer. What is the speed of the car? What is the new symmetry number?
Input: None
Output: Number of symmetry and speed that meets the conditions
Input sample: None
Output sample: 95959
50.00
Tip: According to the question, assuming the symmetric number sought is i, and its initial value is 95859, increment the value sequentially, decompose each bit of the value i and compare it with the number at its symmetric position. If the numbers at each symmetric position are equal, then it can be determined that i is the symmetric number to be found.

#include<>
 int main()
 {
     int t,a[5]; /*Array a stores the decomposed digits*/
     long int k,i;
     for(i=95860;; i++) /* Use 95860 as the initial value, cycle test*/
     {
         for(t=0,k=100000; k>=10; t++) /*Each digit of i value taken from high to low*/
         {
             /* characters are stored in a[0]~a[5] in sequence*/
             a[t]=(i%k)/(k/10);
             k/=10;
         }
         if((a[0]==a[4])&&(a[1]==a[3]))
         {
             printf("%d%d%d%d%d\n",
                    a[0],a[1],a[2],a[3],a[4]);
             printf("%.2f\n",(i-95859)/2.0);
             break;
         }
     }
     return 0;
 }

50. Armstrong Number

If a positive integer is equal to the cube sum of its respective numbers, the number is called the Armstrong number (also known as the narcissistic number). For example, 407=4^3+0^3+7^3 is an Armstrong number. Try to program and find all Armstrong numbers within 1000.
Input: None
Output: Armstrong Number
Input sample: None
Output sample:   153 ​​ 370  371  407
Tip: When outputting, the format string of printf() is "%5d".

#include<>
 int main()
 {
     int i,t,k,a[3];
     //printf("There are follwing Armstrong number smaller than 1000:\n");
     for(i=2; i<1000; i++) /*The value range of the number i to be determined in exhaustively is 2~1000*/
     {
         for(t=0,k=1000; k>=10; t++) /*Intercept each bit of the integer i (from high to low)*/
         {
             a[t]=(i%k)/(k/10); /* Assign to a[0]~a[2}*/
             k/=10;
         }
         if(a[0]*a[0]*a[0]+a[1]*a[1]*a[1]+a[2]*a[2]*a[2]==i)
             /*Judge whether i is an Armstrong number*/
             printf("%5d",i); /*If the condition is met, output */
     }
     printf("\n");
     return 0;
 }

51. Complete number

If a number is exactly equal to the sum of its factors, then the number is called a "complete number" and find a complete number within 1000.
Input: None
Output: Complete number
Input sample: None
Output sample:      6   28  496
Tip: 1. According to the definition of the complete number, first calculate the selected integer a (the value of a is 1~1000), and accumulate each factor in m. If m is equal to a, then a can be confirmed to be a complete number.
2. Use printf("%4d",a) for output.

#include<>
 int main()
 {
     int a,i,m;
     //printf("There are following perfect numbers smaller than 1000:\n");
     for(a=1; a<1000; a++) /*Cycle control selects the numbers from 1 to 1000 for judgment*/
     {
         for(m=0,i=1; i<=a/2; i++) /* Calculate the factor of a and add the sum of each factor m=a, then a is the complete number output*/
             if(!(a%i))m+=i;
         if(m==a)
             printf("%4d",a);
     }
     printf("\n");
     return 0;
 }

52. Intimacy Number

If the sum of all factors of integer A (including 1, excluding A itself) is equal to B; and the sum of all factors of integer B (including 1, excluding B itself) is equal to A, the integers A and B are called intimate numbers. Find all intimate numbers within 3000.
Input: None
Output: Intimacy Number
Input sample: None
Output sample: 220<->284
1184<->1210
2620<->2924

/*Intimacy Number*/
 #include<>
 void main()
 {
     int a,i,b,n;
     
     for(a=1; a<3000; a++)
     {
         for(b=0,i=1; i<=a/2; i++)
             if(!(a%i))b+=i;
         for(n=0,i=1; i<=b/2; i++)
             if(!(b%i))n+=i;
         if(n==a&&a<b)
             printf("%d<->%d\n",a,b);
     }
 }

53. Keep your own numbers

A self-conserving number refers to the fact that the square mantissa of a number is equal to the natural number of the number itself. For example: 25^2=625, 76^2=5776, 9376^2=87909376, request a self-secure number within 200,000.
Input: None
Output: Self-contained number
Input sample: None
Output sample: 0 1 5 6 25 76 376 625 9376 90625 109376
Tip: It is obviously undesirable to use the method of "finding the square of a number and then intercepting the last corresponding number of digits" because the computer cannot represent an excessively large integer.

#include<>
 int main()
 {
     long mul,number,k,ll,kk;
     //printf("It exists following automatic nmbers small than 200000:\n");
     for(number=0; number<200000; number++)
     {
         for(mul=number,k=1; (mul/=10)>0; k*=10);
         /*Determine the coefficient k when intercepting the number and multiplication by multiplication by number*/
         kk=k*10; /*kk is the coefficient when intercepting the partial product*/
         mul=0; /*The last n bits of the product*/
         ll=10; /*ll is the coefficient when intercepting the corresponding bit of the multiplier*/
         While(k>0)
         {
             mul=(mul+(number%(k*10))*(number%ll-number%(ll/10))%kk;
             /*(partial product + intercept the N bit of the multiplier* intercept the M-th bit of the multiplier), %kk then intercept the partial product*/
             k/=10; /*k is the coefficient when intercepting the multiplier*/
             ll*=10;
         }
         if(number==mul) /*If it is determined to be a self-conservative number, output*/
             printf(" %ld",number);
     }
     printf("\n");
     return 0;
 }

54. Number of palindromes

Print all numbers (also called palindrome numbers) with symmetric properties that do not exceed n (taken n<256).
Input: None
Output: integer that meets the conditions
Input sample: None
Output sample:  1           1
 2         2         4
 3         3         9
 4        11       121
 5        22       484
 6        26       676
 7       101     10201
 8       111     12321
 9       121     14641
10       202     40804
11       212     44944 
Tip: 1. For the number n to be judged, calculate its square (stored in a), decompose each bit of a, and then restore it to a number k in the order from low to high (such as n=13, then a=169 and k=961). If a is equal to k, it can be determined that n is the number of palindrome.
2. Use the output format string: "%2d%10d%10d\n".

#include<>
 int main(void)
 {
     int m[16],n,i,j,t,count=0;
     long unsigned a,k;
     //printf("No. number it's square(palindrome)\n");
     for(n=1; n<256; n++) /*Exhausted value range of n*/
     {
         k=0;
         t=1;
         a=n*n; /* calculates the square of n*/
         for(i=0; a!=0; i++) /*Each bit of the decomposition number a from low to high is stored in the array m[0]~m[16]*/
         {
             m[i]=a%10;//This is the single bit of obtaining a, and the entire cycle can obtain each bit
             a/=10;
         }
         j=0;
         for(i--; j<i; j++,i--) //Because all bits of n square are in the array, let's determine whether it is symmetric
             if(m[j]!=m[i])break;//As long as there is a one that is not symmetric, it means it is not symmetric and you can exit
         //All bits are symmetrical, which means they are symmetrical, so that the result can be printed
         if(j>=i)printf("%2d%10d%10d\n",++count,n,n*n);

     }
     return 0;
 }

55. Four digits with the property abcd=(ab+cd)^2

The number 3025 has a unique property: divide it into two segments, that is, 30 and 25, add them and square them, that is, (30+25)^2, which is exactly equal to 3025 itself. All four-digit numbers of such properties are requested.
Input: None
Output: Four digits with the property abcd=(ab+cd)^2
Input sample: None
Output sample: 2025 3025 9801

#include<>
 int main()
 {
     int n,a,b;
     //printf("There are following number with 4 digits satisfied condition\n");
     for(n=1000; n<10000; n++) /*The value range of four-digit N is 1000~9999*/
     {
         a=n/100; /* The first two digits of intercept N are stored in a*/
         b=n%100; /*The last two digits of N intercepted are stored in b*/
         if((a+b)*(a+b)==n) /*Judge whether N is a four-digit number that meets the properties specified in the question*/
             printf("%d ",n);
     }
     return 0;
 }