64.2k views
24 votes
Write a java program to print the following series:
1 5 9 13 17...n terms​

User Sungtae
by
5.3k points

1 Answer

3 votes

Answer:

In Java:

import java.util.*;

public class Main{

public static void main(String[] args) {

Scanner input = new Scanner(System.in);

int n;

System.out.print("Max of series: ");

n = input.nextInt();

for(int i = 1; i<=n;i+=4){

System.out.print(i+" ");

}

}

}

Step-by-step explanation:

This declares n as integer. n represents the maximum of the series

int n;

This prompts the user for maximum of the series

System.out.print("Max of series: ");

This gets user input for n

n = input.nextInt();

The following iteration prints from 1 to n, with an increment of 4

for(int i = 1; i<=n;i+=4){

System.out.print(i+" ");

}

User Hudsonb
by
4.5k points