Ask C language exercises!

Program 1

Title: An integer, after adding 100, is a complete square number, and after adding 168, is a complete square number. What is the number?

1. Scheme analysis: If it is judged to be within 65438+ million, add 100 to this number before prescribing, and add 268 to this number before prescribing again. If it is stipulated,

The result is the result if the following conditions are met. Please see the specific analysis:

2. The program source code:

# contains "math.h"

Master ()

{

long int i,x,y,z;

for(I = 1; I< 100000; i++)

{ x = sqrt(I+ 100); /*x is the result of adding 100 to the prescription */

y = sqrt(I+268); /*y is the result of adding 168 */

if(x * x = = I+ 100 & amp; & ampY*y==i+268)/* If the square of the square root of a number is equal to the number, it means that the number is completely squared */

printf("\n%ld\n ",I);

}

}

==============================================================

Procedure 4

Title: Enter a day of a year to decide which day of the year it is.

1. Scheme analysis: Take March 5th as an example, first add up the first two months, and then add five days, which is the day of this year. Under special circumstances, consider adding one more day in leap year, and the input month is greater than 3.

2. The program source code:

Master ()

{

Int day, month, year, harmony and leap;

Printf ("\ nPlease enter year, month and day \ n");

Scanf("%d, %d, %d ",& year & month & day);

Switch(month)/* Calculate the total number of days in the month before a month */

{

Case1:sum = 0; Break;

Case 2: sum = 31; Break;

Case 3: sum = 59 broken;

Case 4: sum = 90 is broken;

Scenario 5: sum =120; Break;

Case 6: sum =151; Break;

Case 7: sum =181; Break;

Case 8: sum = 212; Break;

Case 9: sum = 243; Break;

Case10: sum = 273; Break;

Case11:sum = 304; Break;

Case12: sum = 334; Break;

Default value: printf ("data error"); Break;

}

Sum = sum+days; /* plus the number of days in a day */

if(year % 400 = = 0 | |(year % 4 = = 0 & amp; & amp Year% 100! =0))/* Judge whether it is a leap year */

leap = 1;

other

Leap = 0;

if(leap = = 1 & amp; & Month & gt2)/* If it is a leap year, if the month is greater than 2, the total number of days will be added by one day */

sum++;

Printf ("Today is the %dth day." ,sum); }

==============================================================

Plan 5

Title: Enter three integers X, Y, z Y, Z, please output these three numbers from small to large.

1. program analysis: we try to put the smallest number on x, and compare x and y first. If X >;; Y exchanges the values of X and Y, and then compares X and Z. If X >: Z exchanges the values of X and Z, X can be minimized.

2. The program source code:

Master ()

{

int x,y,z,t;

scanf("%d%d%d ",& ampx & amp; y & amp; z);

if(x & gt; y)

{ t = x; x = y; y = t; }/* Exchange the values of x and y */

if(x & gt; z)

{ t = z; z = x; x = t; }/* Exchange the values of x and z */

If (y>z)

{ t = y; y = z; z = t; }/* Exchange the values of z and y */

Printf ("from small to large: %d %d %d\n", x, y, z);

}

==============================================================

Program 12

Topic: Judge how many prime numbers are between 10 1-200, and output all prime numbers.

1. program analysis: the method of judging the prime number: divide 2 by a number to sqrt (this number), if it can be divisible,

It means that this number is not prime, but prime.

2. The program source code:

# contains "math.h"

Master ()

{

int m,I,k,h=0,leap = 1;

printf(" \ n ");

for(m = 10 1; m & lt=200; m++)

{ k = sqrt(m+ 1);

for(I = 2; I<= k;; i++)

If (m%i==0)

{ leap = 0; Break; }

if(leap) {printf("%-4d ",m); h++;

if(h% 10==0)

printf(" \ n ");

}

leap = 1;

}

Printf ("\ nTotal is %d", h);

}

==============================================================

Program 13

Title: Print out all "Narcissus". The so-called "daffodil number" refers to a three-digit number, and the cubic sum of its digits is equal to this number.

In itself. For example, 153 is a "daffodil number" because 153= 1 cubic +5 cubic +3 cubic.

