C + + brush problem--2830: Recursive seeking 1*1+2*2+3*3+......+n*n

Source: Internet
Author: User
Description defines a recursive function sumint sum (int n);    function declaration, return 12+22+32+......+n2 and note: Sum is the value of the recursive function input positive integer n output12+22+32+......+n2 and sample Input5sample Output55

/* Copyright (c) 2014, Yantai University School of Computer * All rights reserved. * File name: Test.cpp * Chen Dani * Completion Date: June 1, 2015 * version number: v1.0 * * #include <iostream> #include <cmath>using namespace s Td;int sum (int n); function declaration, 12+22+32+......+n2 and int main () {   int n,s;   cin>>n;   s= sum (n);  function call   cout<<s<<endl;   return 0;} int sum (int n) {    int i,m,w=0;    for (i=1;i<=n;i++)    {        m=i*i;        w=w+m;    }    Return w;}

Experience: This is a simple recursive function, if encountered complex recursive function, before writing can find the law, written in the form of recursion, it is better. Keep trying!

C + + brush problem--2830: Recursive seeking 1*1+2*2+3*3+......+n*n

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.