10 Classic C Language Programs

1. Problem: How many distinct three-digit numbers can be formed using the digits 1, 2, 3, and 4 without repetition? What are they?

Program Analysis: The digits that can fill the hundreds, tens, and units places are 1, 2, 3, and 4. Generate all permutations and then remove those that do not meet the criteria.

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 i, j, k are distinct */ printf("%d,%d,%d\n",i,j,k);

}

}

2. Problem: The bonuses distributed by the company are based on profits. If the profit (I) is less than or equal to 100,000, the bonus is 10%; if the profit is between 100,000 and 200,000, the portion below 100,000 is calculated at 10%, and the portion above 100,000 is calculated at 7.5%; for profits between 200,000 and 400,000, the portion above 200,000 is calculated at 5%; for profits between 400,000 and 600,000, the portion above 400,000 is calculated at 3%; for profits between 600,000 and 1,000,000, the portion above 600,000 is calculated at 1.5%; and for profits above 1,000,000, the portion exceeding 1,000,000 is calculated at 1%. Input the profit for the month from the keyboard and calculate the total bonus to be distributed.

Program Analysis: Use a number line to delineate and locate. Note that the bonus should be defined as a long integer.

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.075;

bonus4=bonus2+200000*0.05;

bonus6=bonus4+200000*0.03;

bonus10=bonus6+400000*0.015;

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

}

10 Classic C Language Programs

3. Problem: An integer, when added to 100, becomes a perfect square, and when 168 is added, it also becomes a perfect square. What is this number?

Program Analysis: Check within 100,000. First, add 100 to the number and take the square root, then add 268 to the number and take the square root again. If the results satisfy the following conditions, then it is the result.

Program Source Code:

#include "math.h" 

main

{

long int i,x,y,z;

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

{ x=sqrt(i+100); /* x is the result after adding 100 and taking the square root */

y=sqrt(i+268); /* y is the result after adding 168 and taking the square root */

if(x*x==i+100&&y*y==i+268)/* If the square of the square root equals the number, it indicates that this number is a perfect square */

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

}

}

4. Problem: Input a specific year, month, and day, and determine which day of the year it is.

Program Analysis: For example, for March 5, first sum the days of the previous two months, then add 5 days to find out which day of the year it is. In special cases, if it is a leap year and the input month is greater than 3, an additional day should be considered.

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)/* First calculate the total days of the months before the given 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;

case 10:sum=273;break;

case 11:sum=304;break;

case 12:sum=334;break;

default: printf("data error"); break;

}

sum=sum+day; /* Add the days of the specific day */

if(year%400==0||(year%4==0&&year%100!=0))/* Check if 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, the total days should be increased by one */

sum++;

printf("It is the %dth day.",sum);

10 Classic C Language Programs

5. Problem: Input three integers x, y, z, and output these three numbers in ascending order.

Program Analysis: We will find a way to place the smallest number in x. First, compare x with y; if x>y, swap the values of x and y. Then compare x with z; if x>z, swap the values of x and z. This way, x will be the smallest.

Program Source Code:

main 

{

int x,y,z,t; scanf("%d%d%d",&x,&y,&z);

if (x>y) /* Swap the values of x and y */

if(x>z) /* Swap the values of x and z */

if(y>z) /* Swap the values of z and y */

printf("small to big: %d %d %d\n",x,y,z);

}

6. Problem: Output the letter C pattern using asterisks.

Program Analysis: First, sketch the letter C on paper using <|>*<|> symbols, then output it line by line.

Program Source Code:

#include "stdio.h" 

main {

printf("Hello C-world!\n");

printf(" ****\n");

printf(" *\n");

printf(" * \n");

printf(" ****\n");

}

10 Classic C Language Programs

7. Problem: Output a special pattern; please run it in the C environment to see how beautiful it is!

Program Analysis: There are 256 characters. Different characters will produce different patterns.

Program Source Code:

#include "stdio.h" 

main

{

char a=176,b=219;

printf("%c%c%c%c%c\n",b,a,a,a,b);

printf("%c%c%c%c%c\n",a,b,a,b,a);

printf("%c%c%c%c%c\n",a,a,b,a,a);

printf("%c%c%c%c%c\n",a,b,a,b,a);

printf("%c%c%c%c%c\n",b,a,a,a,b);

}

8. Problem: Output the multiplication table (9*9).

Program Analysis: Consider rows and columns, with a total of 9 rows and 9 columns, where i controls the rows and j controls the columns.

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 means left-aligned, occupying 3 spaces*/ }

printf("\n");/* New line after each row */

}

}

10 Classic C Language Programs

9. Problem: Output a chessboard pattern.

Program Analysis: Use i to control rows and j to control columns, and determine whether to output a black or white square based on the sum of i and j.

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%c",219,219);

else

printf(" ");

printf("\n");

}

}

10. Problem: Print stairs, while printing two smiley faces above the stairs.

Program Analysis: Use i to control rows and j to control columns, where j varies based on i to control the number of black squares output.

Program Source Code:

#include "stdio.h" 

main

{

int i,j;

printf("\n");/* Output two smiley faces */

for(i=1;i<11;i++)

{

for(j=1;j<=i;j++)

printf("%c%c",219,219);

printf("\n");

}

}

Source: C Language Programming10 Classic C Language ProgramsClick to read the original text to view historical articles

Leave a Comment