当前位置:网站首页>C language related programming exercises

C language related programming exercises

2022-07-28 14:48:00 Youth is short!

  1. Program to achieve per 5 All data can be output in a row 3 and 5 to be divisible by ,  But ten people are not 0 Of 3 An integer .

 #include<stdio.h>

void main( )

{

int i,j=0;

  for (i=100;i<=1000;i++)

     if((i%3==0&&i%5==0)&&((i%100)/10!=0&&(i%100)/10!=3))

     {

        printf("%5d",i);

     j++;

     if(j%5==0)

        printf("\n");

     }

}


2. Programming from the keyboard to enter a binary form of string , Convert it to decimal integer output .
 

#include <stdio.h>

#include <math.h>

#include <string.h>

int main()

{

   char s[21];

   long a, i, len, k;

   scanf("%s",s);

   len = strlen(s);

   a = 0; k = 1;

   for(i=len-1; i>=0; i--)

   {

      if(s[i] == '1') a += pow(2.0, len-1-i);

   }

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

   return 0;

}


3. Give a 100 point mark ,  Output grade is required “A”、“B” 、“C” 、“D”、“E”.90 The above points are “A”, 80~89  It is divided into “B”,70~79 It is divided into “C”,60~69 It is divided into “D”,60 It is divided into the following “E”.

#include<stdio.h>

void main()

{

     float score;

     printf("please input a number:\n");

     scanf("%f",&score);

     if(score>=90)

        printf("A\n");

     else if(score>=80)

        printf("B\n");

     else if(score>=70)

        printf("C\n");

     else if(score>=60)

        printf("D\n");

     else if(score<60)

        printf("E\n");

}

4.  seek 1! +2! +3! ....+50!( This programming result is wrong because bytes cannot hold that large number , But there is nothing wrong with the program )

#include<stdio.h>

void main()

{

 int i,j;

 long int t=1,sum=0;

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

 {

 t=1;

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

 t=t*j;

 sum=sum+t;

 }

 printf("1!+2!+3!...+50!=%d",sum);

}


5   Using bubbling method 10 An integer sort .

#include<stdio.h>

void main(  )

 {

   int i,j,t;

   int a[10]={8,9,0,7,6,5,4,3,2,1};

  for(i=0;i<9;i++)

     for(j=0;j<9-i;j++)

      {

       if(a[j]>a[j+1])

         {

          t=a[j];a[j]=a[j+1];a[j+1]=t;

       }

      }  

  for(i=0;i<10;i++)

    printf("%d",a[i]);  

}


6.  seek 500 Prime number within .

#include <stdio.h>

 int main()

{

int a,i;

    for (a=2;a<=500;a++)

    {

        for (i=2;i<=a-1;i++)

        {

            if (a%i==0) 

            {

                break;

            }

        }

         if(i>=a-1)

            printf("%4d",a);

    }

}


7. Programming technology 5  A row of data is output by 1、3、5、7 A three digit number consisting of different and non repeating numbers .

#include <stdio.h>

void main()

{

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

 int i,j,k,n=0;

 for(i=0;i<4;i++)

 {

  for(j=0;j<4;j++)

  {

   for(k=0;k<4;k++)

   {

    if(i!=j&&j!=k&&k!=i)   // The three numbers are not equal to each other

    {

     printf("%3d%d%d",a[i],a[j],a[k]);

          n++;

        if(n%5==0)

               printf("\n");

    }

   }

  }

 }

}

Add :1+3+5+……+99=?

#include<stdio.h>

void main()

 {

    int  i=1,sum=0;

    while(i<100)

       {

         sum=sum+i;

         i=i+2;

        }

     printf(“%d”,sum);

  }

seek 1×2×3×……×10=?

#include<stdio.h>

void main()

{

     int i,k=1;

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

                      k=k*i;

               printf("k=%d",k);

}

8. Count all daffodils ( The so-called daffodils number , It's such a three digit number : This number is equal to the sum of the cubes of its bits . Such as 153=13+53+33 )

#include<stdio.h>

void main()

{

     int i,gw,sw,bw;

        for(i=100;i<1000;i++)

        {

               gw=i%10;

               sw=(i%100)/10;

               bw=i/100;

               if(gw*gw*gw+sw*sw*sw+bw*bw*bw==i)

                      printf("%4d",i);

        }

}

9.c Language seeking 500 Prime number within , And 10 One line output

#include <stdio.h>

 int main()

{

int a,i,m=0;

    for (a=2;a<=500;a++)

    {

        for (i=2;i<=a-1;i++)

        {

            if (a%i==0) 

            {

                break;

            }

        }

                     if(i>=a-1)

                     {

                            printf("%4d",a);

                     m++;

                     if(m%10==0)

              printf("\n");

                     }

    }

}

10. multiplication table , Align triangle left

#include<stdio.h>

       void main()

        {int i,j;

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

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

                    printf(“%d*%d=%2d ”,i,j,i*j);

                printf(“\n”);

              }

       }                 

