例4-12(1)
/*源程序:exp4-12(1).cpp*/
#include<stdio.h>
#include<stdlib.h>
void main()
{
int r;
int g;
r=rand();
printf("Input a number:");
scanf("%d",&g);
if(g==r)
printf("Right!\n");
}
运行结果
---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
例4-12(2)
/*源程序:exo4_12(2).cpp*/
#include<stdio.h>
#include<stdlib.h>
void main()
{
int r;
int g;
r=rand();
printf("Input a number:");
scanf("%d",&g);
if(g==r)
printf("Right!\n");
else
printf("The number is:%d,You are wrong!\n",r);
}
运行结果
---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
例5-2
/*源程序:exp5_2.cpp*/
#include<stdio.h>
int main()
{ int i=1;
int sum=0;
while(i<=100)
{ sum=sum+i;
i++;
}
printf("sum=%d\n",sum);
}
运行结果
---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
例5-5
/*源程序:exp5_5.cpp*/
#include<stdio.h>
void main()
{ int i;
int sum=0;
for(i=1;i<=100;i++)
{
sum=sum+i*(i+1);
}
printf("1*2+2*3+3*4+...+100*101=%d\n",sum);
}
运行结果