1. Program analysis: Use for loop to control the number of 100-999, and each number is decomposed into units, tens and hundreds.

2. The program source code:

Master ()

{

int i,j,k,n;

Printf ("Splash" is ");

for(n = 100; n & lt 1000; n++)

{

I = n/ 100; /* Break down into hundreds of */

j = n/ 10% 10; /* Break down into ten digits */

k = n % 10; /* Break down into bits */

if(I * 100+j * 10+k = = I * I * I+j * j * j+k * k * k)

{

printf("%-5d ",n);

}

}

printf(" \ n ");

}

==============================================================

Program 14

Topic: decompose a positive integer into prime factors. For example, enter 90 and print 90=2*3*3*5.

Program analysis: to decompose the prime factor of n, we must first find a minimum prime number k, and then complete it according to the following steps:

(1) If this prime number is just equal to n, it means that the process of decomposing the prime factor is over, just print it out.

(2) If n

Repeat the first step.

(3) If n is not divisible by k, use k+ 1 as the value of k and repeat the first step.

2. The program source code:

/* Zheng int was carved up */

Master ()

{

int n,I;

Printf ("\ nPlease enter a number: \ n");

scanf("%d ",& ampn);

printf("%d= ",n);

for(I = 2; I < = n; i++)

{

And (n! =i)

{

If (n%i==0)

{ printf("%d* ",I);

n = n/I;

}

other

Break;

}

}

printf("%d ",n); }

==============================================================

Program 15

Topic: use the nesting of conditional operators to complete this question: academic performance >; = Students with 90 points are indicated by A, those with 60-89 points are indicated by B, and those below 60 points are indicated by C.

1. program analysis: (a>b)? This is a basic example of a conditional operator.

2. The program source code:

Master ()

{

Int score;

Carbonization grade;

Printf ("Please enter a score \ n");

Scanf("%d ",& score);

Grade = score & gt=90? A': (Score & gt=60? b ':' C ');

Printf("%d belongs to %c ",score, grade);

}

==============================================================

Title: A ball falls freely from the height of 100 meters, and bounces back to half the original height after each landing; Fall again and ask how many meters * * * passed when landing for the first time 10. How high is the rebound of 10?

1. Program analysis: See the following note.

2. The program source code:

Master ()

{

float sn= 100.0,HN = sn/2;

int n;

for(n = 2; n & lt= 10; n++)

{

sn = sn+2 * HN; /* The number of meters passed by * * * when landing for the nth time */

HN = HN/2; /* The height of the nth rebound */

}

Printf ("Total roads are %f\n", sn);

Printf ("the tenth is %f meters \n", HN);

}

==============================================================

Procedure 2 1

Topic: Monkeys eat peaches: On the first day, monkeys picked many peaches and ate half of them at once. He's not addicted. He ate another one.

The next morning, I ate half the remaining peaches and another one. I eat leftovers from the day before every morning.

Half and one. 10 in the morning, when I wanted to eat again, I saw only one peach left. Ask how much you picked on your first day.

1. program analysis: adopt the method of reverse thinking and infer from back to front.

2. The program source code:

Master ()

{

int day,x 1,x2;

Day = 9;

x2 = 1;

while(day & gt; 0)

{ x 1 =(x2+ 1)* 2; /* The number of peaches on the first day is twice that on the second day plus 1 */

x2 = x 1;

Day-;

}

Printf ("Total %d\n", x1);

}

==============================================================

Plan 22

Title: Two table tennis teams compete, each with three players. Team A consists of three people: A, B and C, while Team B consists of three people: X, Y, Z, Y and Z. The competition list has been decided by drawing lots. Someone asked the players about the list of matches. A says he won't compare with X, and C says he won't compare with X and Z. Please make a program to find out.

A list of the players from the third team.

1. program analysis: the method of judging the prime number: divide 2 by a number to sqrt (this number), if it can be divisible,

It means that this number is not prime, but prime.

2. The program source code:

Master ()

