print -2 -4 6 8 -10 -11................. n series using loop java
Answers
Answered by
0
Answer:
12 , 13 , - 14 , - 15 , 16 , 17 .
Answered by
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.
- import java.util.*;
- public class SeriesBrainly {
- public static void main(String[] args) {
- Scanner sc=new Scanner(System.in);
- System.out.print("Enter n - ");
- int n=sc.nextInt();
- int a=2, b=1, c=-1;
- for(int i=1;i<=n;i++)
- {
- System.out.print(a*c+" ");
- a+=2;
- b++;
- if(b>2)
- {
- c*=-1;
- b=1;
- }
- }
- sc.close();
- }
- }
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