资源描述
练习
1.求1~100的偶数的和;
#include <iostream>
using namespace std;
int main()
{
int i=1,sum=0;
while(i<=100)
{
if(i%2==0)sum+=i;
i++;
}
cout<<sum<<endl;
return 0;
}
2、从键盘连续输入字符,直到输入“回车”符为止, 统计输入的字符中’A’个数。
#include<iostream>
using namespace std;
int main()
{
int count=0;
char ch;
cout<<"Please a sentence,then press enter:";
ch=getchar();
while(ch!='\n')
{
if(ch=='A')count++;
ch=getchar();
}
cout<<"count="<<count<<endl;
return 0;
}
4.p86 第6题
#include <iostream>
using namespace std;
int main ()
{char c;
int letters=0,space=0,digit=0,other=0;
cout<<"enter one line::"<<endl;
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
other++;
}
cout<<"letter:"<<letters<<", space:"<<space<<", digit:"<<digit<<",
other:"<<other<<endl;
return 0;
}
3. 输出九九乘法表
1*1=1
1*2=2 2*2=4
1*3=3 2*3=6 3*3=9
……
1*9=9 2*9=18 3*9=27 …… 9*9=81
#define N 9
#include <iostream>
using namespace std;
int main( )
{ int i,j;
for(i=1;i<=N;i++)
{
for(j=1;j<=i;j++ )
{
cout<<i<<"*"<<j<<"=" <<i*j<<" ";
}
cout<<'\n';
}
return 0;
}
4. 输出以下图形
当N=4时,输出
*
***
*****
*******
#include<iostream>
using namespace std;
int main()
{
int i,j;
for(i=1;i<=4;i++)
{
for(j=1;j<=8-2*i; j++)
cout<<' ';
for(j=1;j<=2*i-1;j++)
cout<<'*';
cout<<endl;
}
return 0;
}
展开阅读全文