Skip to main content

ProwessApps

Learn · Practice · Excel

WAP to print the following series. 1 3 5 7... n

C++ Code Example — Series Programs

ADVERTISEMENT

WAP to print the following series. 1 3 5 7... n

Objective

Write a C++ program to print the odd number series: 1, 3, 5, 7 ... N.

Algorithm / Approach

  1. Read an integer n.
  2. Start a for loop with i = 1.
  3. Increment the loop by 2: i = i + 2.
  4. Print i.
main.cpp
#include<iostream>
using namespace std;
int main() {
 int n;
 cout<<"Enter the Value for N : ";
 cin>>n;
 for(int i = 1; i<=n; i=i+2) {
  cout<< i<<"  ";
 }
 cout<< endl;
return 0;
}

Expected Output

Enter the Value for N : 20
1  3  5  7  9  11  13  15  17  19

Explanation of the Program

  • This uses the exact same step-logic as the even number series.
  • The only difference is the starting point. By starting at 1 and jumping by 2, you perfectly land on all the odd numbers.

Complexity

Time Complexity O(n)
Space Complexity O(1)
ADVERTISEMENT