100 C Programs

Source: Internet
Author: User

[Procedure 1]
Question: How many three numbers can be composed of 1, 2, 3, and 4 numbers that are different from each other and have no repeated numbers? What is it?
1. Program Analysis: It can be filled in hundreds of digits, ten digits, and one digit is 1, 2, 3, and 4. Make up all sorts and then proceed
Drop the order that does not meet the conditions.
2. program source code:
Main ()
{
Int I, J, K;
Printf ("/N ");
For (I = 1; I <5; I ++)/* The following is a triple loop */
For (j = 1; j <5; j ++)
For (k = 1; k <5; k ++)
{
If (I! = K & I! = J & J! = K)/* Ensure that I, j, and K are different */
Printf ("% d, % d, % d/N", I, j, k );
}
}
========================================================== ======================================
[Procedure 2]
Question: The bonus paid by an enterprise is based on the profit. If the profit (I) is less than or equal to 0.1 million yuan, the bonus can be raised by 10%. The profit is high.
If the price is less than 0.1 million yuan, the price of the part lower than 0.2 million yuan is 0.1 million, and the price of the part higher than 10% yuan is 0.1 million.
7.5%; 0.2 million to 0.4 million, more than 0.2 million yuan, can be raised to 5%; 0.4 million to 0.6 million is higher
For the part of 0.4 million yuan, it can be raised to 3%; for the part between 0.6 million and 1 million, the part above 0.6 million yuan can be raised to 1.5%, higher
1 million Yuan, more than 1 million yuan of part of the 1% commission, input the current month's profit from the keyboard I, the total number of bonuses payable?
1. Program Analysis: Use the number axis to divide and locate. Note that you need to define the bonus type as a growth integer.
2. program source code:
Main ()
{
Long int I;
Int bonus1, bonus2, bonus4, bonus6, bonus10, bonus;
Scanf ("% lD", & I );
Bonus1 = 100000*0.1; bonus2 = bonus1 + 100000*0.75;
Bonus4 = bonus2 + 200000*0.5;
Bonus6 = bonus4 + 200000*0.3;
Bonus10 = bonus6 + 400000*0.15;
If (I <= 100000)
Bonus = I * 0.1;
Else if (I <= 200000)
Bonus = bonus1 + (i-100000) * 0.075;
Else if (I <= 400000)
Bonus = bonus2 + (i-200000) * 0.05;
Else if (I <= 600000)
Bonus = bonus4 + (i-400000) * 0.03;
Else if (I <= 1000000)
Bonus = bonus6 + (i-600000) * 0.015;
Else
Bonus = bonus10 + (i-1000000) * 0.01;
Printf ("bonus = % d", bonus );
}

