Home > Backend Development > C++ > C++ program to calculate the sum of all odd numbers between 1 and N

C++ program to calculate the sum of all odd numbers between 1 and N

王林
Release: 2023-09-06 20:05:04
forward
3100 people have browsed it

C++ program to calculate the sum of all odd numbers between 1 and N

Obtaining the sum of a series is one of the simplest practice tasks when we learn programming and logic construction. In mathematics, there are ways to find the sum of series present in different series. In programming we generate them one by one by implementing logic and add them repeatedly to get the sum or else do anything else as required. In this article, we will introduce the technique of using C to get the sum of all odd numbers up to N.

There are two possible ways to get this sum, with a twist. Let’s look at these methods one by one.

algorithm

  • Use the number N as the upper limit.
  • Initialize the sum to 0.
  • i ranges from 1 to N.
    • If i is an odd number, then.
      • Sum:=sum i.
    • If end.
  • Display the sum.

Example

#include <iostream>
using namespace std;

int solve( int n ) {
   int i;
   int sum = 0;
   cout << "Odd numbers are: ";
   for( i = 1; i <= n; i++ ) {
      if( i % 2 == 1 ) {
         cout << i << ", ";
         sum = sum + i;
      }
   }

   cout << endl;
   return sum;
}
int main(){
   int sum = solve( 25 );
   cout << "Sum is: " << sum;
}
Copy after login

Output

Odd numbers are: 1, 3, 5, 7, 9, 11, 13, 15, 17, 19, 21, 23, 25, 
Sum is: 169
Copy after login

In this method, we check whether each number is odd or even. When it's odd, print the number and add it to the sum variable. But we can ignore this check by incrementing the for loop by 2. The algorithm is as follows -

algorithm

  • Use the number N as the upper limit.
  • Initialize the sum to 0.
  • For i from 1 to N, increase i by 2.
    • Sum:=sum i.
  • Display the sum.

Example

#include <iostream>
using namespace std;

int solve( int n ) {
   int i;
   int sum = 0;
   cout << "Odd numbers are: ";
   for( i = 1; i <= n; i = i + 2 ) {
      cout << i << ", ";
      sum = sum + i;
   }
   cout << endl;
   return sum;
}
int main(){
   int sum = solve( 75 );
   cout << "Sum is: " << sum;
}
Copy after login

Output

Odd numbers are: 1, 3, 5, 7, 9, 11, 13, 15, 17, 19, 21, 23, 25, 27, 29, 31, 33, 35, 37, 39, 41, 43, 45, 47, 49, 51, 53, 55, 57, 59, 61, 63, 65, 67, 69, 71, 73, 75, 
Sum is: 1444
Copy after login

in conclusion

To find the sum of a series, you need to use a loop to repeatedly add numbers in the program. In this problem we are trying to find the sum of odd numbers. So from 1 to N, we take one number at a time and check if the number is odd using the modulo 2 operator. When the remainder is 1, then it is an odd number, then display that number and combine it with the sum variable to get the final sum. The process is simple and easy to understand. But we can think of it, odd numbers always add 2. So starting from 1, if we add 2 to the number, we only get odd numbers. No additional checks are required in this case.

The above is the detailed content of C++ program to calculate the sum of all odd numbers between 1 and N. For more information, please follow other related articles on the PHP Chinese website!

source:tutorialspoint.com
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template