C程序(4)

来源:互联网 发布:js offsetwidth 编辑:程序博客网 时间:2024/05/07 01:38


程序一、输入一个数将它的倒序输出来。

#include <stdio.h>

 

int main()

{

    int n,t,m,num;

    t=0,num=0;

    printf("Please input a number (q to quit):\n");

    scanf("%d",&n);

    m=n;

    while(n>0)

    {

        t=n%10;

        n=n/10;

        num=num*10+t;

     }

    printf("%d flashback is %d.\n",m,num);

    return 0;

}

 

程序二、输出乘法口诀表

i*j    1    2    3    4    5    6    7    8    9

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

1|    1    2    3    4    5    6    7    8    9

2|         4    6    8   10   12   14   16   18

3|              9   12   15   18   21   24   27

4|                  16   20   24   28   32   36

5|                       25   30   35   40   45

6|                            36   42   48   54

7|                                 49   56   63

8|                                      64   72

9|                                           81

 

#include <stdio.h>

 

int main()

{

int i,j;

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

{

if(j==0)

{

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

{

if(i==0)

printf("i*j");

else

printf("%3d",i);

}

printf("\n");

printf("------------------------------\n");

}

else

{

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

{

if(i==0)

printf("%2d|",j);

else if(i>=j)

printf("%3d",i*j);

else

printf("   ");

}

printf("\n");

}

}

return 0;

}



 

 

程序三、输出一串字符串,判断其中的数字最多的一段,将其第一个数的位数打印出来,并打印这串数字。

#include <stdio.h>

 

int main()

{

char a[100];

int max_len=0;

int len=0;

int max_loc=0;

int loc=0;

int i=0;

int flag=0;

   

printf("Please input a string with number:\n");

scanf("%s",a);

while(a[i]!='\0')

{

if(a[i]>='0'&&a[i]<='9')

{

if(flag==0)

{

loc=i;

flag=1;

}

len++;

}

else

{

if(len>max_len)

{

max_len=len;

max_loc=loc;

}

if(flag==1)

{

flag=0;

len=0;

}

}

i++;

 

}

if(len>max_len)

{

max_len=len;

max_loc=loc;

}

printf("the max length is %d,and the location is %d.\n",max_len,max_loc+1);

printf("the number is:");

for(i=max_loc;i<=max_loc+max_len-1;i++)

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

printf("\n");

    return 0;

}

执行结果:

Please input a string with number:

3681327hjsv367bnv43243

the max length is 7,and the location is 0.

the number is:3681327

 

程序四、求n的阶乘(利用递归)。

#include <stdio.h>

long long fact (int n);

 

int main()

{

    int n;

printf("Please input a number (q to quit):\n");

while(scanf("%d",&n)==1)

{

if(n<=0)

printf("Please input another number:\n");

else

{

printf("%d factorial is %lld.\n",n,fact(n));

printf("Please input a number (q to quit):\n");

}

}

    return 0;

}

 

long long fact (int n)

{

long long m;

if(n>0)

{

m=n*fact(n-1);

}

else

m=1;

return m;

}

执行结果:

Please input a number (q to quit):

9

9 factorial is 362880.

0 0