{

char i,j,k; /*i is the opponent of A, J is the opponent of B and K is the opponent of C */

for(I = ' x '); I<=' zi++)

for(j = ' x '); j & lt= ' zj++)

{

If (me! =j)

for(k = ' x '); k & lt= ' zk++)

{If (I! = k & amp& ampj! =k)

{If (I! = ' x ' & amp& ampk! = ' x ' & amp& ampk! ='z ')

Printf("order is a-%c\tb-%c\tc-%c\n ",I, J, K);

}

}

}

}

==============================================================

Plan 23

Title: Print out the following pattern (diamond)

*

***

******

********

******

***

*

1. program analysis: first divide the graph into two parts, the first four lines are a rule, and the last three lines are a rule, using double.

For loops, the first layer controls rows and the second layer controls columns.

2. The program source code:

Master ()

{

int i,j,k;

for(I = 0; I < = 3; i++)

{

for(j = 0; j & lt= 2-I; j++)

printf(" ");

for(k = 0; k & lt= 2 * I; k++)

printf(" * ");

printf(" \ n ");

}

for(I = 0; I < = 2; i++)

{

for(j = 0; j & lt= I; j++)

printf(" ");

for(k = 0; k & lt= 4-2 * I; k++)

printf(" * ");

printf(" \ n ");

}

}

==============================================================

Plan 24

Topic: There is a fractional sequence: 2/ 1, 3/2, 5/3, 8/5, 13/8, 2113 ... Find the sum of the top 20 items in this sequence.

1. Program analysis: Please master the changing law of numerator and denominator.

2. The program source code:

Master ()

{

int n,t,number = 20

Floating point numbers a=2, b= 1, s = 0;;

for(n = 1; N<= number; n++)

{

s = s+a/b;

t = a; a = a+b; b = t; /* This part is the key of the program. Please guess the function of T */

}

Printf("sum is %9.6f\n ",s);

}

==============================================================

Plan 25

Title: Find 1+2! +3! +...+20! total

1. Program analysis: This program just turns accumulation into multiplication.

2. The program source code:

Master ()

{

Floating point number n, s=0, t =1;

for(n = 1; n & lt=20; n++)

{

t * = n;

s+= t;

}

printf(" 1+2! +3! ...+20! =%e\n ",s);

}

==============================================================

Plan 26

Topic: Find 5 by recursive method! .

1. Program analysis: recursive formula: fn=fn_ 1*4!

2. The program source code:

# contains "stdio.h"

Master ()

{

int I;

int fact();

for(I = 0; I<5; i++)

printf("\40:%d! =%d\n ",I,fact(I));

}

Internal facts (j)

int j;

{

int sum

If (j==0)

sum = 1;

other

sum = j * fact(j- 1);

Returns the sum;

}

==============================================================

Plan 27

Title: Use recursive function call to print these five characters in reverse order.

1. program analysis:

2. The program source code:

# contains "stdio.h"

Master ()

{

int I = 5;

Void Palin (int n);

printf(" \ 40:");

Palin (me);

printf(" \ n ");

}

Void Palin (n)

int n;

{

char next

If (n<= 1)

{

next = getchar();

printf(" \ n \ 0:");

Putchar (next);

}

other

{

next = getchar();

Palin (n-1);

Putchar (next);

}

}

==============================================================

Plan 28

Topic: There are five people sitting together. How old is the fifth person? He said he was two years older than the fourth man. Asked the fourth person's age, he said he was older than the first one.

All three are two years older. Ask the third person, and he says he is two years older than the second person. Ask the second man and say that he is two years older than the first man. finally

Ask the first person, he said it was 10 years old. How old is the fifth person?

1. program analysis: using recursive method, recursion can be divided into two stages: backward push and recursion. To know the age of the fifth person, you need to know

The age of the fourth person, and so on, to the first person (10 years old), and then back.

2. The program source code:

age

int n;

{

int c;

if(n = = 1)c = 10;

Else c = age (n-1)+2;

Return (c);

}

Master ()

{printf("%d ",age (5));

}

==============================================================

Plan 29

Title: Give a positive integer not exceeding 5 digits. Requirements: 1. Find out how many digits it is; 2. Print out all the numbers in reverse order.

1. program analysis: learn to decompose each digit, as explained below: (here is a simple algorithm provided by Zhao Xin, Class 002, Teachers College)

