Write a program
To print the following series
1,-2,3,-4,5,-6 up to n terms
Answers
Answered by
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
1
Write a program in Java to display the following series.
1 -2 3 -4 5 -6 7 -8 .....N terms.
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