C++ Online Compiler
Example: Generate Cube Series in C++
C
C++
C#
Java
Python
PHP
main.cpp
STDIN
Run
// Generate Cube Series #include <iostream> using namespace std; int main() { int n; cout << "Enter the limit (n): "; cin >> n; if (n <= 0) { cout << "Please enter a positive integer for n." << endl; return 1; // Indicate an error } cout << "The series up to " << n << " terms is: "; // Step 1: Loop from 1 to n to generate each term for (int i = 1; i <= n; ++i) { // Step 2: Calculate the cube of the current number 'i' // Using 'long long' to prevent overflow for larger 'i' values long long cube = (long long)i * i * i; // Step 3: Print the calculated cube cout << cube; // Step 4: Add a comma and space if it's not the last term for neat formatting if (i < n) { cout << ", "; } } cout << endl; // Move to the next line after printing the series return 0; // Indicate successful execution }
Output
Clear
ADVERTISEMENTS