案例1,两数相乘
代码语言:javascript复制#include <stdio.h>
main()
{
int x,y,m;
printf("Please input x and yn");
scanf("%d%d",&x,&y);
m=x*y;
printf("%d * %d = %dn",x,y,m);
}
输入,x和y,输出m为x和y的乘积。
其中*可以更换为 -/等,int也可以用float。
案例2,比较两数大小,输出max。
代码语言:javascript复制#include <stdio.h>
main()
{
float x,y,c;
printf("Please input x and y:n");
scanf("%f%f",&x,&y);
c=x>y?x:y;
printf("MAX of (%f,%f) is %f",x,y,c);
}
比大小或者是否相同,都可以用类似方法实现。
案例3:
代码语言:javascript复制#include <stdio.h>
main()
{
char ch,nch;
int count;
int k;
printf("Please input a string with a # in the end.n");
scanf("%c",&ch);
while(ch != '#')
{
if(ch >= '0' && ch <= '9')
{
count = ch-'0' 1;
scanf("%c",&nch);
for(k=0;k<count;k )
printf("%c",nch);
}
else
printf("%c",ch);
printf(" ");
scanf("%c",&ch);
}
printf("#n");
}
字符串输入和输入,结束符为#。
案例4:
代码语言:javascript复制#include <stdio.h>
void main()
{
printf("The bytes of the variables are:n");
printf("int:%d bytesn",sizeof(int));
printf("char:%d byten",sizeof(char));
printf("short:%d bytesn",sizeof(short));
printf("long:%d bytesn",sizeof(long));
printf("float:%d bytesn",sizeof(float));
printf("double:%d bytesn",sizeof(double));
printf("long double:%d bytesn",sizeof(long double));
getchar();
}
显示各种变量所占用的字节数。
32位/64位系统是否有差异?
案例5:
代码语言:javascript复制#include <stdio.h>
main()
{
int a=5,b,c,i=10;
b=a ;
c= b;
printf("a = %d, b = %d, c = %dn",a,b,c);
printf("i,i ,i = %d,%d,%dn",i,i ,i );
printf("%dn", i);
printf("%dn",--i);
printf("%dn",i );
printf("%dn",i--);
printf("%dn",-i );
printf("%dn",-i--);
getchar();
}
所谓 ,--,前前后后的各种效果。
案例6:
代码语言:javascript复制#include <stdio.h>
main()
{
int i,j,n;
long sum=0,temp=0;
printf("Please input a number to n:n");
scanf("%d",&n);
if(n<1)
{
printf("The n must no less than 1!n");
return;
}
for(i=1;i<=n;i )
{
temp=0;
for(j=1;j<=i;j )
temp =j;
sum =temp;
}
printf("The sum of the sequence(%d) is %dn",n,sum);
getchar();
getchar();
}
简单数列求和,还可以用递归方式实现。
怎么做?
猜猜如下两段程序是啥功能?
代码语言:javascript复制#include <stdio.h>
#include <conio.h>
void main(void)
{
int i,j,x,y;
clrscr();
printf("nn multiplication table nn");
x=9;
y=5;
for(i=1;i<=9;i )
{
gotoxy(x,y);
printf("- ",i);
x =3;
}
x=7;
y=6;
for(i=1;i<=9;i )
{
gotoxy(x,y);
printf("- ",i);
y ;
}
x=9;
y= 6;
for(i=1;i<=9;i )
{
for(j=1;j<=9;j )
{
gotoxy(x,y);
printf("- ",i*j);
y ;
}
y-=9;
x =3;
}
printf("nn");
}
代码语言:javascript复制#include <stdio.h>
#include <conio.h>
void main()
{
int Password=0,Number=0,price=58,i=0;
clrscr();
printf("n====This is a Number Guess Game!====n");
while( Password != 1234 )
{
if( i >= 3 )
return;
i ;
puts("Please input Password: ");
scanf("%d",&Password);
}
i=0;
while( Number!=price )
{
do{
puts("Please input a number between 1 and 100: ");
scanf("%d",&Number);
printf("Your input number is %dn",Number);
}while( !(Number>=1 && Number<=100) );
if( Number >= 90 )
{
printf("Too Bigger! Press any key to try again!n");
}
else if( Number >= 70 && Number < 90 )
{
printf("Bigger!n");
}
else if( Number >= 1 && Number <= 30 )
{
printf("Too Small! Press any key to try again!n");
}
else if( Number > 30 && Number <= 50 )
{
printf("Small! Press any key to try again!n");
}
else
{
if( Number == price )
{
printf("OK! You are right! Bye Bye!n");
}
else if( Number < price )
{
printf("Sorry,Only a little smaller! Press any key to try again!n");
}
else if( Number > price )
printf(" Sorry, Only a little bigger! Press any key to try again!n");
}
getch();
}
}
是乘法表和猜数字吗??????