10 classic c programs

  • 2020-04-01 21:34:18
  • OfStack

[procedure 1]
Title: there are 1, 2, 3, 4 Numbers, how many different three digits can be formed without repeating Numbers? What are they?
1. Program analysis: the Numbers that can be filled in the hundreds, tens and ones place are 1, 2, 3 and 4. Make up all the permutations before you go
Drop the permutation that doesn't satisfy the condition.
2. Program source code:


main() 
{ 
int i,j,k; 
printf("n"); 
for(i=1;i<5;i++) / * The following is a triple cycle */ 
 for(j=1;j<5;j++)  
  for (k=1;k<5;k++) 
   { 
    if (i!=k&&i!=j&&j!=k)     
    printf("%d,%d,%dn",i,j,k); 
    } 
} 


[procedure 2]
Title: the bonus paid by the enterprise is based on the profit commission. 10% bonus if profit (I) is less than or equal to 100,000 yuan; High profits
At 100,000 yuan, less than 200,000 yuan, less than 100,000 yuan by 10% commission, higher than 100,000 yuan, cocoa
As 7.5%; Between 200,000 yuan and 400,000 yuan, higher than 200,000 yuan, can be 5% commission; Between 400,000 and 600,000 is higher
3% commission for the portion of 400,000 yuan; Between 600,000 yuan and 1,000,000 yuan, higher than 600,000 yuan, can be 1.5% commission, higher than
When 1 million yuan, the part that exceeds 1 million yuan presses 1% commission, enter monthly profit I from the keyboard, ask should issue bonus total amount?
1. Program analysis: please use the number line to divide and locate. Note that the bonus is defined 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]
Title: an integer, which is a perfect square number after adding 100, plus 168 is a perfect square number, what is the number?
1. Program analysis: judge within 100,000, add the number to 100 before taking the square root, and then add the number to 268 before taking the square root, if after taking the square root
Is the result that satisfies the following condition. Please 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);    
  y=sqrt(i+268);    
   if(x*x==i+100&&y*y==i+268) 
    printf("n%ldn",i); 
  } 
} 

[procedure 4]
Title: input the date of the year, which day is the date of the year?
1. Program analysis: take March 5 as an example, the first two months should be added up, and then add 5 days that is the day of the year, special
In this case, a leap year with an input month greater than 3 requires an extra day.
2. Program source code:


main() 
{ 
int day,month,year,sum,leap; 
printf("nplease input year,month,dayn"); 
scanf("%d,%d,%d",&year,&month,&day); 
switch(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; 
 defaultrintf("data error");break; 
} 
sum=sum+day;   
 if(year%400==0||(year%4==0&&year%100!=0)) 
  leap=1; 
 else 
  leap=0; 
if(leap==1&&month>2) 
sum++; 
printf("It is the %dth day.",sum); } 

[procedure 5]
Title: input three integers x,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 with y first. Y is going to swap the values of x with the values of y,
And then compare x to z, if x> Z is going to swap the values of x with the values of z, which is going to minimize x.
2. Program source code:


main() 
{ 
int x,y,z,t; 
scanf("%d%d%d",&x,&y,&z); 
if (x>y) 
 
if(x>z) 
 
if(y>z) 
 
printf("small to big: %d %d %dn",x,y,z); 
} 

[procedure 6]
Title: print the letter C with the * sign.
1. Program analysis: can be used first < | > * < | > Write the letter C on a piece of paper and print it in separate lines.
2. Program source code:


#include "stdio.h" 
main() 
{ 
printf("Hello C-world!n"); 
printf(" ****n"); 
printf(" *n"); 
printf(" * n"); 
printf(" ****n"); 
} 

[procedure 7]
Title: output special pattern, please run in c environment, have a look, Very Beautiful!
1. Program analysis: 256 characters in total. Different characters, different graphics.
2. Program source code:


#include "stdio.h" 
main() 
{ 
char a=176,b=219; 
printf("%c%c%c%c%cn",b,a,a,a,b); 
printf("%c%c%c%c%cn",a,b,a,b,a); 
printf("%c%c%c%c%cn",a,a,b,a,a); 
printf("%c%c%c%c%cn",a,b,a,b,a); 
printf("%c%c%c%c%cn",b,a,a,a,b); } 

[procedure 8]
Title: output 9*9 formula.
1. Program analysis: considering the branch and column, 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); 
     } 
   printf("n"); 
   } 
} 

[procedure 9]
Title: to output a chess board.
1. Program analysis: I control the row, j to control the column, according to the change of the sum of I +j to control the output black square or white square.
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%c",219,219); 
   else 
    printf(" "); 
   printf("n"); 
  } 
} 

[procedure 10]
Title: print the staircase and print two smiley faces on top of the staircase at the same time.
1. Program analysis: I control the rows and j control the columns. J controls the number of output black squares according to the change of I.
2. Program source code:


#include "stdio.h" 
main() 
{ 
int i,j; 
printf("n"); 
for(i=1;i<11;i++) 
 { 
 for(j=1;j<=i;j++) 
   printf("%c%c",219,219); 
 printf("n"); 
  } 
} 


Related articles: