求1000以内所有偶数的和
# include <iostream>
using namespace std;
int main()
{
int i, sum = 0;
i = 2;
do
{
if(i % 2 == 0)
{
sum += i;
}
i++;
}while(i <= 1000);
cout << "sum = " << sum << endl;
return 0;
}
# include <iostream>
using namespace std;
int main()
{
int i, sum = 0;
for(i = 2; i <= 1000; i++)
{
if(i % 2 == 0)
{
sum = sum + i;
}
}
cout << "sum = " << sum << endl;
return 0;
}
# include <iostream>
using namespace std;
int main()
{
int i, sum = 0;
i = 2;
while(i <= 1000)
{
if(i % 2 == 0)
{
sum = sum + i;
}
i++;
}
cout << "sum = " << sum << endl;
return 0;
}
编程序,输出1/3-3/5+5/7-7/9…+19/21的结果
#include<iostream>
#include <cmath>
using namespace std;
int main()
{
double sum=0,i=1,j=3,k=1;
while (i<=19 && j<=21) {
sum=sum+i/j*k;
i+=2;
j+=2;
k=-k;
}
cout<<"1/3-3/5+5/7-7/9…+19/21的结果为:"<<sum<<endl;
return 0;
}
乘法口诀表
# include <iostream>
# include <iomanip>
using namespace std;
int main()
{
int i, k, j = 1;
for(k = 1; k < 10; k++)
{
for(i = 1; i <= k; i++)
{
cout << i << "x" << k << "=" << setw(2) << setiosflags(ios::left) << i*k << " ";
}
j++;
cout << endl;
}
return 0;
}