Write a program for the following problem
To print the following series
1.-2.3.-4.5.-6 upto 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 to display the following series:-
1 -2 3 -4 5 -6....N terms.
import java.util.*;
class Series
{
public static void main(String s[])
{
Scanner sc = new Scanner(System.in);
System.out.print("Enter the number of terms for the series: ");
int n=sc.nextInt();
for(int i=1;i<=n;i++)
{
if(i%2!=0)
System.out.println(i+ " ");
else
System.out.println(-i+ " ");
}
} // end of main.
} // end of class.
Similar questions