//task: to write a program which in the interval from n to m would find
//odd numbers product and even numbers amount.
#include <iostream>
using namespace std;
int main()
{
int n, m; //numbers
int k; //loop's variable
int r; //product
int s=0; //sum
cout<<"Write two numbers - "<<endl;
cout<<"The first number: "<<endl;
cin>>n;
cout<<"The second number: "<<endl;
cin>>m;
if (n>m)
swap(n,m);
r=1;
for (k=n; k<=m; k+=1)
{
if (k%2==0) //even
s+=k;
else // odd
r*=k;
}
cout <<"s = "<<s<<endl;
cout<<"r = "<<r<<endl;
}
return 0;
}
此代码现在有效!
任务是:假设 n =1, m=10 偶数将是 2,4,6,8,10,其数量将是 =30。奇数是 1,3,5,7,9,它的乘积是 = 945。所以我需要在屏幕上显示数量(30)和乘积(945)。