WAP to print the following series. 3 6 9 12... n
Objective
Write a C++ program to print the multiples of 3 series: 3, 6, 9, 12 ... N.
Algorithm / Approach
- Read an integer
n. - Start a
forloop from 1 ton. - Print the mathematical product:
3 * 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++) {
cout<< 3*i<<" ";
}
cout<< endl;
return 0;
}
Expected Output
Enter the Value for N : 10 3 6 9 12 15 18 21 24 27 30
Explanation of the Program
- Instead of modifying the loop's step counter (like
i = i + 3), this program keeps a standard 1-by-1 loop but modifies what it prints. - Multiplying the standard loop counter by 3 generates the mathematical times table for 3.
Complexity
Time Complexity
O(n)
Space Complexity
O(1)