Finding Odd Numbers

Hey guys, I am new to c++ and I have been doing many exercises to help me learn to code and to train my brain on how to visualize how to start code.

Here I am trying to input a number range and print out all the odd numbers within that range.


LINK TO MY CODE: https://repl.it/@codecaine1/PrevailingPracticalMachinecode


Question 1: I have written the code but it turned out very complex. Can anyone tell me if I did good or bad? And how would I print out number 1 in my code?

Question 2: Also can anyone show me any other code that is more simple?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
#include <iostream>

int main()
{
    int start ;
    std::cout << "enter starting number: " ;
    std::cin >> start ;

    int end ;
    std::cout << "enter ending number: " ;
    std::cin >> end ;

    // print all odd numbers in [start,end]
    
    std::cout << "odd numbers in [" << start << ',' << end << "] are\n" ;

    // 1. get to the next number if start is even (make it odd)
    if( start%2 == 0 ) ++start ;

    // 2. loop from start (which is guaranteed to be odd) to end in steps of two
    for( int n = start ; n <= end ; n += 2 ) std::cout << n << '\n' ;
}
Topic archived. No new replies allowed.