========================================================== ======================================
[Procedure 3]
Question: an integer. After 100 is added, it is a full number of workers, and 168 is a full number of workers. What is this number?
1. Program Analysis: If the value is less than 0.1 million, add 100 to the number before the start, and then add 268 to the number before the start. If
The following conditions are met. See the specific analysis:
2. program source code:
# Include "math. H"
Main ()
{
Long int I, x, y, z;
For (I = 1; I <100000; I ++)
{X = SQRT (I + 100);/* X indicates the result after 100 is added */
Y = SQRT (I + 268);/* y indicates the result after the 168 clause is added */
If (x * x = I + 100 & Y * Y = I + 268)/* If the square root of a number is equal to this number, this indicates that this number is the total number of shards */
Printf ("/n % LD/N", I );
}
}
========================================================== ======================================
[Procedure 4]
Question: enter a day of a year to determine the day of the year?
1. Program Analysis: Taking January 1, March 5 as an example, we should add up the previous two months, and then add 5 days, that is, the day of the current year.
In this case, an additional day is required when the input month is greater than 3 in a leap year.
2. program source code:
Main ()
{
Int day, month, year, sum, leap;
Printf ("/nplease input year, month, day/N ");
Scanf ("% d, % d, % d", & year, & month, & Day );
Switch (month)/* calculate the total number of days in the previous month */
{
Case 1: Sum = 0; break;
Case 2: Sum = 31; break;
Case 3: Sum = 59; break;
Case 4: Sum = 90; break;
Case 5: Sum = 120; break;
Case 6: Sum = 151; break;
Case 7: Sum = 181; break;
Case 8: Sum = 212; break;
Case 9: Sum = 243; break;


Author: zhlei81 reply to this speech

--------------------------------------------------------------------------------

2. 100 typical C source code examples
Case 10: Sum = 273; break;
Case 11: Sum = 304; break;
Case 12: Sum = 334; break;
Default: printf ("data error"); break;
}
Sum = sum + day;/* plus the day of the day */
If (Year % 400 = 0 | (Year % 4 = 0 & year % 100! = 0)/* determines whether it is a leap year */
Leap = 1;
Else
Leap = 0;
If (LEAP = 1 & month> 2)/* if it is a leap year and the month is greater than 2, add one day to the total number of days */
Sum ++;
Printf ("it is the % DTH day.", sum );}
========================================================== ======================================
[Procedure 5]
Question: input three integers x, y, and 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 with y first. If x> Y, the values of X and Y are exchanged,
Then compare X with Z. If x> Z, the value of X is exchanged with Z to minimize X.
2. program source code:
Main ()
{
Int X, Y, Z, T;
Scanf ("% d", & X, & Y, & Z );
If (x> Y)
{T = x; X = y; y = T;}/* swap the values of x and y */
If (x> Z)
{T = z; Z = x; X = T;}/* swap the value of X and Z */
If (Y> Z)
{T = y; y = z; Z = T;}/* swap Z, Y value */
Printf ("small to big: % d/N", x, y, z );
}
========================================================== ======================================
[Procedure 6]
Question: Use the letter "*" to output the C "pattern.
1. Program Analysis: You can use the '*' sign to write the letter C on the paper, and then output it by branch.
2. program source code:
# Include "stdio. H"
Main ()
{
Printf ("Hello C-world! /N ");
Printf ("****/N ");
Printf ("*/N ");
Printf ("*/N ");
Printf ("****/N ");
}
========================================================== ======================================
[Procedure 7]
Question: Output A special pattern. Run it in the C environment. Have a look, very beautiful!
1. Program Analysis: There are 256 characters in total. Different Characters and images are different.
2. program source code:
# Include "stdio. H"
Main ()
{
Char A = 176, B = 219;
Printf ("% C/N", B, A, A, B );
Printf ("% C/N", A, B, A, B, );
Printf ("% C/N", A, A, B, A, );
Printf ("% C/N", A, B, A, B, );
Printf ("% C/N", B, A, a, B );}
========================================================== ======================================
[Procedure 8]
Question: 9*9 tips.
1. Program Analysis: For branch and column consideration, there are 9 rows and 9 columns, I control row and J control column.
2. program source code:
# Include "stdio. H"
Main ()
{
Int I, j, result;
Printf ("/N ");
For (I = 1; I <10; I ++)
{For (j = 1; j <10; j ++)
{
Result = I * J;
Printf ("% d * % d = %-3D", I, j, result);/*-3D indicates the left alignment, which occupies three places */
}
Printf ("/N");/* line feed after each line */
}
}
========================================================== ======================================
[Procedure 9]
Question: It is required to output the chess board.
1. Program Analysis: Use I control rows and J to control columns, and control the output black square or white square according to the sum of I + J.
2. program source code:
# Include "stdio. H"
Main ()
{
Int I, J;
For (I = 0; I <8; I ++)
{
For (j = 0; j <8; j ++)
If (I + J) % 2 = 0)
Printf ("% C", 219,219 );
Else
Printf ("");
Printf ("/N ");
}
}
========================================================== ======================================
[Procedure 10]
Question: print the stairs and print two smiling faces above the stairs.
1. Program Analysis: Use I to control rows and J to control columns. j controls the number of black squares output based on I changes.
2. program source code:
# Include "stdio. H"
Main ()
{
Int I, J;
Printf ("/1/1/N");/* output two smiling faces */
For (I = 1; I <11; I ++)
{
For (j = 1; j <= I; j ++)
Printf ("% C", 219,219 );
Printf ("/N ");
}
}


