循环15~17

来源:互联网 发布:淘宝店铺如何快速升钻 编辑:程序博客网 时间:2024/06/05 23:55

循环15

#include<iostream>
bool just(long n);
int main()
{
 using namespace std;
 long a, b; int count = 0; long sum = 0;
 cout << "Enter 2 integer between 1 and 500 :";
 cin >> a >> b;
 for (int i = a; i < b + 1; i++)
 {
  if (just(i))
  {
   count++;
   sum += i;
  }
 }
 cout << count << ' ' << sum << endl;
 return 0;
}
bool just(long n)
{
 if (n == 2)
  return true;
 else if (n % 2 == 0)
  return false;
 int mark = 0;
 for (int i = 3; i < n / 2 + 1; i += 2)
 {
  if (n % i == 0)
   mark = 1;
 }
 if (mark == 0)
  return true;
 else
  return false;
}

 

循环16

#include<iostream>
int main()
{
 using namespace std;
 int n; int sum = 1;
 cout << "Enter eat day ,between 2 to 10:"; cin >> n;
 for (int i = 2; i < n + 1; i++)
 {
  sum = 2 * (sum + 1);
 }
 cout << sum << endl;
 return 0;
}

 

循环17

#include<iostream>
int main()
{
 using namespace std;
 int one; char ch; int two;
 cout << "Enter your quation :";
 cin >> one;
 while (cin >> ch &&ch!='=')
 {
  cin >> two;
  switch (ch)
  {
  case'+':one = one + two; break;
  case'-':one = one - two; break;
  case'*':one = one*two; break;
  case'/':
  {if (two == 0)
   break;
  else
   one = one / two; break; }
  default:
   cout << "Error .\n";
  }
 }
 cout << one << endl;
 return 0;
}

0 0
原创粉丝点击