C Program to print the following series. 3 6 9 12 15 ... n terms
Objective
Write a C program to print the series of multiples of 3: 3 6 9 12 15 ..... n terms.
Algorithm / Approach
- Read an integer
nrepresenting the total number of terms. - Run a loop
ifrom 1 ton. - Inside the loop, calculate the term:
value = i * 3. - Print the
value.
main.c
#include<stdio.h>
int main( ){
int n, i, value;
printf("Enter Value for N : ");
scanf("%d", &n);
for(i=1; i<=n; i++) {
value = i*3;
printf("%d ", value);
}
printf("\n");
return 0;
}
Expected Output
Enter Value for N: 10 3 6 9 12 15 18 21 24 27 30
Explanation of the Program
- Notice that
nhere represents the number of *terms*, not the maximum limit. If the user enters 10, the loop runs 10 times and generates 10 multiples. - By multiplying the current loop iteration (1, 2, 3) by 3, we easily generate the multiples (3, 6, 9).
Complexity
Time Complexity
O(n)
Space Complexity
O(1)