TechTorch

Location:HOME > Technology > content

Technology

How to Write a Program to Print the Series 3 6 9 12 15

January 18, 2025Technology1057
How to Write a Program to Print the Series 3 6 9 12 15 To write a prog

How to Write a Program to Print the Series 3 6 9 12 15

To write a program that generates and prints the series 3 6 9 12 15, you can use loops in various programming languages. Here, we will explore how to do this in Python, JavaScript, Java, C, and C#. The series is created by multiplying each index in the range with the number 3.

Python

for i in range(1, 6):
    print(i * 3)

This Python code uses a for loop to iterate from 1 to 5, printing each value multiplied by 3. The output will be:

3
6
9
12
15

JavaScript

for (let i  1; i 

The JavaScript code also uses a for loop, iterating from 1 to 5, and logs each value multiplied by 3 to the console. The output will be:

3
6
9
12
15

Java

class Main {
    public static void main(String[] args) {
        for (int i  1; i 

In Java, the for loop is used to iterate from 1 to 5, multiplying each value by 3 and printing the result. The output will be:

3
6
9
12
15

C

#include iostream
using namespace std;
int main() {
    for (int i  1; i 

The C code again uses a for loop, but it utilizes the cout operator from the iostream library to print each value multiplied by 3 followed by a new line. The output will be:

3
6
9
12
15

C#

using System;
class Program {
    static void Main() {
        for (int i  1; i 

The C# code uses a for loop similar to the others, but it uses the Console.WriteLine method to output each value multiplied by 3. The output will be:

3
6
9
12
15

Explanation of the Logic

In each example, a loop is used to iterate through the numbers 1 to 5. During each iteration, the current loop index (i) is multiplied by 3 and the result is printed or logged. This process generates the series 3, 6, 9, 12, 15.

You can copy any of these code snippets and run them in their respective environments to see the output.

Keywords: programming series, print series in programming, loop in programming