WAP to print the series in JAVA:
1,-3,5,-7,9,-11,..............(+/-)n.
Answers
public class Series
{
public static void main(String[] args)
{
Scanner sc = new Scanner(System.in);
System.out.print("Enter the value of n: ");
int n = sc.nextInt();
int k=-1;
for(int i=1;i<=n;i+=2)
{
k *= -1;
System.out.print((k*i)+" ");
}
}
}
Answer:
import java . util . * ;
class series
{
Scanner sc = new Scanner ( System . in ) ; // creating a object of class . scanner
System. out. print ( " Enter the value of n: " ) ;
int n = sc . nextInt ( ) ;
int k = -1 ;
void calculation ( )
{
for ( int i = 1 ; i <= n ; i + = 2) // for loop
{
k * = -1 ;
System. out. print ( ( k * i ) + " ");
}
}
}
public class Main_class
{
public static void main ( string args [ ] )
{
series obj = new series ( ) ; // creating a object of class series
obj . calculation ( ) ;
}
}