Given an integer n, find the sum of all integers from 1 to n, and find the C language code for the sum of all odd numbers from 1 to n

Code to find the sum of all integers from 1 to n: int sum(int n) { int result = 0; for (int i=1; i<=n; i++) { result += i; } return result; } Find 1 Code for the sum of all odd numbers up to n: int sumOdd(int n) { int result = 0; for (int i=1; i<=n; i+=2) { result += i; } return result; }

Guess you like

Origin blog.csdn.net/weixin_35756624/article/details/129554425