C programming language ................

Program 1

Title: There are 1, 2, 3, 4 numbers. How many different three digits can you make up? How much are they altogether?

1. Program analysis: The numbers that can be filled in in hundreds, tens and units are all 1, 2, 3 and 4. Make all the arrangements before you leave.

Delete unqualified arrangements.

2. The program source code:

Master ()

{

int i,j,k;

printf(" \ n ");

for(I = 1; I<5; I++)/* The following is a triple cycle */

for(j = 1; j & lt5; j++)

for(k = 1; k & lt5; k++)

{

If (me! = k & me! = j & amp& ampj! =k) /* Make sure that I, J and K are different from each other */

printf("%d,%d,%d\n ",I,j,k);

}

}

Program 2

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 the result after prescription meets the following conditions, it is a result. 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);

}

}

Program 3

Title: Output 9*9 formula.

1. Program analysis: branch columns are considered, ***9 rows and 9 columns, I control row and J control column.

2. The program source code:

# contains "stdio.h"

Master ()

{

int i,j,result

printf(" \ n ");

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

{ for(j = 1; j & lt 10; j++)

{

Result = I * j;;

printf("%d*%d=%-3d ",I,j,result); /*-3d means left alignment, accounting for 3 digits */

}

printf(" \ n "); /* Line breaks after each line */

}

}

Procedure 4

Title: output chessboard is required.

1. Program analysis: use I to control rows, use J to control columns, and control whether to output black squares or white squares according to the change of the sum of I+J.

2. The program source code:

# contains "stdio.h"

Master ()

{

int i,j;

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

{

for(j = 0; j & lt8; j++)

If ((i+j)%2==0)

printf("%c%c ",2 19,2 19);

other

printf(" ");

printf(" \ n ");

}

}

Plan 5

Topic: Classic question: There is a pair of rabbits. From the third month after birth, they give birth to a pair of rabbits every month. After the third month, the little rabbit gives birth to a pair of rabbits every month. If rabbits don't die, what is the total number of rabbits per month?

1. Program analysis: the rabbit's rule is the sequence of 1, 1, 2,3,5,8,13,21. ....

2. The program source code:

Master ()

{

long f 1,F2;

int I;

f 1 = F2 = 1;

for(I = 1; I & lt=20; i++)

{ printf(" % 12ld % 12ld ",f 1,F2);

if(I % 2 = = 0)printf(" \ n "); /* Control output, four per line */

f 1 = f 1+F2; /* Add the first two months and allocate them to the third month */

F2 = f 1+F2; /* Add the first two months and allocate them to the third month */

}

}

Program 6

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

1. program analysis: the method of judging a prime number: divide a number by 2 to sqrt (this number). If it is divisible, it means that this number is not a prime number, and vice versa.

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);

}

Procedure 7

Title: Print out all "Narcissus". The so-called "daffodil number" refers to three digits, the sum of which is equal to the number 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 8

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

(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 9

Title: Enter two positive integers M and N, and find their greatest common divisor and least common multiple.

1. Program analysis: using rolling division.

2. The program source code:

Master ()

{

int a,b,num 1,num2,temp

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

scanf("%d,%d ",& ampnum 1。 num 2);

if(num 1 & lt; Num2)/* Exchange two numbers, so that the big number is put in num 1 */

{ temp = num 1;

num 1 = num 2;

num2 = temp

}

a = num 1; b = num2

And (b! =0)/* Use rolling division until b is 0 */

{

temp = a % b;

a = b;

B = temperature;

}

printf("gongyueshu:%d\n ",a);

Printf ("Gongbeishu: %d\n", num1* num 2/a);

}

Program 10

Title: Enter a line of characters and count the number of English letters, spaces, numbers and other characters.

1. Program Analysis: Use the while statement, provided that the input character is not' \n'.

2. The program source code:

# contains "stdio.h"

Master ()

{ char c;

Int letters =0, spaces =0, numbers =0, others = 0;

Printf ("Please enter some characters \ n");

while((c=getchar())! ='\n ')

{

if(c & gt; = ' a ' & amp& ampc & lt= ' z ' | | c & gt= ' A ' & amp& ampc & lt='Z ')

Letter++;

else if(c== ' ')

space++;

else if(c & gt; = ' 0 ' & amp& ampc & lt='9')

digit++;

other

Other++;

}

Printf ("all in all: char =% d space =% d digit =% d others =% d \ n", letters,

Spaces, numbers, others);

}

Program 1 1

Topic: Find the value of s=a+aa+aaa+aaaa+aa ... A, where A is a number. For example, 2+22+222+2222+22222 (at this time * * has five numbers to add), and the addition of several numbers is controlled by the keyboard.

1. Program analysis: The key is to calculate the value of each item.

2. The program source code:

Master ()

{

int a,n,count = 1;

long int sn=0,TN = 0;

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

Scanf("%d, %d ",& i, & n);

printf("a=%d,n=%d\n ",a,n);

while(count & lt; =n)

{

TN = TN+a;

sn = sn+TN;

a = a * 10;

++counting;

}

Printf("a+aa+...=%ld\n ",serial number);

}

Program 12

Title: If a number is exactly equal to the sum of its factors, it is called "perfect number". Such as 6 = 1+2+3. The program finds all the perfect numbers in 1000.

1. Scheme analysis: Please refer to Scheme 8.

2. The program source code:

Master ()

{

Static int k [10];

int i,j,n,s;

for(j = 2; j & lt 1000; j++)

{

n =- 1;

s = j;

for(I = 1; I & ltj;; i++)

{

If ((j%i)==0)

{ n++;

s = s-I;

k[n]= I;

}

}

If (s==0)

{

Printf("%d is a million trees ",j);

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

printf("%d ",k);

printf("%d\n ",k[n]);

}

}

}

Program 13

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);

}

Program 14

Topic: Monkeys Eat Peaches: On the first day, the monkey picked a few peaches and ate half of them at once, which was not enough. He ate another one. The next morning, he ate half the remaining peaches and another one. After that, eat the remaining half of the day before every morning. 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);

}

Program 15

Topic: There is a string of peaches on the beach. Five monkeys will share them. The first monkey divided the pile of peaches into five parts and one part. The monkey threw the extra one into the sea and took one. The second monkey divided the remaining peaches into five parts equally, and there was one part left. It also threw the extra one into the sea and took part of it. The third, fourth and fifth monkeys also asked, how many peaches are there on the beach?

1. program analysis:

2. The program source code:

Master ()

{int i,m,j,k,count

for(I = 4; I< 10000; i+=4)

{ count = 0;

m = I;

for(k = 0; k & lt5; k++)

{

j = I/4 * 5+ 1;

I = j;

if(j%4==0)

count++;

other

Break;

}

I = m;

if(count==4)

{printf("%d\n ",j);

Break; }

}

}

Program 16

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 compete with X, and C says he won't compete with X and Z. Please make a program to find out the names of the three teams.

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);

}

}

}

}

Program 17

Title: Print out the following patterns.

*

***

*****

*******

*****

***

*

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. Use double for loop, 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 ");

}

}

Program 18

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 19

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);

}