Computer Science, asked by Dhruvsaraswat, 3 months ago

print -2 -4 6 8 -10 -11................. n series using loop java​

Answers

Answered by karan2387
0

Answer:

12 , 13 , - 14 , - 15 , 16 , 17 .

Answered by anindyaadhikari13
1

Required Answer:-

Question:

Write a Java program to print the given series using loop.

-2 -4 6 8 -10 -12....N

Solution:

Here is the code.

  1. import java.util.*;
  2. public class SeriesBrainly {
  3. public static void main(String[] args) {
  4. Scanner sc=new Scanner(System.in);
  5. System.out.print("Enter n - ");
  6. int n=sc.nextInt();
  7. int a=2, b=1, c=-1;
  8. for(int i=1;i<=n;i++)
  9. {
  10. System.out.print(a*c+" ");
  11. a+=2;
  12. b++;
  13. if(b>2)
  14. {
  15. c*=-1;
  16. b=1;
  17. }
  18. }
  19. sc.close();
  20. }
  21. }

Explanation:

  • In this series, two consecutive terms are negative and their next two terms are positive. Using this logic, the problem is solved.

Output is attached.

Attachments:
Similar questions