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.
- C程序(4)
- C语言趣味程序(4)
- 单片机时钟程序(4)ds1302.c
- 单片机时钟程序(4)ds1302.c
- C程序小结4
- 程序积累(C)
- C程序(1)
- 聊天程序(C++)
- C程序(2)
- C 程序(3)
- c程序(6)
- C程序经典程序集锦(一)
- 程序优化的方法(C/C++)
- Nginx + FastCGI 程序(C/C++)-1
- Nginx + FastCGI 程序(C/C++)
- C++/C程序的基本概念(0)
- 简单c/s程序(Linux c)
- 三天打鱼,两天晒网程序(C语言/C++)
- java:从消息机制谈到观察者模式
- (19)Java学习笔记——多线程02
- 【JZOJ3885】搞笑的代码
- linux下的ftp访问路径设置
- linux shell编程之利用环境变量IFS自定义分隔符
- C程序(4)
- Spark安装与使用
- 快速幂
- struts 接收参数(通过modelDriver接口)
- 梳理公司的组织架构---组合模式
- 复选框跨页多选实现(js+struts2)
- 【JZOJ3891】【NOIP2014模拟10.25A组】钻石交易
- jsp_03 jsp语法
- C++幼儿园[1] - 项目创建