Computer Science, asked by justinbaijuabraham, 8 months ago

Write a program

To print the following series

1,-2,3,-4,5,-6 up to n terms

Answers

Answered by Oreki
0

import java.util.Scanner;

public class Series {

public static void main(String[ ] args) {

System.out.print("Enter n - ");

int n = new Scanner(System.in).nextInt( );

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

System.out.print(((i % 2 = = 0) ? -i : i) + " ");

}

}

Answered by anindyaadhikari13
1

\star\:\:\:\sf\large\underline\blue{Question:-}

Write a program in Java to display the following series.

1 -2 3 -4 5 -6 7 -8 .....N terms.

\star\:\:\:\sf\large\underline\blue{Source\:Code:-}

import java.util.*;

class Series

{

public static void main(String s[ ])

{

Scanner sc = new Scanner(System.in);

System.out.println("Enter the number of terms for the series: ");

int n=sc.nextInt();

for(int i=0;i<n;i++)

{

System.out.print((int)(i * Math.pow(-1,i)+ " ");

}

}

}

Similar questions