C Online Compiler
Example: Generate Series 1,3,6,10,15... in C
C
C++
C#
Java
Python
PHP
main.c
STDIN
Run
// Generate Series 1,3,6,10,15... #include <stdio.h> int main() { // Step 1: Declare variables for the limit, current series term, and incrementer int N; // User-defined limit int current_term = 1; // Starts with the first term of the series int increment = 2; // The value to add to the current_term, starts with 2 (1 + 2 = 3) // Step 2: Prompt the user to enter the limit N printf("Enter the upper limit (N): "); scanf("%d", &N); printf("Series up to %d: ", N); // Step 3: Loop to generate and print the series // Continue as long as the current_term does not exceed N while (current_term <= N) { // Step 3a: Print the current term printf("%d", current_term); // Step 3b: Add a comma and space if it's not the last term to be printed // This check prevents an extra comma at the end if (current_term + increment <= N) { printf(", "); } // Step 3c: Calculate the next term current_term += increment; // Add the current increment to get the next term // Step 3d: Increment the incrementer for the next iteration increment++; // The increment itself increases by 1 each time (2, then 3, then 4...) } printf("\n"); // Print a newline at the end for clean output return 0; }
Output
Clear
ADVERTISEMENTS