Two ways to find the sum of all adjacent odd even numbers within 1--100

Source: Internet
Author: User

Analysis:

The sum of all adjacent odd even numbers within 100

1 2 3 4 5 6 7 8 9 10 11 ... 100
This is the following:
1*2+3*4+5*6+ ...
Method One:
(2*1-1) 2*1 + (2*2-1) 2*2 + (2*3-1) 2*3 concluded: (2n-1) *2n (50 cycles)
Method Two:

1*2+3*4+...+99*100 concludes: N (n+1) (2 paces per jump i+=2)

#include <stdio.h> #include <stdlib.h>//the sum of all adjacent odd even numbers within 100 and void fun1 () {int res = 0;for (int i=1;i<=50;i++) Loop 50 times {res + = (2*i-1) * (2*i);//Inference Mathematical formula printf ("%d---%d\n", I,res);}} Recommend this, easy to understand void fun2 () {int res = 0;for (int i=1;i<100;i+=2)//Loop 50 times 2 level 2 jumps that output is 1 3 5 7 9 equal odd {res + i* (i+1);//Inference mathematical formula 1*2   3*4   5*6  


Two ways to find the sum of all adjacent odd even numbers within 1--100

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.