11. hold 100~200 Can be 7 Divisible number , Output ten numbers as a line , How many such numbers are finally output .

#Include<stdio.h>

main( )

{ int n,j=0;

   for(n=100;n<=200;n++)

{

 if (n%7!=0)

          continue;   

       printf("%6d",n);

       j++;

       if (j%10==0)

          printf("\n");

      }

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

 }

12. Judge a certain number entered m Prime or not . If it's a prime , Output “YES”, If it is not , Output “NO”.

#include<stdio.h>

void main( )

{

       int j,m;

  printf("Enter an integer number: ");

  scanf("%d",&m);

  for (j=2; j<=m-1; j++)

     if (m%j==0) break;

  if (j>=m)

    printf("YES\n");

  else

    printf("NO\n");

}

13. Programming sequence 2~10000 Perfect numbers within .( A factor of a number ( Except for the number itself ) The sum is equal to the number itself .)

for example :6 What is the best factor 1、2、3, Factor and 1+2+3=6

therefore 6 It's a complete number

#include<stdio.h>

void main( )

{

       int i,j,s;

       for(i=2;i<=10000;i++)

       {

                     s=0;

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

                     if(i%j==0)

                            s+=j;

                     if(i==s)

                            printf("%6d\n",s);

       }

}

14. Output 8*8 Black and white chessboard

#include<stdio.h>

void main( )

{

       int i,j;

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

       {

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

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

                            printf("■");

                            else

                            printf("  ");

                            printf("\n");

       }

}

15. Programming , Output the following graphics . 

*******

 *****

  ***

   *

#include<stdio.h>

void main( )

{

int i,j;

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

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

         printf(" ");

       for (j=1;j<=8-(2*i-1);j++)

         printf("*");

       printf("\n");

     }

 }

16. Output ad 1 - 5000 year ( contain ) All leap years , And count the total number of leap years ;

#include<stdio.h>

void main( )

{

int i,j=0;

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

         if((i%4==0&&i%100!=0)||(i%400==0))      

         {

                printf("%5d",i);

         j++;

         }

         printf(" Leap year total :%5d",j);

}

17. Enter a positive integer n (1<n<=10), Input again n It's an integer , In reverse order ( Storage ) Output these numbers .
       Such as : Input n=5; Input again 2,5,8,1,4 Save to array ,
       Then exchange their positions       4,1,8,5,2. Output

#include <stdio.h>

void main( )

{

       int i, n, temp;

       int a[10];

       scanf("%d", &n);

       for(i=0; i<n; i++)

              scanf("%d", &a[i]);

       for(i=0; i<n; i++)

       {

              temp=a[i];

              a[i]=a[n-1-i];

              a[n-1-i]=temp;

       }

       for(i=0; i<n; i++)

              printf("%d ", a[i]);

       printf("\n");

}

18. Program to find a 3x4 The maximum element value of the matrix and its row and column numbers

#include <stdio.h>

void main( )

 {

       int  i,j,row,col,max,a[3][4];

  max=a[0][0];

  row=0 ;

  col=0 ;

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

           for(j=0;j<4;j++)     

       if(a[i][j]>max) 

          {

                 max=a[i][j]; row=i;col=j;

          }

   printf("row=%d,col=%d",row,col);

  }

19. The initial value in a character array “I am happy!” Lowercase characters in become uppercase output .

#include <stdio.h>

  void  main( )

 {

char  c[10]={'i', ' ',’a’…};

    int  i;

    for(i=0;i<10;i++)

        if(c[i]>='a'&&c[i]<='z')c[i]-=32;

    for(i=0;i<10;i++)

        printf(“%c”,a[i]);

     printf(“\n”); 

 }

20. Receive a string from the keyboard , Find the number of characters in the string .

#include <stdio.h>

  void  main( )

 {  char  str[30];

    int  i=0;

    scanf(“%s”,str);

    while (str[i] != ‘\0’) i++;

printf(“the string lenth is %d”,i);

 }

 

原网站

版权声明
本文为[Youth is short!]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/196/202207130924595068.html