2. The program source code:

Master ()

{

Long a, b, c, d, e, x;

scanf("%ld ",& ampx);

a = x/ 10000; /* Decompose ten thousand bits */

b = x % 10000/ 1000; /* Break down into thousands */

c = x % 1000/ 100; /* Break down into hundreds of */

d = x % 100/ 10; /* Break down into ten digits */

e = x % 10; /* Break down into bits */

If (a! =0) printf ("there are five,% LD% LD% LD \ n", e, d, c, b, a);

else if (b! =0) printf ("there are four,% LD% LD \ n", e, d, c, b);

else if (c! =0) printf ("There are three,% LD% LD \ n", e, d, c);

else if (d! =0) printf ("There are two, %ld %ld\n", e, d);

else if (e! =0) printf ("Yes 1, %ld\n", e);

}

==============================================================

Program 30

Title: A five-digit number, judging whether it is palindrome or not. That is, 1232 1 is a palindrome, with one digit equal to ten thousand digits and ten digits equal to one thousand digits.

1. Scheme analysis: same as 29 cases.

2. The program source code:

Master ()

{

Brother Long, Stone, Money, Wan, X;

scanf("%ld ",& ampx);

wan = x/ 10000;

Money = x%10000/1000;

Shi = x % 100/ 10;

ge = x % 10;

if(ge = = wan & amp; & Give = = Money)/* Bit equals ten thousand, and ten equals one thousand */

Printf ("This number is a Huiwen \ n");

other

Printf ("This number is not Huiwen \ n");

}

==============================================================

Procedure 3 1

Title: Please enter the first letter of the day of the week to judge what day it is today. If the first letter is the same, continue to judge the second letter.

1. Scheme analysis: it is better to state the situation. If the first letter is the same, the second letter is judged by the situation statement or if statement.

2. The program source code:

# Including

void main()

{

Char letter;

Printf ("Please enter the first letter of a day \ n");

while ((letter=getch())! ='Y')/* End when the pressed letter is Y */

{Switch (letter)

{case 'S':printf ("Please enter the second letter \ n");

if((letter=getch())=='a ')

Printf ("Saturday \ n");

else if ((letter=getch())=='u ')

printf(" Sunday \ n ");

Else printf ("data error \ n");

Break;

case ' F ':printf(" Friday \ n "); Break;

case ' M ':printf(" Monday \ n "); Break;

Case 'T':printf ("Please enter the second letter \ n");

if((letter=getch())=='u ')

Printf ("Tuesday \ n");

else if ((letter=getch())=='h ')

Printf ("Thursday \ n");

Else printf ("data error \ n");

Break;

Case 'W':printf ("Wednesday \ n"); Break;

Default value: printf ("data error \ n");

}

}

}

==============================================================

Program 34

Topic: Practice function calling.

1. program analysis:

2. The program source code:

# Including

void hello_world(void)

{

Printf ("Hello, world! \ n ");

}

Void three _hellos (void)

{

Int counter;

for(counter = 1; Counter < = 3; Counter++)

hello _ world(); /* Call this function */

}

Invalid master (invalid)

{

three _ hello(); /* Call this function */

}

}

==============================================================

Program 36

Subject: Finding Prime Numbers in 100

1. program analysis:

2. The program source code:

# Including

# contains "math.h"

# Definition number 10 1

Master ()

