1.输入两个正整数,m和n,求其最大公约数和最小公倍数。
#include<stdio.h>
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);
}
#include<stdio.h>
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<stdio.h>
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<stdio.h>
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.求1!+2!+3!+……+20!,将结果输出。
#include<stdio.h>
void main()
{
float s=0,t=1;
int n;
for(n=1;n<=20;n++)
{
t=t*n;
s=s+t;
}
printf("1!+2!+3!+……+20!=%e\n",s);
}
6.求下列试子的值:1-1/2+1/3-1/4+……+1/99-1/100,将结果输出。
#include<stdio.h>
void main()
{
float sum=1.0,t,s=1;
int i;
for(i=1;i<=100;i++)
{
t=s/i;
sum=sum+t;
s=-s;
}
printf("1-1/2+1/3-1/4+……+1/99-1/100=%5.4f\n",sum);
}
7.打印出100~999之间的所有水仙花数。
#include<