欢迎来到精华作文网!
【www.jinghuajt.com--写作基础】
c语言是一门计算机基础语言,很多大学的编程课程都会从C语言开始,这不是没有它的道理。下面是百分网小编为大家整理的关于c语言入门基础知识,希望大家喜欢!
c语言入门基础知识
1、C语言打印一条语句源代码:
/* C Program to print a sentence. */
#include
int main()
{
printf("C Programming"); /* printf() prints the content inside quotation */
return 0;
}
输出:
C Programming
2、C语言打印用户输入的一个整数#include
int main()
{
int num;
printf("Enter a integer: ");
scanf("%d",&num); /* Storing a integer entered by user in variable num */
printf("You entered: %d",num);
return 0;
}
输出:
Enter a integer: 25You entered: 25
3、C语言实现两个整数相加/*C programming source code to add and display the sum of two integers entered by user */
#include
int main( )
{
int num1, num2, sum;
printf("Enter two integers: ");
scanf("%d %d",&num1,&num2); /* Stores the two integer entered by user in variable num1 and num2 */
sum=num1+num2; /* Performs addition and stores it in variable sum */
printf("Sum: %d",sum); /* Displays sum */
return 0;
}
输出:
Enter two integers: 1211Sum: 23
4、C语言实现两个小数相乘/*C program to multiply and display the product of two floating point numbers entered by user. */
#include
int main( )
{
float num1, num2, product;
printf("Enter two numbers: ");
scanf("%f %f",&num1,&num2); /* Stores the two floating point numbers entered by user in variable num1 and num2 respectively */
product = num1*num2; /* Performs multiplication and stores it */
printf("Product: %f",product);
return 0;
}
输出:
Enter two numbers: 2.41.1Product: 2.640000
5、C语言查找字符的ASCII值/* Source code to find ASCII value of a character entered by user */
#include
int main(){
char c;
printf("Enter a character: ");
scanf("%c",&c); /* Takes a character from user */
printf("ASCII value of %c = %d",c,c);
return 0;
}
输出:
Enter a character: GASCII value of G = 71
6、C语言根据用户输入的整数做商和余数/* C Program to compute remainder and quotient */
#include
int main(){
int dividend, divisor, quotient, remainder;
printf("Enter dividend: ");
scanf("%d",÷nd);
printf("Enter divisor: ");
scanf("%d",&divisor);
quotient=dividend/divisor; /* Computes quotient */
remainder=dividend%divisor; /* Computes remainder */
printf("Quotient = %d\n",quotient);
printf("Remainder = %d",remainder);
return 0;
}
输出:
Enter dividend: 25Enter divisor: 4Quotient = 6Remainder = 1
7、C语言获取整型、单精度浮点型、双精度浮点型和字符型的长度基本语法:
/* This program computes the size of variable using sizeof operator.*/
#include
int main(){
int a;
float b;
double c;
char d;
printf("Size of int: %d bytes\n",sizeof(a));
printf("Size of float: %d bytes\n",sizeof(b));
printf("Size of double: %d bytes\n",sizeof(c));
printf("Size of char: %d byte\n",sizeof(d));
return 0;
}
输出:
Size of int: 4 bytesSize of float: 4 bytesSize of double: 8 bytesSize of char: 1 byte
8、C语言获取关键字long的长度范围#include
int main(){
int a;
long int b; /* int is optional. */
long long int c; /* int is optional. */
printf("Size of int = %d bytes\n",sizeof(a));
printf("Size of long int = %ld bytes\n",sizeof(b));
printf("Size of long long int = %ld bytes",sizeof(c));
return 0;
}
输出:
Size of int = 4 bytesSize of long int = 4 bytesSize of long long int = 8 bytes
9、C语言交换数值#include
int main(){
float a, b, temp;
printf("Enter value of a: ");
scanf("%f",&a);
printf("Enter value of b: ");
scanf("%f",&b);
temp = a; /* Value of a is stored in variable temp */
a = b; /* Value of b is stored in variable a */
b = temp; /* Value of temp(which contains initial value of a) is stored in variable b*/
printf("\nAfter swapping, value of a = %.2f\n", a);
printf("After swapping, value of b = %.2f", b);
return 0;
}
输出:
Enter value of a: 1.20Enter value of b: 2.45After swapping, value of a = 2.45After swapping, value of b = 1.2
10、C语言检查数值是奇数还是偶数/*C program to check whether a number entered by user is even or odd. */
#include
int main(){
int num;
printf("Enter an integer you want to check: ");
scanf("%d",&num);
if((num%2)==0) /* Checking whether remainder is 0 or not. */
printf("%d is even.",num);
else
printf("%d is odd.",num);
return 0;
}
输出1:
Enter an integer you want to check: 2525 is odd.
输出2:
Enter an integer you want to check: 1212 is even.
也可以用条件运算符解决:
/* C program to check whether an integer is odd or even using conditional operator */
#include
int main(){
int num;
printf("Enter an integer you want to check: ");
scanf("%d",&num);
((num%2)==0) ? printf("%d is even.",num) : printf("%d is odd.",num);
return 0;
}
11、C语言检查是元音还是辅音#include
int main(){
char c;
printf("Enter an alphabet: ");
scanf("%c",&c);
if(c=="a"||c=="A"||c=="e"||c=="E"||c=="i"||c=="I"||c=="o"||c=="O"||c=="u"||c=="U")
printf("%c is a vowel.",c);
else
printf("%c is a consonant.",c);
return 0;
}
输出1:
Enter an alphabet: ii is a vowel.
输出2:
Enter an alphabet: GG is a consonant.
也可以用条件运算符解决
/* C program to check whether a character is vowel or consonant using conditional operator */
#include
int main(){
char c;
printf("Enter an alphabet: ");
scanf("%c",&c);
(c=="a"||c=="A"||c=="e"||c=="E"||c=="i"||c=="I"||c=="o"||c=="O"||c=="u"||c=="U") ? printf("%c is a vowel.",c) : printf("%c is a consonant.",c);
return 0;
}
输出结果和上面的程序相同。
12、C语言实现从三个数值中查找最大值实现1:
/* C program to find largest number using if statement only */
#include
int main(){
float a, b, c;
printf("Enter three numbers: ");
scanf("%f %f %f", &a, &b, &c);
if(a>=b && a>=c)
printf("Largest number = %.2f", a);
if(b>=a && b>=c)
printf("Largest number = %.2f", b);
if(c>=a && c>=b)
printf("Largest number = %.2f", c);
return 0;
}
实现2:
/* C program to find largest number using if...else statement */
#include
int main(){
float a, b, c;
printf("Enter three numbers: ");
scanf("%f %f %f", &a, &b, &c);
if (a>=b)
{
if(a>=c)
printf("Largest number = %.2f",a);
else
printf("Largest number = %.2f",c);
}
else
{
if(b>=c)
printf("Largest number = %.2f",b);
else
printf("Largest number = %.2f",c);
}
return 0;
}
实现3:
/* C Program to find largest number using nested if...else statement */
#include
int main(){
float a, b, c;
printf("Enter three numbers: ");
scanf("%f %f %f", &a, &b, &c);
if(a>=b && a>=c)
printf("Largest number = %.2f", a);
else if(b>=a && b>=c)
printf("Largest number = %.2f", b);
else
printf("Largest number = %.2f", c);
return 0;
}
输出结果相同:
Enter three numbers: 12.213.45210.193Largest number = 13.45
13、C语言解一元二次方程/* Program to find roots of a quadratic equation when coefficients are entered by user. */
/* Library function sqrt() computes the square root. */
#include
#include /* This is needed to use sqrt() function.*/
int main()
{
float a, b, c, determinant, r1,r2, real, imag;
printf("Enter coefficients a, b and c: ");
scanf("%f%f%f",&a,&b,&c);
determinant=b*b-4*a*c;
if (determinant>0)
{
r1= (-b+sqrt(determinant))/(2*a);
r2= (-b-sqrt(determinant))/(2*a);
printf("Roots are: %.2f and %.2f",r1 , r2);
}
else if (determinant==0)
{
r1 = r2 = -b/(2*a);
printf("Roots are: %.2f and %.2f", r1, r2);
}
else
{
real= -b/(2*a);
imag = sqrt(-determinant)/(2*a);
printf("Roots are: %.2f+%.2fi and %.2f-%.2fi", real, imag, real, imag);
}
return 0;
输出1:
Enter coefficients a, b and c: 2.345.6Roots are: -0.87+1.30i and -0.87-1.30i
输出2:
Enter coefficients a, b and c: 410Roots are: 0.00 and -0.25
14、C语言检查是否是闰年/* C program to check whether a year is leap year or not using if else statement.*/
#include
int main(){
int year;
printf("Enter a year: ");
scanf("%d",&year);
if(year%4 == 0)
{
if( year%100 == 0) /* Checking for a century year */
{
if ( year%400 == 0)
printf("%d is a leap year.", year);
else
printf("%d is not a leap year.", year);
}
else
printf("%d is a leap year.", year );
}
else
printf("%d is not a leap year.", year);
return 0;
}
输出1:
Enter year: 20002000 is a leap year.
输出2:
Enter year: 19001900 is not a leap year.
输出3:
Enter year: 20122012 is a leap year.
C语言基础知识总结
1. 每个C程序有且只有一个主函数main(),且程序必须从main()函数开始执行,并在main()函数中结束。
2.在C语言中,用e来表示科学计数法时,规定在e的前面必须有数字,后面必须为整数。
3.用单引号括起来的一个字符常量只能存放一个字符;C语言中没有字符串变量,只能用字符数组来存储字符串。
4.外部变量在编译时由系统分配永久的内存空间,所以外部变量的类型不是自动存储类别。
5.在一个函数内的复合语句中定义的变量,只能在这个复合语句范围内有效。 6.用sizeof(int)计算int类型数据的字节数。
7.C语言运行时,首先系统检查语法的正误,再运行程序的语法;C语言中,可以在一个函数中嵌套一个函数,但是不能在一个函数中定义一个函数;只有在函数外部定义的函数才是外部函数;C语言的子程序有过程和函数两种。
8.预处理命令行的位置没有规定,只是习惯放在前面;在源文件中的可以有多条预处理命令,但一行只能写一条;宏名的大写只是习惯性的表示;宏替换不仅不占用运行时间还不分配内存空间。
9.feof函数的作用是检查文件是否结束,当结束时返回的值为非零,否则为零。
10.当定义了数组后,在给其赋值时,不能直接把字符串赋给数组名
C语言是一种计算机程序设计语言,它既具有高级语言的特点,又具有汇编语言的特点。以下是小编为大家搜索整理的C语言程序设计题,希望能给大家带来帮助!更多精彩内容请及时关注我们应届毕业生考试网!
1: 第1题请编写函数fun,其功能使:将两个两位正整数a b合并形成一个整数放在c中。合并的方式使:将a数的十位和个位数依次放在c数的百位和个位上,b数的十位和个位数依次放在c数的千位和十位上。
答案:
void fun(int a,int b,long *c)
{*c=(b/10)*1000+(a/10)*100+(b%10)*10+a%10;}
2: 第2题请编写一个函数fun,它的功能使:计算n门课程的平均分,计算结果作为函数值返回。
答案:
float fun(float *a,int n)
{float ave=0.0;
int i;
for(i=0;i
ave+=a;
ave/=n;
return ave;}
3: 第3题假定输入的字符串中只包含字母和*号。请编写函数fun,它的功能使:将字符串尾部的*号全部删除,前面和中间的*号不删除。
答案:
void fun(char *a)
{int i=0;
char *p,*q;
p=q=a;
while(*p)
p++;
p--;
while(*p==‘*’)
p--;
while(q<=p)
{a=*q;
i++;
q++;}
a=‘\0’;}
4: 第4题请编写函数fun,其功能是:将两个两位数的正整数a b合并形成一个整数放在c中,合并的方式是:将a数的十位和个位数依次放在c的个位和百位上,b数的十位和个位数依次放在c数的千位和十位上。
答案:
void fun(int a,int b,long *c)
{c=(b/10)*1000+(a%10)*100+(b%10)*10+a/10;}
5: 第5题 n名学生的成绩已在主函数中放入一个带头节点的链表结构中,h指向链表的头节点。请编写函数fun,它的功能是:求出平均分,由函数值返回。
答案:
double fun(strec *h)
{double aver=0.0;
while(h!=null)
{aver+=h->s;
hy=h->next;}
aver/=n;
return aver;}
6: 第6题请编写函数fun,计算并输出给定10个数的方差。
答案:
double fun(double x[10])
{int i,j;
double s=0.0,s1=0.0;
for(i=0;i<10;i++)
s1+=x;
s1/=10;
for(j=0;j<10;j++)
s+=(x[j]-s1)*(x[j]-s1);
s/=10;
s=pow(s,0.5);
return s;}
7: 第7题请编写函数fun,其功能是:将两个两位数的正整数a b合并形成一个整数放在c 中。合并的方式是:将a数的十位和个位数依次放在c数的千位和十位上,b数的十位和个位数依次放在c数的个位和百位上。
答案:
void fun(int a,int b,long *c)
{*c=(a/10)*1000+(a%10)*10+(b%10)*100+b/10;}
本文是百分网小编搜索整理的对C语言程序设计的50个小案例进行了详细的分析介绍,有需要的小伙伴可以参考一下,希望对大家有所帮助!想了解更多相关信息请持续关注我们应届毕业生考试网!
【程序1】
题目:有1、2、3、4个数字,能组成多少个互不相同且无重复数字的三位数?都是多少?
1.程序分析:可填在百位、十位、个位的数字都是1、2、3、4。组成所有的排列后再去
掉不满足条件的排列。
2.程序源代码:
代码如下:
#include "stdio.h"
#include "conio.h"
main()
{
int i,j,k;
printf("\n");
for(i=1;i<5;i++) /*以下为三重循环*/
for(j=1;j<5;j++)
for (k=1;k<5;k++)
{
if (i!=k&&i!=j&&j!=k) /*确保i、j、k三位互不相同*/
printf("%d,%d,%d\n",i,j,k);
}
getch();
}
==============================================================
【程序2】
题目:企业发放的奖金根据利润提成。利润(I)低于或等于10万元时,奖金可提10%;利润高
于10万元,低于20万元时,低于10万元的部分按10%提成,高于10万元的部分,可可提
成7.5%;20万到40万之间时,高于20万元的部分,可提成5%;40万到60万之间时高于
40万元的部分,可提成3%;60万到100万之间时,高于60万元的部分,可提成1.5%,高于
100万元时,超过100万元的部分按1%提成,从键盘输入当月利润I,求应发放奖金总数?
1.程序分析:请利用数轴来分界,定位。注意定义时需把奖金定义成长整型。
2.程序源代码:
代码如下:
#include "stdio.h"
#include "conio.h"
main()
{
long int i;
int bonus1,bonus2,bonus4,bonus6,bonus10,bonus;
scanf("%ld",&i);
bonus1=100000*0. 1;
bonus2=bonus1+100000*0.75;
bonus4=bonus2+200000*0.5;
bonus6=bonus4+200000*0.3;
bonus10=bonus6+400000*0.15;
if(i<=100000)
bonus=i*0.1;
else if(i<=200000)
bonus=bonus1+(i-100000)*0.075;
else if(i<=400000)
bonus=bonus2+(i-200000)*0.05;
else if(i<=600000)
bonus=bonus4+(i-400000)*0.03;
else if(i<=1000000)
bonus=bonus6+(i-600000)*0.015;
else
bonus=bonus10+(i-1000000)*0.01;
printf("bonus=%d",bonus);
getch();
}
==============================================================
【程序3】
题目:一个整数,它加上100后是一个完全平方数,再加上168又是一个完全平方数,请问该数是多少?
1.程序分析:在10万以内判断,先将该数加上100后再开方,再将该数加上268后再开方,如果开方后
的结果满足如下条件,即是结果。请看具体分析:
2.程序源代码:
代码如下:
#include "math.h"
#include "stdio.h"
#include "conio.h"
main()
{
long int i,x,y,z;
for (i=1;i<100000;i++)
{
x=sqrt(i+100); /*x为加上100后开方后的结果*/
y=sqrt(i+268); /*y为再加上168后开方后的结果*/
if(x*x==i+100&&y*y==i+268) /*如果一个数的平方根的平方等于该数,这说明此数是完全平方数*/
printf("\n%ld\n",i);
}
getch();
}
==============================================================
【程序4】
题目:输入某年某月某日,判断这一天是这一年的第几天?
1.程序分析:以3月5日为例,应该先把前两个月的加起来,然后再加上5天即本年的第几天,特殊
情况,闰年且输入月份大于3时需考虑多加一天。
2.程序源代码:
代码如下:
#include "stdio.h"
#include "conio.h"
main()
{
int day,month,year,sum,leap;
printf("\nplease input year,month,day\n");
scanf("%d,%d,%d",&year,&month,&day);
switch(month) /*先计算某月以前月份的总天数*/
{
case 1:sum=0;break;
case 2:sum=31;break;
case 3:sum=59;break;
case 4:sum=90;break;
case 5:sum=120;break;
case 6:sum=151;break;
case 7:sum=181;break;
case 8:sum=212;break;
case 9:sum=243;break;
case 10:sum=273;break;
case 11:sum=304;break;
case 12:sum=334;break;
default:printf("data error");break;
}
sum=sum+day; /*再加上某天的天数*/
if(year%400==0||(year%4==0&&year%100!=0)) /*判断是不是闰年*/
leap=1;
else
leap=0;
if(leap==1&&month>2) /*如果是闰年且月份大于2,总天数应该加一天*/
sum++;
printf("It is the %dth day.",sum);
getch();
}
==============================================================
【程序5】
题目:输入三个整数x,y,z,请把这三个数由小到大输出。
1.程序分析:我们想办法把最小的数放到x上,先将x与y进行比较,如果x>y则将x与y的值进行交换,
然后再用x与z进行比较,如果x>z则将x与z的值进行交换,这样能使x最小。
2.程序源代码:
代码如下:
#include "stdio.h"
#include "conio.h"
main()
{
int x,y,z,t;
scanf("%d%d%d",&x,&y,&z);
if (x>y)
{t=x;x=y;y=t;} /*交换x,y的值*/
if(x>z)
{t=z;z=x;x=t;} /*交换x,z的值*/
if(y>z)
{t=y;y=z;z=t;} /*交换z,y的值*/
printf("small to big: %d %d %d\n",x,y,z);
getch();
}
==============================================================
【程序6】
题目:用*号输出字母C的图案。
1.程序分析:可先用"*"号在纸上写出字母C,再分行输出。
2.程序源代码:
代码如下:
#include "stdio.h"
#include "conio.h"
main()
{
printf("Hello C-world!\n");
printf(" ****\n");
printf(" *\n");
printf(" * \n");
printf(" ****\n");
getch();
}
==============================================================
【程序7】
题目:输出特殊图案,请在c环境中运行,看一看,Very Beautiful!
1.程序分析:字符共有256个。不同字符,图形不一样。
2.程序源代码:
代码如下:
#include "stdio.h"
#include "conio.h"
main()
{
char a=176,b=219;
printf("%c%c%c%c%c\n",b,a,a,a,b);
printf("%c%c%c%c%c\n",a,b,a,b,a);
printf("%c%c%c%c%c\n",a,a,b,a,a);
printf("%c%c%c%c%c\n",a,b,a,b,a);
printf("%c%c%c%c%c\n",b,a,a,a,b);
getch();
}
==============================================================
【程序8】
题目:输出9*9口诀。
1.程序分析:分行与列考虑,共9行9列,i控制行,j控制列。
2.程序源代码:
代码如下:
#include "stdio.h"
#include "conio.h"
main()
{
int i,j,result;
printf("\n");
for (i=1;i<10;i++)
{
for(j=1;j<10;j++)
{
result=i*j;
printf("%d*%d=%-3d",i,j,result); /*-3d表示左对齐,占3位*/
}
printf("\n"); /*每一行后换行*/
}
getch();
}
==============================================================
【程序9】
题目:要求输出国际象棋棋盘。
1.程序分析:用i控制行,j来控制列,根据i+j的和的变化来控制输出黑方格,还是白方格。
2.程序源代码:
代码如下:
#include "stdio.h"
#include "conio.h"
main()
{
int i,j;
for(i=0;i<8;i++)
{
for(j=0;j<8;j++)
if((i+j)%2==0)
printf("%c%c",219,219);
else
printf(" ");
printf("\n");
}
getch();
}
==============================================================
【程序10】
题目:打印楼梯,同时在楼梯上方打印两个笑脸。
1.程序分析:用i控制行,j来控制列,j根据i的变化来控制输出黑方格的个数。
2.程序源代码:
代码如下:
#include "stdio.h"
#include "conio.h"
main()
{
int i,j;
printf("\1\1\n"); /*输出两个笑脸*/
for(i=1;i<11;i++)
{
for(j=1;j<=i;j++)
printf("%c%c",219,219);
printf("\n");
}
getch();
}
【程序11】
题目:古典问题:有一对兔子,从出生后第3个月起每个月都生一对兔子,小兔子长到第三个月
后每个月又生一对兔子,假如兔子都不死,问每个月的兔子总数为多少?
1.程序分析: 兔子的规律为数列1,1,2,3,5,8,13,21....
2.程序源代码:
代码如下:
#include "stdio.h"
#include "conio.h"
main()
{
long f1,f2;
int i;
f1=f2=1;
for(i=1;i<=20;i++)
{
printf("%12ld %12ld",f1,f2);
if(i%2==0) printf("\n"); /*控制输出,每行四个*/
f1=f1+f2; /*前两个月加起来赋值给第三个月*/
f2=f1+f2; /*前两个月加起来赋值给第三个月*/
}
getch();
}
==============================================================
【程序12】
题目:判断101-200之间有多少个素数,并输出所有素数。
1.程序分析:判断素数的方法:用一个数分别去除2到sqrt(这个数),如果能被整除,
则表明此数不是素数,反之是素数。
2.程序源代码:
代码如下:
#include "stdio.h"
#include "conio.h"
#include "math.h"
main()
{
int m,i,k,h=0,leap=1;
printf("\n");
for(m=101;m<=200;m++)
{
k=sqrt(m+1);
for(i=2;i<=k;i++)
if(m%i==0)
{
leap=0;
break;
}
if(leap)
{
printf("%-4d",m);
h++;
if(h%10==0)
printf("\n");
}
leap=1;
}
printf("\nThe total is %d",h);
getch();
}
==============================================================
【程序13】
题目:打印出所有的“水仙花数”,所谓“水仙花数”是指一个三位数,其各位数字立方和等于该数
本身。例如:153是一个“水仙花数”,因为153=1的三次方+5的三次方+3的三次方。
1.程序分析:利用for循环控制100-999个数,每个数分解出个位,十位,百位。
2.程序源代码:
代码如下:
#include "stdio.h"
#include "conio.h"
main()
{
int i,j,k,n;
printf(""water flower"number is:");
for(n=100;n<1000;n++)
{
i=n/100;/*分解出百位*/
j=n/10%10;/*分解出十位*/
k=n%10;/*分解出个位*/
if(i*100+j*10+k==i*i*i+j*j*j+k*k*k)
printf("%-5d",n);
}
getch();
}
==============================================================
【程序14】
题目:将一个正整数分解质因数。例如:输入90,打印出90=2*3*3*5。
程序分析:对n进行分解质因数,应先找到一个最小的质数k,然后按下述步骤完成:
(1)如果这个质数恰等于n,则说明分解质因数的过程已经结束,打印出即可。
(2)如果n<>k,但n能被k整除,则应打印出k的值,并用n除以k的商,作为新的正整数你n,
重复执行第一步。
(3)如果n不能被k整除,则用k+1作为k的值,重复执行第一步。
2.程序源代码:
代码如下:
/* zheng int is pided yinshu*/
#include "stdio.h"
#include "conio.h"
main()
{
int n,i;
printf("\nplease input a number:\n");
scanf("%d",&n);
printf("%d=",n);
for(i=2;i<=n;i++)
while(n!=i)
{
if(n%i==0)
{
printf("%d*",i);
n=n/i;
}
else
break;
}
printf("%d",n);
getch();
}
==============================================================
【程序15】
题目:利用条件运算符的嵌套来完成此题:学习成绩>=90分的同学用A表示,60-89分之间的用B表示,
60分以下的用C表示。
1.程序分析:(a>b)?a:b这是条件运算符的基本例子。
2.程序源代码:
代码如下:
#include "stdio.h"
#include "conio.h"
main()
{
int score;
char grade;
printf("please input a score\n");
scanf("%d",&score);
grade=score>=90?"A":(score>=60?"B":"C");
printf("%d belongs to %c",score,grade);
getch();
}
==============================================================
【程序16】
题目:输入两个正整数m和n,求其最大公约数和最小公倍数。
1.程序分析:利用辗除法。
2.程序源代码:
代码如下:
#include "stdio.h"
#include "conio.h"
main()
{
int a,b,num1,num2,temp;
printf("please input two numbers:\n");
scanf("%d,%d",&num1,&num2);
if(num1<num2)/*交换两个数,使大数放在num1上*/
{
temp=num1;
num1=num2;
num2=temp;
}
a=num1;b=num2;
while(b!=0)/*利用辗除法,直到b为0为止*/
{
temp=a%b;
a=b;
b=temp;
}
printf("gongyueshu:%d\n",a);
printf("gongbeishu:%d\n",num1*num2/a);
getch();
}
==============================================================
【程序17】
题目:输入一行字符,分别统计出其中英文字母、空格、数字和其它字符的个数。
1.程序分析:利用while语句,条件为输入的字符不为"\n".
2.程序源代码:
代码如下:
#include "stdio.h"
#include "conio.h"
main()
{
char c;
int letters=0,space=0,digit=0,others=0;
printf("please input some characters\n");
while((c=getchar())!="\n")
{
if(c>="a"&&c<="z"||c>="A"&&c<="Z")
letters++;
else if(c==" ")
space++;
else if(c>="0"&&c<="9")
digit++;
else
others++;
}
printf("all in all:char=%d space=%d digit=%d others=%d\n",letters,
space,digit,others);
getch();
}