Calculating Summation of Odd Numbers in C++ Program

How can we find the summation of odd numbers from 1 to 10 in C++?

Write a C++ program to find the summation of odd numbers from 1 to 10 and skip the summation of 7 and 9.

Solution:

Here's a C++ program that calculates the summation of odd numbers from 1 to 10 while excluding the values 7 and 9:

This C++ program achieves the task using a for loop to iterate through the odd numbers and skip values 7 and 9:

            #include 
            int main() {
                int sum = 0;
                for (int i = 1; i <= 10; i += 2) {
                    if (i == 7 || i == 9) {
                        continue;
                    }
                    sum += i;
                }
                std::cout << "The summation of odd numbers from 1 to 10 (skipping 7 and 9) is: " << sum << std::endl;
                return 0;
            }
        

The program starts with an initial sum of 0 and iterates through odd numbers from 1 to 10, skipping 7 and 9. It uses an `if` statement to check and the `continue` statement to skip unwanted values. The final sum is then displayed as output.

← Patch panels the central hub for network connectivity Preventing hacking attacks essential tips to protect your personal information →