Computer Science, asked by vasanthij97, 1 year ago

write a java program to print the series: 2, 5, 8, 11, 14...

Answers

Answered by snehadhiman77
33

Answer:

ok....soo see have a look tha answer

Explanation:

import java.io.*;

public class whatever

.

.

.

..

int i = 0;

int n ;

sopln ( " enter the number of terms ");

n= integer.parseInt;

for(i=2; i less than equal to n ;i +=3);

{

sopln(i+",");

}

}

Answered by AskewTronics
11

Below are the java program for the above question:

Explanation:

import java.util.Scanner; // import the header file

public class Print// definition of print class.

{

public static void main(String[] args) // definition of main function

{

 Scanner in = new Scanner(System.in); // object of scanner class

 int n = in.nextInt(); // take input to print the series in number of times.

 int a=2; // declare a integer variable and initialize the starting value 2.

 for(int i=0;i<(n-1);i++) // for loop to print the series.

 {

     System.out.print(a+", "); // print statement to print the series

     a=a+3; // increment operation which increase the difference in series.

 }

  System.out.print(a); // print the last element of the series.

 

}

}

Output:

  • If the user enter 5 then the output is 2, 5, 8, 11, 14
  • If the user enter 4 then the output is 2, 5, 8, 11

Code Explanation:

  • This program takes an input of n which defines the times of series.
  • Then there is one variable 'a' which initializes the 2 because the series starts from 2.
  • Then there is a for loop which runs n-1 times.
  • Then there is a print statement that prints the value separated by the comma and space.
  • Then after for loop, the last element of series is printed.

Learn More:

  • Java program : https://brainly.in/question/11035609

Similar questions