Author: zhlei81 reply to this speech

--------------------------------------------------------------------------------

3. Reply: 100 typical C source code examples
[Procedure 11]
Question: classical question: a rabbit has been born every month since 3rd months after birth.
Next, I gave birth to another rabbit every month. If the Rabbit does not die, I would like to ask what the total number of Rabbits is every month?
1. program analysis: the rabbit rule is a sequence of numbers 1, 1, 2, 3, 5, 8, 13, 21 ....
2. program source code:
Main ()
{
Long F1, F2;
Int I;
F1 = F2 = 1;
For (I = 1; I <= 20; I ++)
{Printf ("% 12ld % 12ld", F1, F2 );
If (I % 2 = 0) printf ("/N");/* control output, four in each line */
F1 = F1 + F2;/* The first two months are assigned to the third month */
F2 = F1 + F2;/* The first two months are assigned to the third month */
}
}
========================================================== ======================================
[Procedure 12]
Question: determine the number of prime numbers between-and output all prime numbers.
1. Program Analysis: Method for Determining prime numbers: Remove 2 to SQRT (This number) with a single number. If the number can be divisible,
It indicates that this number is not a prime number, and vice versa.
2. program source code:
# Include "math. H"
Main ()
{
Int m, I, K, H = 0, leap = 1;
Printf ("/N ");
For (M = 101; m <= 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 ("/nthe total is % d", H );
}
========================================================== ======================================
[Procedure 13]
Question: print out all the "Daffodils". The so-called "Daffodils" refers to a three-digit number.
Itself. For example, 153 is a "Daffodils" because 153 = the power of 1 + the power of 5 + the power of 3.
1. program analysis: The for loop is used to control the number of 100-999. Each number is used to calculate a single digit, ten digits, and hundreds of digits.
2. program source code:
Main ()
{
Int I, j, k, n;
Printf ("'water flower' number is :");
For (n = 100; n <1000; n ++)
{
I = N/100;/* break down hundreds of digits */
J = N/24/60 10;/* break down ten digits */
K = n % 10;/* Split a single location */
If (I * 100 + J * 10 + k = I * I + J * j + K * K)
{
Printf ("%-5d", N );
}
}
Printf ("/N ");
}
========================================================== ======================================
[Procedure 14]
Question: decompose a positive integer into a prime factor. For example, enter 90 and print 90 = 2*3*3*5.

Program Analysis: to break down the prime factor of N, first find a minimum prime number k, and then follow the steps below:
(1) If the prime number is equal to N, it indicates that the process of decomposing the prime factor is over. Print it out.
(2) If n <> K, but N can be divisible by K, the value of K should be printed, and N is divided by the quotient of K, as the new positive integer you n,
Repeat the first step.
(3) If n cannot be divisible by K, k + 1 is used as the value of K and the first step is repeated.

2. program source code:
/* Zheng int is divided yinshu */
Main ()
{
Int N, I;
Printf ("/nplease input a number:/N ");
Scanf ("% d", & N );
Printf ("% d =", N );
For (I = 2; I <= N; I ++)
{
While (n! = I)
{
If (N % I = 0)
{Printf ("% d *", I );
N = N/I;
}
Else
Break;
}
}
Printf ("% d", n );}
========================================================== ======================================
[Procedure 15]
Question: Use nested conditional operators to complete this question: Students with scores> = 90 are represented by a, while students with scores between 60 and 89 are represented by B,
The value below 60 is represented by C.
1. program analysis: (A> B )? A: B is a basic example of conditional operators.
2. program source code:
Main ()
{
Int score;
Char grade;
Printf ("Please input a score/N ");
Scanf ("% d", & score );
Grade = score> = 90? 'A': (score> = 60? 'B': 'C ');
Printf ("% d belongs to % C", score, grade );
}
========================================================== ======================================
[Procedure 16]
Question: enter two positive integers m and n to obtain the maximum common approx. And the minimum public multiple.


Author: zhlei81 reply to this speech

--------------------------------------------------------------------------------

4. Reply: 100 typical C source programs
1. Program Analysis: using rolling division.

2. program source code:
Main ()
{
Int A, B, num1, num2, temp;
Printf ("Please input two numbers:/N ");
Scanf ("% d, % d", & num1, & num2 );
If (num1 {temp = num1;
Num1 = num2;
Num2 = temp;
}
A = num1; B = num2;
While (B! = 0)/* use the rolling division until B is 0 */
{
Temp = A % B;
A = B;
B = temp;
}
Printf ("gongyueshu: % d/N", );
Printf ("gongbeishu: % d/N", num1 * num2/);
}
========================================================== ======================================
[Procedure 17]
Question: enter a line of characters to calculate the numbers of English letters, spaces, numbers, and other characters.
1. Program Analysis: using the while statement, the condition is that the input character is not '/N '.

2. program source code:
# Include "stdio. H"
Main ()
{Char C;
Int letters = 0, Space = 0, digit = 0, others = 0;
Printf ("Please input some characters/N ");
While (C = getchar ())! = '/N ')
{
If (C> = 'A' & C <= 'Z' | C> = 'A' & C <= 'Z ')
Letters ++;
Else if (C = '')
Space ++;
Else if (C> = '0' & C <= '9 ')
Digit ++;
Else
Others ++;
}
Printf ("all in all: Char = % d Space = % D digit = % d others = % d/N", letters,
Space, digit, others );
}
========================================================== ======================================
[Procedure 18]
Question: calculate 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
A total of 5 numbers are added.
1. program analysis: the key is to calculate the value of each item.
2. program source code:
Main ()
{
Int A, N, Count = 1;
Long int Sn = 0, Tn = 0;
Printf ("Please input a and n/n ");
Scanf ("% d, % d", & A, & N );
Printf ("A = % d, n = % d/N", A, N );
While (count <= N)
{
Tn = tn +;
Sn = Sn + tn;
A = A * 10;
++ Count;
}
Printf ("A + AA +... = % LD/N", SN );
}
========================================================== ======================================
[Procedure 19]
Question: If a number is equal to the sum of its factors, it is called the "complete number ". For example, 6 = 1 + 2 + 3. Programming
Find all the completions within 1000.
1. Program Analysis: see Program <-- program on the page 14.
2. program source code:
Main ()
{
Static int K [10];
Int I, j, N, S;
For (j = 2; j <1000; j ++)
{
N =-1;
S = J;
For (I = 1; I {
If (J % I) = 0)
{N ++;
S = s-I;
K [N] = I;
}
}
If (S = 0)
{
Printf ("% d is a wanshu", J );
For (I = 0; I printf ("% d,", K [I]);
Printf ("% d/N", K [N]);
}
}
}
========================================================== ======================================
[Procedure 20]
Question: A ball falls freely from the height of 100 meters. After each landing, the ball jumps back to half of the original height. If it falls, please
How many meters are there during the first landing? How high is the rebound of 10th times?
1. Program Analysis: see the notes below
2. program source code:
Main ()
{
Float Sn = 100.0, HN = Sn/2;
Int N;
For (n = 2; n <= 10; n ++)
{
Sn = Sn + 2 * HN;/* Number of meters that pass through the nth landing */
HN = hn/2;/* the nth anti-Skip height */
}
Printf ("the total of road is % F/N", SN );
Printf ("the tenth is % F meter/N", HN );
}

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.