{

int i,j,line,a[N];

for(I = 2; ifor(I = 2; I< sqrt (n); I++)

for(j = I+ 1; j & ltn; J++)

{

If (a[i]! = 0 & amp& ampa[j]! =0)

if(a[j]%a[i]==0)

a[j]= 0; }

printf(" \ n ");

for(i=2,line = 0; I & ltn;; I++)

{

If (a[i]! =0)

{printf("%5d ",a[I]);

line++; }

if(line== 10)

{ printf(" \ n ");

line = 0; }

}

}

==============================================================

Plan 37

Topic: Sort the numbers of 10.

1. Program analysis: You can use the selection method, that is, from the last nine comparison processes, choose the smallest one to exchange with the first element, and so on next time, that is, compare and swap the second element with the last eight elements.

2. The program source code:

# Definition number 10

Master ()

{int i,j,min,tem,a[N];

/* Enter data */

Printf ("Please enter ten numbers: \ n");

for(I = 0; I & ltn;; I++)

{

printf("a[%d]= ",I);

scanf("%d ",& ampa[I]); }

printf(" \ n ");

for(I = 0; I & ltn;; I++)

printf("%5d ",a[I]);

printf(" \ n ");

/* Sort ten numbers */

for(I = 0; I<n-1; I++)

{ min = I;

for(j = I+ 1; j & ltn; J++)

if(a[min]& gt; a[j])min = j;

tem = a[I];

a[I]= a[min];

a[min]= tem;

}

/* Output data */

Printf ("after sorting \ n");

for(I = 0; I & ltn;; I++)

printf("%5d ",a[I]);

}

==============================================================

Program 46

Subject: Macro # Define Command Practice (1)

1. program analysis:

2. The program source code:

# contains "stdio.h"

# Define true 1

# Define false 0

# Define SQ(x) (x)*(x)

void main()

{

int num

Int again =1;

Printf("\40: If the input value is less than 50, the program will stop. \ n ");

When (again)

{

Printf("\40: Please enter a number = = >);

scanf("%d ",& ampnum);

Printf("\40: the square of this number is %d \n ",sq (num));

if(num & gt; =50)

Once again = true;

other

Again = false;

}

}

==============================================================

Program 86

Title: Two String Linkers

1. program analysis:

2. The program source code:

# contains "stdio.h"

Master ()

{ char a[]= " acegikm ";

char b[]= " bdfhjlnpq ";

char c[80],* p;

int i=0,j=0,k = 0;

while(a[i]! = ' \ 0 ' & amp& ampb[j]! ='\0')

{ if(a[I]{ c[k]= a[I]; i++; }

other

c[k]= b[j++];

k++;

}

c[k]= ' \ 0 ';

if(a[i]=='\0 ')

p = b+j;

other

p = a+I;

strcat(c,p);

Put option (c);

}

==============================================================

Program 87

Title: Answer result (structural variable transfer)

1. program analysis:

2. The program source code:

# contains "stdio.h"

Structural students

{ int x;

char c;

} a;

Master ()

{ a . x = 3;

a.c = ' a

f(a);

printf("%d,%c ",a.x,a . c);

}

F (structural student b)

{

b.x = 20

b.c = ' y

}

==============================================================

Program 88

Title: Read 7 integer values (1-50). For each read value, the program prints out the number of *.

1. program analysis:

2. The program source code:

Master ()

{int i,a,n = 1;

while(n & lt; =7)

Do {

scanf("%d ",& ampa);

} while(a & lt; 1 | | a >; 50);

for(I = 1; I < = a; i++)

printf(" * ");

printf(" \ n ");

n++; }

getch();

}

==============================================================

Program 89

Title: A company uses public telephones to transmit data. Data is a four-digit integer, which is encrypted during transmission. The encryption rules are as follows: add 5 to each digit, then replace it with the remainder of sum divided by 10, and then exchange the first and fourth digits, the second and third digits.

1. program analysis:

2. The program source code:

Master ()

{int a,I,aa[4],t;

scanf("%d ",& ampa);

aa[0]= a % 10;

aa[ 1]= a % 100/ 10;

aa[2]= a % 1000/ 100;

aa[3]= a/ 1000;

for(I = 0; I < = 3; i++)

{ aa[I]+= 5;

aa[I]% = 10;

}

for(I = 0; I < = 3/2; i++)

{ t = aa[I];

aa[I]= aa[3-I];

aa[3-I]= t;

}

for(I = 3; I & gt=0; I-)

printf("%d ",aa[I]);

}

==============================================================

Program 90

Topic: the question of upgrading from college to undergraduate depends on the results.

1. program analysis:

2. The program source code:

# contains "stdio.h"

# Define M 5

Master ()

{int a[M]={ 1,2,3,4,5 };

int i,j,t;

I = 0; j = M- 1;

while(I { t = *(a+I);

*(a+I)= *(a+j);

*(a+j)= t;

i++; j-;

}

for(I = 0; i printf("%d ",*(a+I));

}