例3-7
题目描述:输入两个整数,求他们的平方和。
设计思路:
1.设计一个函数用于求一个数的平方。
2.输入两个整数分别求出平方和。
3.将他们的平方和相加。
流程图:
代码实现:
#include<iostream> #include<cmath> using namespace std; int fun(int a) { return pow(a,2); } int main() { int x, y; int s; cin >> x >> y; s = fun(x) + fun(y); cout << s; return 0; }
例n!
设计思路:
1.定义一个s,用于记录n的阶乘。
2.运用递归,求n*(n-1).,当n=0时结束。
流程图:
#include<iostream> using namespace std; int main() { int s=1; int n; cin >> n; while (n - 1 != 0) { s = s * n; n = n - 1; } cout << s; }