大学C语言编程试题,大学经典C语言编程试题.doc

1.输入两个正整数,m和n,求其最大公约数和最小公倍数。

#include

void main()

{

int hcf(int,int); /*函数声明*/

int lcd(int,int,int); /*函数声明*/

int u,v,h,l;

printf("Please input two numbers:\n");

scanf("%d,%d",&u,&v);

h=hcf(u,v);

printf("H.C.F=%d\n",h);

l=lcd(u,v,h);

printf("L.C.D=%d\n",l);

}

int hcf(int u,int v)

{

int t,r;

if(v>u)

{t=u;u=v;v=t;}

while((r=u%v)!=0)

{u=v;v=r;}

return(v);

}

int lcd(int u,int v,int h)

{

return(u*v/h);

}

2.输入一行字符,分别统计出其中字母、空格、数字和其他字符的个数。

#include

int letter,digit,space,others;

void main()

{

void count(char[]);

char text[80];

printf("Please input string:\n");

gets(text);

printf("string:\n");

puts(text);

letter=0;

digit=0;

space=0;

others=0;

count(text);

printf("letter:%d,digit:%d,space:%d,others:%d\n",letter,digit,space,others);

}

void count(char str[])

{

int i;

for(i=0;str[i]!='\0';i++)

if((str[i]>='a'&&str[i]<='z')||(str[i]>='A'&&str[i]<='Z'))

letter++;

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

digit++;

else if(str[i]==32)

space++;

else

others++;

}

3.输入一个正整数求出它是几位数;输出原数和位数。

#include

int digit;

void main()

{

void count(char[]);

char text[80];

printf("Please input numbers:\n");

gets(text);

printf("Numbers:\n");

puts(text);

digit=0;

count(text);

printf("digit:%d\n",digit);

}

void count(char str[])

{

int i;

for(i=0;str[i]!='\0';i++)

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

digit++;

}

4.输入一个正整数,输出原数并逆序打印出各位数字。

#include

void invertLongInt(long);

void main()

{

unsigned long iNumber;

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

scanf("%ld",&iNumber);

printf("The input number is:%ld\n",iNumber);

printf("The inverse number is:");

invertLongInt(iNumber);

}

void invertLongInt(long x)

{

if(x>=0&&x<=9)

printf("%d\n",x);

else

{

printf("%d",x%10);

invertLongInt(x/10);

}

}

5.从键盘上输入若干学生的一门课成绩,统计并输出最高成绩和最低成绩及相应的序号,当输入负数时结束输入。

6.从键盘上输入若干学生的一门课成绩,计算出平均分,当输入负数时结束输入。将结果输出。

7.求1!+2!+3!+……+20!,将结果输出。

#include

void main()

{

float s=0,t=1;

int n;

for(n=1;n<=20;n++)

{

t=t*n;

s=s+t;

}

  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值