Skip to main content

ProwessApps

Learn · Practice · Excel

Java Program to print the following series. 3 6 9 12... n

Java Code Example — Series Programs

ADVERTISEMENT

Java Program to print the following series. 3 6 9 12... n

Objective

Write a Java program to print the multiples of 3 series: 3, 6, 9, 12 ... up to N terms.

Algorithm / Approach

  1. Read the number of desired terms N from the user.
  2. Start a for loop from i = 1 up to n.
  3. Inside the loop, multiply i by 3 (3 * i).
  4. Print the computed product.
  5. Repeat until n terms are generated.
Test.java
import java.util.Scanner;
class Test {
 public static void main(String[] a)
 {
  Scanner s=new Scanner(System.in);
  System.out.print("Enter N: ");
  int n = s.nextInt();
  for(int i = 1; i<=n; i++) {
   System.out.print((3*i)+"  ");
  } 
 }
}

Expected Output

Enter N: 6
3  6  9  12  15  18

Explanation of the Program

  • Notice that N in this program represents the *number of terms*, not the maximum value.
  • If the user inputs 6, the loop runs 6 times, producing the first 6 multiples of 3.
  • The mathematical formula for the i-th term in an arithmetic progression where the common difference is 3 is simply 3 * i.

Complexity

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