C++循环控制结构练习题.docVIP

  • 23
  • 0
  • 约1.62万字
  • 约 24页
  • 2019-04-01 发布于浙江
  • 举报
循环控制结构练习题 从键盘上输入一个正整数n,计算其阶乘n! #include iostream #include cmath using namespace std; int f(int n) { int t = 1; for (int i = n; i 0; i--) t = t*i; return t;(0的阶层为1) } int main() { int n; cout n=; cin n; cout endl; if (n 0)n = abs(n); cout n!=f(n); system(pause); return 0; } 从键盘上输入一个正整数n,判断是否是素数? 法1: #includeiostream #include iomanip using namespace std; void f(int n) { int k = sqrt(n),i; for ( i = 2; i = k; i++) if (n%i == 0)break; if (i = k+1)cout n 是素数 endl; else cout n 不是素数 endl; } int main() { int n; cout n=; cin n; f(n); system(pause); return

文档评论(0)

1亿VIP精品文档

相关文档