Skip to main content
3 of 5
Added new methods in order to satisfy comments...

Using namespaces imports the said namespace globally into your code file, in this case you are importing the standard library.

Let's say you have another custom namespace providing a function cout what will happen then.

Namespace Collision

Here your code will be confused on which function it can call, hence it will call them both leaving you with unwanted results.

Also, the statement std::cout adds clarity to your code as here you are aware that the cout belongs to the standard library.

For the answer to your question...

Yes, your code can be shorter using these 2 ways: (PS: Both of them use mathematics)

Here limit variable essentially asks, how many odd numbers you want to add? If you say 10 then it means add first 10 odd numbers not the odd numbers till 10...

First approach

#include <iostream>

/*
 * This method uses the formula (2 x n + 1) with a sign modification.
 * There n denotes the odd number at nth place and n = 0, 1, 2...
 * eg: if n = 2 then the odd number is 5 and sum till n is 9
 */

int main(){
    int limit = 10, oddSum = 0;
    
    // Here you set a limit... i.e: The value of n
    // Notice we are subtracting one from the limit
    // Hence the value of n or limit ranges from 1, 2, 3, 4.....
    /*
     * limit << 1 is a right shift operation which is an efficient way 
     * of saying limit * 2, as right shift operation multiplies the 
     * number given by 2
     */

    for(limit = limit; limit > 0; limit--)
        oddSum += (limit << 1) - 1;
    
    // Wola! You are done now print the sum
    // Yes, mathematics is important in coding

    std::cout << oddSum << std::endl;
}

Second approach

Here pow is a function in math.h

It will raise the limit i.e n to the said exponent

Please don't use it for squares, I added it so that you might learn a useful function.

pow(value, exponent)... Fancy way of saying n2

#include <iostream>

#include <math.h>

/*
 * Using mathematics we can always see that the sum of n odd numbers
 * is the square of itself... 
 * This result makes the problem very simple
 */

int main(){
    int limit = 10, oddSum = pow(limit, 2);

    // Boom! You are done now print the sum

    std::cout << oddSum << std::endl
}

Approach without mathematics

First Approach

#include <iostream>

int main(){
    int limit, oddNumber = 1, oddSum = 0;

    for (limit = 10; limit > 0; limit-- && (oddNumber = oddNumber + 2))
        oddSum += oddNumber;

    std::cout << oddSum << std::endl;
    
}

Second Approach

Here limit means add all odd numbers till the number limit. If limit = 10 then add 1, 3, 5, 7, 9...

#include <iostream>

int main(){
    int limit, oddSum = 0;

    for (limit = 10; limit > 0; limit--)
        oddSum = (limit % 2 != 0) ? (oddSum + limit): oddSum;

    std::cout << oddSum << std::endl;
}

Third Approach

Here the meaning of limit is same as the previous approach...

#include <iostream>

int main(){
    int limit = 10, oddNumber, oddSum = 0;

    for (oddNumber = 1; oddNumber <= limit; oddNumber = oddNumber + 2)
        oddSum += oddNumber;

    std::cout << oddSum << std::endl;
}

Increasing the variable range from int to long or long long will increase the number of values you can calculate... No need to overkill such a simple problem with methods. Its like bringing machine guns to a knife fight.